Overview
In this guide, we will learn about the PWM, i.e Pulse Width Modulation in Raspberry Pi Pico. We will be gradually changing the luminance of an LED through programming. Since the pulsing light looks like breathing, we give it a magical name – breathing LED. We’ll accomplish this effect with pulse width modulation (PWM).
Pulse Width Modulation or PWM is a common technique used to vary the width of the pulses in a pulse train. PWM has many applications such as controlling servos and speed controllers, limiting the effective power of motors and LEDs.
Components Required
In this guide, I used Elecrow Raspberry Pi Pico Starter Kit to test PWM results. You can buy the kit and perform some other operations as well. From this kit, you can use the following components.
- Raspberry Pi Pico
- Red/Green/Blue Any LED
- Breadboard
- Jumper Wires
- USB Cable
Pulse Width Modulation (PWM)
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 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 width. If you repeat this on-off pattern fast enough with some device, an LED, for example, it would be like this: the signal is a steady voltage between 0 and 5V controlling the brightness of the LED.
You will find that the smaller the PWM value is, the smaller the value will be after being converted into voltage. Then the LED becomes dimmer accordingly. Therefore, we can control the brightness of the LED by controlling the PWM value.
PWM Pins Raspberry Pi Pico
The RP2040 microcontroller at the core of the Raspberry Pi Pico has 8 Slices of PWM, and each Slice is independent of the others. This simply means that we can set the frequency of a Slice without affecting the others.
The RP2040 PWM block has 8 identical PWM slices, each with two output channels (A/B), where the B pin can also be used as an input for frequency and duty cycle measurement. That means each slice can drive two PWM output signals, or measure the frequency or duty cycle of an input signal. This gives a total of up to 16 controllable PWM outputs. All 30 GPIO pins can be driven by the PWM block.
An interesting thing at this moment to recall about PWM on the RP2040 is the possibility of having Duty Cycle values at 0% and at 100% without spurious peaks as happens on most other microcontrollers. Besides, it might be possible to have the two outputs of the same Slice with inverted phase signals.
In the above pin diagram, pins 21/GP16 and 22/GP17 belong to the same slice – GP16 is output A and GP17 is output B.
PWM Usage in Raspberry Pi Pico
In this example we will use a Raspberry Pi Pico PWM pin with an LED. Through PWM pulse width modulation, the LED light is controlled to light up gradually, and then gradually turn off, so as to form a breathing light effect in a cycle.
Connect the positive Pin of the LED to Pi Pico GP2 Pin and the negative Pin to GND.
MicroPython Code/Program
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from machine import Pin, PWM import utime led = PWM(Pin(2)) led.freq(1000) # Set the frequency value led_value = 0 #LED brightness initial value led_speed = 5 # Change brightness in increments of 5 if __name__ == '__main__': while True: led_value += led_speed led.duty_u16(int(led_value * 500)) # Set the duty cycle, between 0-65535 utime.sleep_ms(100) if led_value >= 100: led_value = 100 led_speed = -5 elif led_value <= 0: led_value = 0 led_speed = 5 |
Copy the above code and run the script.
The LED connected to Raspberry Pi Pico will behave as a breathing LED as its illuminance and brightness change after regular inteval.
The frequency (led.freq) tells Raspberry Pi Pico how often to switch the power between on and off for the LED. The duty cycle tells the LED for how long it should be on each time. For Raspberry Pi Pico in MicroPython, this can range from 0 to 65025. 65025 would be 100% of the time, so the LED would stay bright. A value of around 32512 would indicate that it should be on for half the time.
This is how you can use (PWM) Pulse Width Modulation in Raspberry Pi Pico to control Breathing LED.











