page24
Pulse Width Modulation (PWM)
The Arduino only has digital (0V or 5V) outputs. However it can output an approximation of an intermediate analog voltage using a technique called Pulse Width Modulation (PWM). In PWM a digital output moves between the on and off states very quickly. The ratio of the the “on” time to the “off” time (called the “duty cycle”) can be varied to create an average voltage which is intermediate between on and off voltages.
PWM signals aren't suitable for everything, but they do work well for some types of system – in particular:
LEDs where the rate of blinking is too fast for the human eye to see, and
Small motors which can have the speed varied by PWM.
PWM in the Arduino
PWM is only available on the special outputs labelled with “PWM” on the board. There are further restrictions if you want to combine PWM with the “tone” function. To send a PWM signal set the pin as an output and use the analogWrite() function with a value between 0 for all off and 255 for all on (see diagram). The designers of Arduino made a big mistake naming this function – there is no analog output. “PWMWrite()” would be a much better name!
To test the effect of PWM on an LED keep the same circuit as before and load the sketch “organandlight” and upload to the Arduino This sends a PWM signal to pin 6 which depends on the pitch of the organ – the LED should be bright for low notes and dimmer for higher notes.
LED
Dim
LED
Bright
Motor
Slow
Motor
Fast
LED
Off
Motor
Off