You are on page 1of 6

PWM, Random and using a potentiometer

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().

Syntax: analogWrite(pin, value)

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.

A.-Connect the LED to digital output pin 9 through a resistor.

B.-Copy this code and try it:

Int ledPin = 9; // LED connected to digital pin 9


Int fadeValue = 150; //value of brightness we want for the LED - between 0 and 255

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.

Something like: if (i == 255) x = -1;

Try it and when it works, save it as “Dimming LED” in your USB key.

Exercise 4 Use of “random” with pwm

“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.

int ledPin = 9;    // LED connected to digital pin 9


int fadeValue = 150;    //value of brightness we want for the LED - between 0 and 255

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

You must be able to vary the


brightness using the potentiometer.

Look at the circuit and copy it

You will want to read a value from


the potentiometer, and then write a
voltage to the LED based on the
reading from the potentiometer. You
must understand that when you read
an analog voltage between 0 and 5
volts, the arduino will report a
number between 0 and 1023, with 0
representing 0 volts, and 1023
representing 5 volts.

Similarly, when you are writing an


analog voltage between 0 and 5 volts,
you must write a number between 0
and 255. If you write a “0” value, that corresponds to 0 volts. If you write a value of 255,
that will output 5 volts. So, you must scale your write values between 0 and 255 to get
voltages between 0 and 5 volts.

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:

int analogPin = A3; // potentiometer connected to analog pin 3

int val = 0; // variable to store the read value

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);  
}

Try it and then …

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.

Try it and save as “LED and potentiometer” in your USB key

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 6

You might also like