Arduino: Pulse-Width Modulation with analogWrite()

Posted by Unknown on Friday, July 11, 2014 with No comments
we can generate analog output values by using a trick called pulse-width modulation(PWM). Select pins on each Arduino can use the analogWrite()command to generate PWM signals that can emulate a pure analog signal when used with certain peripherals. These pins are marked with a ~ on the board.On the Arduino Uno, Pins 3, 5, 6, 9, 10, and 11 are PWM pins. If you’re using an Uno, you can continue to use the circuit from Figure 2-1 to test out the analogWrite()command with your LED. Presumably, if you can decrease the voltage being dropped across the resistor, the LED should glow more dimly because less current will flow. That is what you will try to accomplish using PWM via the analogWrite()command. The analogWrite()command accepts two arguments: the pin to control and the value to write to it.The PWM output is an 8-bit value. In other words, you can write values from 0 to 28-1, or 0 to 255. Try using a similar forloop structure to the one you used previously to cycle through varying brightness values .

SKETCH:
const int LED=9; //define LED for Pin 9
void setup()
{
pinMode (LED, OUTPUT); //Set the LED pin as an output
}
void loop()
{
for (int i=0; i<256; i++)
{
analogWrite(LED, i);
delay(10);
}
for (int i=255; i>=0; i--)
{
analogWrite(LED, i);
delay(10);
}
}

What does the LED do when you run this code? You should observe the LED fading from off to on, then from on to off. Of course, because this is all in the main loop, this pattern repeats ad infinitum. Be sure to note a few differences in this forloop. In the first loop, i++is just shorthand code to represent i=i+1. Similarly, i--is functionally equivalent to i=i–1. The first for loop fades the
LED up, and the second loop fades it down.

Pulse Width Modulation

PWM control can be used in lots of circumstances to emulate pure analog control, but it cannot always be used when you actually need an analog signal. For instance, PWM is great for driving direct current (DC) motors at variable speeds, but it does not work well for driving speakers unless you supplement it with some external circuitry. Take a moment to examine how PWM actually works.


PWM works by modulating the duty cycle of a square wave (a signal that 
switches on and off). Duty cycle refers to the percentage of time that a square 
wave is high versus low. You are probably most familiar with square waves that 
have a duty cycle of 50%—they are high half of the time, and low half of the time.

The analogWrite()command sets the duty cycle of a square wave depending
on the value you pass to it:

■Writing a value of 0 with analogWrite()indicates a square wave with a
duty cycle of 0 percent (always low).
■Writing a 255 indicates a square wave with a duty cycle of 100 percent
(always high).
■Writing a 127 indicates a square wave with a duty cycle of 50 percent
(high half of the time, low half of the time).

The graphs in Figure show that for a signal with a duty cycle of 25 percent, it is high 25 percent of the time, and low 75 percent of the time. The frequency of this square wave, in the case of the Arduino, is about 490Hz. In other words, the signal varies between high (5V) and low (0V) about 490 times every second.So, if you are not actually changing the voltage being delivered to an LED, why do you see it get dimmer as you lower the duty cycle? It is really a result of your eyes playing a trick on you! If the LED is switching on and off every 1ms (which is the case with a duty cycle of 50 percent), it appears to be operating at approximately half brightness because it is blinking faster than your eyes can perceive. Therefore, your brain actually averages out the signal and tricks you into believing that the LED is operating at half brightness.











Categories: