• EUR€
  • £GBP
  • $USD

Arduino Projects 8: Light Regulator

DFRobot Jun 22 2017 391

A light regulator is a device with which you can control brightness of a light. In this session, we’ll show you how to control the brightness of an LED with an analog rotation sensor (also known as a potentiometer). Using this, the light will change from dim to bright and bright to dim depending on the rotation of the sensor. The greater the rotation angle, the brighter the LED, and vice versa.

The rotation sensor can also be used to control the rotation angle of a servo or the speed of a DC motor. You can find many uses for the rotation sensor.

Parts Needed:

DFRduino Uno  (similar as Arduino UNO R3) x1
I/O Sensor Expansion Shield V7.1  x1
Digital Piranha  LED-R  x1 
Analog Rotation Sensor  x1 


Be sure that your power, ground and signal connections are correct or you risk damaging your components.
When you have connected the components and checked your connections, connect your arduino microcontroller to your PC with the USB cable so you can upload a program.

Hardware Analysis
(Analog Input-Analog Output)

In the last session we learned how to achieve analog output with a digital pin using PWM. In this session, we are going to control analog output using analog input.
The rotation sensor is the input device. The LED is the output device. Both input and output devices use analog values.

Code Input

// Item Six—Light –regulator
int potPin = 0; // Analog Rotation Sensor shall be attached to analog pin 0
int ledPin = 10; // LED shall be attached to digital pin No.10
pinMode(ledPin, OUTPUT); }
int sensorValue = analogRead(potPin); // collect value at analog pin No.0
// A value between 0 and 1023 to mapped to a value between 0 and 255
int outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(ledPin, outputValue); // write corresponding value for LED

void setup() {

void loop() {

Use this sample code to implement the behavior we want.
You can copy and paste it in to the Arduino IDE, but if you want to develop you skills we recommend typing it out.

When you have finished, click “Verify” to check the code for syntax errors. If the code verifies successfully,
you can upload it to your DFRduino UNO (same as Arduino UNO) microcontroller.

Turn the shaft of the rotation sensor slowly. The LED’s brightness should change as you turn it.

Code Analysis

Here we have used the map function. The map function is used to map a value within a range to one within another range. Let’s examine it a little closer:
map(value, fromLow,

The function’s syntax is as follows:

The function maps a value between fromLow and fromHigh to one between toLow and toHigh.

The parameters included in the map function are:

value  the value that needs to be mapped
fromLow lower limit of the current range
fromHigh upper limit of the current range
toLow lower limit of the target range
toHigh upper limit of the target range

One advantage of map function is that the lower limit of both ranges can be a number greater than their upper limits:
y = map (x, 1, 50, 50, 1);

The value included in the map function can also be a negative number:
y = map (x, 1, 50, 50, -100);

Let’s re-examine the code:
int outputValue = map(sensorValue, 0, 1023, 0, 255);
The code above is used to map a value (0-1023) read from the analog pin to one used on the PWM pin (0-255).

There is also another arduino starter kit for you to choose.