Professional Documents
Culture Documents
PWM, Random and Using A Potentiometer: Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-Mail: 1
PWM, Random and Using A Potentiometer: Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-Mail: 1
If you look at the Arduino board pins carefully, you will see there are some which have
pwm or ~) written on them next to the number.
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital
means. Digital control is used to create a square wave, a signal switched between on and off.
This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by
changing the portion of the time the signal spends on versus the time that the signal spends
off. The duration of "on time" is called the pulse width. To get varying analog values, you
change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an
LED for example, the result is as if the signal is a steady voltage between 0 and 5v
controlling the brightness of the LED.
In the graphic below, the green lines represent a regular time period. This duration or period
is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at
about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is
on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on),
and analogWrite(127) is a 50% duty cycle (on half the time) for example.
Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 1
analogWrite()
Description
Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying
brightnesses or drive a motor at various speeds.
You do not need to call pinMode() to set the pin as an output before calling
analogWrite().
Parameters:
1 .- pin: the pin to write to.
2.- value: the duty cycle: between 0 (always off) and 255 (always on).
Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 2
Exercise 1 Fading an LED
This example demonstrates the use of analog output (Pulse Width Modulation (PWM)) to
fade an LED.
void setup() {
// nothing happens in setup
}
void loop() {
analogWrite( ledPin,
fadeValue );
}
C.- Change the value of “fadeValue” and
see the difference.
Exercise 2
Using “for” make the LED dim from 0 to 255. Review the “for” function and you will see
it is quite easy.
You will need some “delay” value in order to notice the dimming of the LED. If you don’t
use “delay” it will change so quickly that you will think there is no change.
Exercise 3
Let’s improve the 2nd exercise. You must make it dim downwards when it reaches the
maximum value. So, it must go from 0 to 255 and then again to 0 in a continuous loop.
Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 3
Hint: use an “if” function to reverse the value (x) you add to the “for” function.
Try it and when it works, save it as “Dimming LED” in your USB key.
“Random” is an interesting function which will give as a random (aleatory) value between
the minimum and maximum numbers we have decided.
Example: random(100,255) will give us some value between 100 and 255.
If we use this function with just one parameter, for example random(200), It will be taken
as the maximum. (the minimum will be 0)
Let’s use it in our pwm LED exercise to make the brightness change randomly. This is very
useful to make an LED appear as real fire.
void setup() {
// nothing happens in setup
}
void loop() {
fadeValue = random(50,255) // we decide the minimum and maximum. Try different
numbers
analogWrite(ledPin, fadeValue);
delay(50) // we need a small pause to notice the random function.
}
Try it and save it as “Random and LED” in your USB key.
Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 4
Exercise 5 Using a potentiometer
The tricky thing now is that we want to dim the LED based on what value we read from the
potentiometer. If we read a 0 value from the potentiometer, we want to write a value of 0,
which corresponds to a voltage of 0. If we read a value of 1023 from the potentiometer, then
we will want to write our maximum voltage of 5 volts, which means we need to write a
value of 255. Basically, we need to scale our read values, which will be between 0 and 1023
to suitable write values, which should be between 0 and 255.
The Write Value that we should write to the LED should be the value that we are reading
from the potentiometer * (255/1023)
Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 5
Now look at the code:
void setup()
{
pinMode(9, OUTPUT); // sets the pin as output
}
void loop()
{
val = analogRead(analogPin); // read the input pin
writeValue = val * (255/1023); // calculating the brightness for the LEDS
analogWrite(9, writeValue);
}
Exercise 6:
Use the … Serial.begin(9600); Serial.print and Serial.println to write the value of the
potentiometer and see the result at the same time you look at the LED.
Could you write the volts you are sending to the LED? Explain in the code the way we can
calculate it.
Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 6