Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » Breathing LED: PWM Usage in Raspberry Pi Pico
Raspberry Pi Raspberry Pi Pico Projects

Breathing LED: PWM Usage in Raspberry Pi Pico

Mamtaz AlamBy Mamtaz AlamUpdated:November 3, 20224 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
Pulse Width Modulation PWM Raspberry Pi Pico
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

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.

  1. Raspberry Pi Pico
  2. Red/Green/Blue Any LED
  3. Breadboard
  4. Jumper Wires
  5. 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.

Raspberry Pi Pico Breathing LED


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.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous Article32 Projects with Elecrow Raspberry Pi Pico Advance Kit
Next Article Colorful Flowing Lights using Raspberry Pi Pico

Related Posts

ADXL375 Accelerometer with Raspberry Pi Pico & MicroPython

ADXL375 Accelerometer with Raspberry Pi Pico & MicroPython

Updated:July 24, 2025
Interface BMI160 with Raspberry Pi Pico & MicroPython

Interface BMI160 with Raspberry Pi Pico & MicroPython

Updated:February 2, 20253K
Shift Register 74HC595 with Raspberry Pi Pico & MicroPython

Shift Register 74HC595 with Raspberry Pi Pico & MicroPython

Updated:February 2, 202513K
Interfacing XBee Module with Raspberry Pi Pico & MicroPython

Interfacing XBee Module with Raspberry Pi Pico & MicroPython

Updated:February 2, 20253K
Modbus RTU with Raspberry Pi Pico & Micropython

Modbus RTU with Raspberry Pi Pico & MicroPython

Updated:February 2, 20258K
Fever Detector with MLX90640 & OpenCV Raspberry Pi

Thermal Fever Detector with MLX90640 & OpenCV Raspberry Pi

Updated:February 2, 20256K
Add A Comment

CommentsCancel reply

Latest Posts
ESP32 Fingerprint Attendance System with Live Web Dashboard

ESP32 Fingerprint Attendance System with Live Web Dashboard

June 21, 2026
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

June 14, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
Top Posts & Pages
  • ESP32 Fingerprint Attendance System with Live Web Dashboard
    ESP32 Fingerprint Attendance System with Live Web Dashboard
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
    ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
  • How to use Modbus RTU with ESP32 to read Sensor Data
    How to use Modbus RTU with ESP32 to read Sensor Data
  • IoT Based ECG Monitoring with AD8232 ECG Sensor & ESP32
    IoT Based ECG Monitoring with AD8232 ECG Sensor & ESP32
  • MAX30102 & Arduino: Heart Rate + Blood Oxygen Monitoring
    MAX30102 & Arduino: Heart Rate + Blood Oxygen Monitoring
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (205)
    • ESP32 MicroPython (7)
    • ESP32 Projects (82)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.