Intro to Arduino: Pulse Width Modulation (PWM)

< All Topics
NightShade Electronics - Intro to Arduino: Pulse Width Modulation (PWM)
PWM Duty Varying between 0% and 100%

Pulse width modulation (PWM) is a way of creating an analog signal from digital pulses. A PWM has two parameters: the frequency of the PWM and the duty of the PWM. The frequency of a PWM is how often it is pulsing. If you have a 1kHz PWM, there are 1000 pulses every second. The duty of the PWM is the percent of the pulse that the output is high. If the duty of the PWM is set to 30%, then the output is high for 30% of the pulse and then low for 70% of the pulse.

The analog nature of a PWM can be used to do things like dim LEDs. Sometimes a PWM is also used as a control signal for another device. This is how servos are controlled.

In the Arduino IDE

In the Arduino IDE, the PWMs are used by calling the function analogWrite(). The argument for the analogWrite() function is a value from 0-255, which corresponds to duty cycles from 0-100%. Only certain pins on Arduino boards are able to output a PWM in this fashion. These pins are usually marked on the board.

analogWrite(3, 127); // Ouput 50% (127/255) duty cycle on pin 3

The frequency of the PWM pins on Arduino boards is approximately 490 Hz, while some pins (5 & 6 on the Uno, 3 & 11 on the Leonardo, etc.) have a frequency of about 977 Hz. While these are the default frequencies of the PWMs on the Arduino boards, they can be adjusted to frequencies as low as 31 Hz and as high as 61.5 kHz. You can find out more about this on the Arduino Timer PWM Cheat Sheet page

Hardware vs Software PWM

The native PWM pins on an Arduino board are driven by timer modules inside the processor. This allows the PWM to continue operating without the attention of the processor.

The other way to make a PWM is to have software manually turn a pin on and off. This can be done on any output pin, but it requires the processor to constantly think about it and do nothing else. If you want to create a software PWM, you can program it manually or use one of the available software PWM libraries that are available for Arduino. (Search “Arduino Software PWM“)

Example

The example below is an Arduino example sketch to demonstrate dimming an LED with a PWM. In this example, an LED is wired to pin 9 and the analogWrite() function is used to change the brightness of the LED.

/*
 Fade

 This example shows how to fade an LED on pin 9
 using the analogWrite() function.

 The analogWrite() function uses PWM, so if
 you want to change the pin you're using, be
 sure to use another PWM capable pin. On most
 Arduino, the PWM pins are identified with 
 a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.

 This example code is in the public domain.
 */

int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}
Previous Intro to Arduino: Analog Input
Next Intro to Arduino: Basics of Writing a Sketch (Blinking LED)
Table of Contents