0

$USD

$USD
TUTORIALS IoT

Intel® Edison Hands-on Day 3: Light dimmer

DFRobot Oct 28 2014 1458

Light dimmer
Light dimmers are devices used to vary the brightness of a light. Through a rotation sensor, we can tune the intensity of the light output. More you rotate the knob, the brighter the LED will be. So let’s try to make a dimmer by ourselves.
Rotation sensor can be applied in various kinds of application, such as the servo controlling, DC motor speed controlling and etc.
 
Component required


 
Connection
Analog Rotation Sensor → Analog Pin 0
Digital piranha LED light module → Digital Pin 9




Coding
  // light dimmer int potPin = 0; // Analog Rotation Sensor connected to Analog Pin 0 int ledPin = 9; //LED connected to Digital Pin 9 void setup() { pinMode(ledPin, OUTPUT); } void loop() { int sensorValue = analogRead(potPin); //Read value from Analog Rotation Sensor // Shrink the value range from 0~1023 to 0~255 via map() function int outputValue = map(sensorValue, 0, 1023, 0, 255); analogWrite(ledPin, outputValue); //Write the LED delay(2); } Slowly rotate the knob, and you can see the varying brightness.
 
Principle?Analog Input—Analog Output?
     
Analog Rotation Sensor acts as an input device, while the PWM output controls the brightness of the LED


 

Coding review

Let’s talking about the map() function
This is the format of the map() function?
map(value, fromLow, fromHigh, toLow, toHigh)
Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.
 
Parameters of map()?
value?the number to map
fromLow?the lower bound of the value's current range
fromHigh?the upper bound of the value's current range
toLow?the lower bound of the value's target range
toHigh?the upper bound of the value's target range
 
Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so the map() function may be used to reverse a range of numbers, for example
y = map(x, 1, 50, 50, 1);
 
The function also handles negative numbers well, so that this example
y = map(x, 1, 50, 50, -100);
   
    Turning back to our example?
    int outputValue = map(sensorValue, 0, 1023, 0, 255);
What we want is to Shrink the value range from 0~1023(from analog value) to 0~255(to PWM output)