Pulse Width Modulation (PWM)

PWM is a technique that uses a digital signal switching rapidly between HIGH and LOW to produce an output that behaves like an analog voltage. Let's understand why we need it and how it works.

Digital vs Analog

Digital Signals

A digital signal has only two states: HIGH (3.3V on Pico) or LOW (0V). There's nothing in between. Think of it like a light switch that can only be fully ON or fully OFF.

Analog Signals

An analog signal can have any voltage value within a range. Instead of just ON or OFF, it varies continuously and smoothly. Think of it like a dimmer switch with infinite positions between off and fully bright.

The Problem

Most microcontroller pins are digital - they can only output HIGH or LOW. But what if you want to:

  • Dim an LED to 50% brightness instead of just ON or OFF?
  • Control a servo motor to any position between 0° and 180°?
  • Adjust the speed of a fan or motor?

You need something that acts like an analog output, but you only have digital pins. This is where PWM comes in.

How PWM Works

PWM rapidly switches a digital signal between HIGH and LOW. When this switching happens very quickly, the connected device can't follow each individual change. Instead, it responds to the average voltage over time.

Normal signal vs PWM signal

Normal 3.3V signal vs PWM signal at 50% duty cycle

Example: 50% Duty Cycle

If the signal is HIGH for half the time and LOW for the other half, the device receives about half the voltage on average. On a 3.3V system, this behaves like approximately 1.65V.

Pulse Width and Duty Cycle

Pulse Width

How long the signal stays ON before it turns OFF. Measured in time (microseconds or milliseconds). For example, if a pulse has a width of 1 millisecond, the signal stays HIGH for 1ms then turns LOW.

Duty Cycle

The percentage of time the signal stays ON during one complete cycle. A 0% duty cycle means always LOW. A 50% duty cycle means HIGH and LOW for equal time. A 100% duty cycle means always HIGH.

LED brightness at different duty cycles

LED brightness changes with duty cycle

Duty Cycle Examples

0% Duty Cycle

Signal always LOW (0V) - LED completely off

25% Duty Cycle

HIGH for 25% of time, LOW for 75% - LED dim

50% Duty Cycle

HIGH and LOW for equal time - LED medium brightness

75% Duty Cycle

HIGH for 75% of time, LOW for 25% - LED bright

100% Duty Cycle

Signal always HIGH (3.3V) - LED fully on

Period and Frequency

Period

The total time it takes for one complete ON-OFF cycle to finish. Measured in seconds, milliseconds, or microseconds. For example, a 20ms period means one cycle takes 20 milliseconds.

Frequency

How many complete cycles happen in one second. Measured in Hertz (Hz). 50Hz means 50 cycles per second. Frequency = 1 / Period.

Note

Example: If the period is 20ms (0.02 seconds), the frequency is 1 / 0.02 = 50Hz.

Servo motors typically operate at 50Hz, meaning they expect a control pulse every 20 milliseconds.

Common PWM Uses

  • LED Dimming: Control brightness by changing duty cycle
  • Servo Control: Pulse width determines servo angle
  • Motor Speed: Duty cycle controls average power to motor
  • Audio Generation: Create tones by varying frequency
Tip

The RP2350 has dedicated PWM hardware with 12 PWM slices, each with 2 channels. This means you can control up to 24 PWM outputs simultaneously without using CPU resources.