Controlling Light with PWM
As I hinted after the first example, if you change the num- bers in the delay function until you don’t see the LED blinking any more, you will notice that the LED seems to be dimmed at 50% of its normal brightness. Now change the numbers so that the LED is on is one quarter of the time that it’s off. Run the sketch and you’ll see that the brightness is roughly 25%. This technique is called pulse width modulation (PWM), a fancy way of saying that if you blink the LED fast enough, you don’t see it blink any more, but you can change its brightness by changing the ratio between the on time and the off time.
This technique also works with devices other than an LED. For example, you can change the speed of a motor in the same way.
While experimenting, you will see that blinking the LED by putting delays in your code is a bit inconvenient, because as soon as you want to read a sensor or send data on the serial port, the LED will flicker while it’s waiting for you to finish reading the sensor. Luckily, the processor used by the Arduino board has a piece of hardware that can very efficiently blink three LEDs while your sketch does something else. This hardware is imple- mented in pins 9, 10, and 11, which can be controlled by the analogWrite() instruction.
For example, writing analogWrite(9,128) will set the brightness of an LED con- nected to pin 9 to 50%. Why 128? analogWrite() expects a number between 0 and 255 as an argument, where 255 means full brightness and 0 means off.
Text Box
// Example 04: Fade an LED in and out like on
// a sleeping Apple computer
const int LED = 9; // the pin for the LED
int i = 0; // We’ll use this to count up and down
void setup() {
pinMode(LED, OUTPUT); // tell Arduino LED is an output
}
void loop(){
for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
analogWrite(LED, i); // set the LED brightness
delay(10); // Wait 10ms because analogWrite
// is instantaneous and we would
// not see any change
}
for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)
analogWrite(LED, i); // set the LED brightness
delay(10); // Wait 10ms
}
}
ANALOG SENSORS
Use a Light Sensor Instead of the Pushbutton
Now we’re going to try an interesting experiment. Take a light sensor, like the one pictured You can get a bag of these from Maker Shed (part number JM169578) or from bit.ly/ArduinoStoreLDR.
In darkness, the resistance of a light-dependent resistor (LDR) is quite high. When you shine some light at it, the resistance quickly drops and it becomes a reasonably good conductor of electricity. It is thus a kind of light-activated switch.
Build the circuit that came with Example 02 (see “Using a Pushbutton to Control the LED” ), then upload the code from Example 02 to your Arduino.
Now plug the LDR onto the breadboard instead of the pushbutton. You will notice that if you cover the LDR with your hands, the LED turns off. Uncover the LDR, and the light goes on. You’ve just built your first real sensor-driven LED. This is important because for the first time in this book, we are using an electronic component that is not a simple mechani- cal device: it’s a real rich sensor.
Analog Input
As you learned in the previous section, Arduino is able to detect whether there is a voltage applied to one of its pins and report it through the digitalRead() function. This kind of either/or response is fine in a lot of applications, but the light sensor that we just used is able to tell us not just whether there is light, but also how much light there is. This is the difference between an on/off sensor (which tells us whether something is there) and an analogue sensor, whose value continuously changes.
In order to read this type of sensor, we need a different type of pin.
In the lower-right part of the Arduino board, you’ll see six pins marked “Analog In”; these are special pins that can tell us not only whether
there is a voltage applied to them, but if so, also its value. By using the analogRead() function, we can read the voltage applied to one of the pins. This function returns a number between 0 and 1023, which represents voltages between 0 and 5 volts. For example, if there is a voltage of 2.5 V applied to pin number 0, analogRead(0) returns 512.
If you now build the circuit below, using a 10k resistor, and run the code
Text Box
// simple ldr sensor // connected to analog in 0 int senseur=0; void setup(){ Serial.begin(9600); } void loop(){ int val=analogRead(senseur); Serial.println(val); delay(100); }
If you now use the same circuit below, using a 10k resistor, and run the code listed below, you’ll see the onboard LED
(you could also insert your own LED into pins 13 and GND as shown in “Blinking an LED” ) blinking at a rate that’s dependent upon the amount of light that hits the sensor.
Text Box
// Example: Blink LED at a rate specified by the
// value of the analogue input
const int LED = 13; // the pin for the LED
int val = 0; // variable used to store the value
// coming from the sensor
void setup() {
pinMode(LED, OUTPUT); // LED is as an OUTPUT
// Note: Analogue pins are
// automatically set as inputs
}
void loop() {
val = analogRead(0); // read the value from
// the sensor
digitalWrite(LED, HIGH); // turn the LED on
delay(val); // stop the program for
// some time
digitalWrite(LED, LOW); // turn the LED off
delay(val); // stop the program for
// some time
}
Set the brightness of LED to a brightness specified by the value of the analogue input
Connect the LED + Anode (long pin) to a 270 resistor
Connect the other leg of the resistor to Digital PMW pin 9~
Connect the LED CATHODE (short leg) to Ground
Text Box
// Example 06B: Set the brightness of LED to
// a brightness specified by the
// value of the analogue input
const int LED = 9; // the pin for the LED
int val = 0; // variable used to store the value
// coming from the sensor
void setup() {
pinMode(LED, OUTPUT); // LED is as an OUTPUT
// Note: Analogue pins are
// automatically set as inputs
}
void loop() {
val = analogRead(0); // read the value from
// the sensor
analogWrite(LED, val/4); // turn the LED on at
// the brightness set
// by the sensor
delay(10); // stop the program for
// some time
}
Other Analog sensors Potentiometers/Flex Sensors
Arduino Video Tutorial -03 The Love-O-Meter