1 Basic PWM Properties: EAS 199 Basic Pulse Width Modulation Fall 2011 Gerald Recktenwald
1 Basic PWM Properties: EAS 199 Basic Pulse Width Modulation Fall 2011 Gerald Recktenwald
Vs
...
c
void loop() {
byte PWM_out_level;
Listing 1: Skeleton of an Arduino sketch to demonstrate the use of pinMode and analogWrite in
controlling PWM output.
expression, and is equivalent to a numerical value of 1. Electrically, a value of HIGH means the pin
voltage is close to 5 V. Similarly, the constant LOW is interpreted as false in conditional expressions,
it is numerically equivalent to 0, and electrically the pin voltage is 0. When a digital I/O pin is
configured for output, digitalWrite is used to set the pin voltage to HIGH or LOW.
On an Arduino Uno, PWM output is possible on digital I/O pins 3, 5, 6, 9, 10 and 11. On these
pins the analogWrite function is used to set the duty cycle of a PWM pulse train that operates at
approximately 500 Hz2 . Thus, with a frequency fc = 500 Hz, the period is c = 1/fc 2 ms. As
with conventional digital I/O, the pinMode function must be called first to configure the digital pin
for output.
The skeleton of a sketch in Listing 1 shows the basic code components for using PWM on an
Arduino. Two integer variables, PWM_out_pin and PWM_out_level are used to indicate the pin
number and output level respectively. In the setup function, the statement
pinMode(PWM_out_pin, OUTPUT);
configures the PWM_out_pin for output. The OUTPUT symbol is a constant defined by the Arduino
programming tools (the IDE or Integrated Development Environment). The statement
analogWrite( PWM_out_pin, PWM_out_level);
sets the PWM_out_pin pin to the value of the PWM duty cycle and hence the effective voltage
according to Equation (1). The parameter that sets the duty cycle is an 8-bit unsigned integer, i.e.,
a value in the range 0 PWM_out_level 255.
The digital output voltage of an Arduino Uno is either 0 V or 5 V. Thus, in Equation (1),
Vs = 5 V. The PWM output level specified with the analogWrite is an 8-bit value that corresponds
to an effective voltage range of 0 to 5 V. Figure 2 depicts the relationships between the PWM output
parameters. The quantities in Figure 2 are linearly related. Thus,
o Veff
PWM_out_level = 255 = 255
c Vs
Remember that the second input to analogWrite is an unsigned 8-bit value, so PWM_out_level
should be an byte variable type. Since Vs = 5 V always, for routine calculations, we can use the
simple formula
255
PWM_out_level = Veff (2)
5
2 See http://www.arduino.cc/en/Tutorial/PWM and http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM.
EAS 199 :: Basic PWM Output 3
1 1 5V 255
o Veff
Veff
c Vs
PWM_out_level
0 0 0 0
Figure 2: Scaling relationships for PWM parameters. PWM out level is the 8-bit value used as the
second parameter to the analogWrite function.
3 Implementation Examples
We now show how PWM can be applied to two practical situations: controlling the brightness of
an LED, and controlling the speed of a DC motor. The Arduino code for both cases is the same.
// Use pins be 0, 1, ..., 13 for digital I/O // Use pins 3, 5, 6, 9, 10 or 11 for PWM
int LED_pin = 11; int LED_pin = 11;
// must be in the range 0 <= level <= 255
int level = 20;
The Blink dim.pde program on the right hand side of Listing 2 uses the analogWrite function
to supply a variable voltage level to the LED.
2
5V
Digital
output
Digital
output
Figure 3: Electrical circuit for the basic blink program on the left, and the transistor-controlled
blink program. Note that the program to drive these two circuits can be identical as long as the
same digital output pin is used for both circuits.
EAS 199 :: Basic PWM Output 5
// File: LED_three_levels.pde
//
// Use PWM to control the brightness of an LED
// Repeat a pattern of three brightness levels
void setup() {
pinMode(LED_pin, OUTPUT); // Initialize pin for output
}
void loop() {
analogWrite(LED_pin, V1);
delay(dtwait);
analogWrite(LED_pin, V2);
delay(dtwait);
analogWrite(LED_pin, V3);
delay(dtwait);
}
The LED three levels.pde sketch in Listing 3 causes an LED to glow at three different
brightess levels specified by the variables V1, V2 and V3. Each level is held for a time interval
specified by the dtwait variable.
2
+5V
Orient the diode so
+
that the black stripe
is at the same voltage
as the positive motor
motor terminal
control
pin 330 Collector. Connect to
negative terminal
of motor
Figure 4: Electrical circuit for using PWM to control the speed control of a DC motor.
EAS 199 :: Basic PWM Output 7