1. Home
  2. Knowledge Base
  3. Tutorials
  4. Arduino
  5. Intro to Arduino: Pulse Width Modulation (PWM)

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.

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



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.


Was this article helpful?

Related Articles