LED Brightness Control on Raspberry Pi Pico with MicroPython & PWM

Table of Contents
Abstract
In this article, a comprehensive step-by-step guide to control the brightness of an LED using PWM (Pulse Width Modulation) with Raspberry Pi Pico board using MicroPython. Raspberry Pi Pico has 4 different variants (Pico, Pico 2, Pico W, Pico 2W) supporting micro-python. This articles lays the foundation for more advanced embedded system and IoT projects.
Pre-Request
- OS : Windows / Linux / Mac / Chrome
- Thonny IDE.
- MicroPython firmware in Raspberry Pi Pico / Pico 2 / Pico W / Pico 2W.
- For step by step procedure click here .
- Interfacing external LED to Raspberry Pi Pico. Learn More
Hardware Required
- Raspberry Pi Pico / Pico 2 / Pico W / Pico 2W.
- LED.
- Resistors.
- BreadBoard.
- Micro USB Cable.
- Connecting wires.
- 5V DC power supply (Optional)
| Components | Purchase Link |
|---|---|
| Raspberry Pi Pico | link |
| Raspberry Pi Pico 2 | link |
| Raspberry Pi Pico W | link |
| Raspberry Pi Pico 2W | link |
| LED | link |
| BreadBoard | large : small |
| Connecting Wires | link |
| Micro USB Cable | link |
| 5V DC Adaptor | link |
Don't own a hardware
No worries,
Still you can learn using simulation.
check out simulation part .
Connection Table
| Particular | GPIO | Remarks |
|---|---|---|
| LED | 2 | LED Anode to GPIO 2 (Pin 4) : High Logic |

fig-Connection Diagram
Code
Code Explanation
Imports
- Importing
PWMclass from machine module to generate PWM signal in specified GPIO. timemodule for creating delay.
PWM pin configuration.
PWM( <Pin_Number> , freq=<value>, duty_u16=<value>)- GPIO pin 2 is configured as PWM
- Frequency of PWM set to 1000 Hz (1K Hz)
- Setting the duty cycle to 0 (Turning OFF LED)
- duty_u16 value range from 0 to 65535. (16 bit PWM)
Infinite Loop
- Continuous loop for varying led brightness achieved using
while True:
Increasing the LED brightness
- duty cycle value of PWM signal can be set using
pwm_pin.duty_u16(<value>)method. range(65536)is set : duty_u16 value range from 0 to 65535 for 0% to 100%. (16 bit PWM)- In the above code, pwm duty cycle value is increased from 0 to 65535.
- This increased the brightness of LED gradually from OFF state to ON state.
Decreasing the LED brightness
range(65535, -1, -1)is set to generate sequence of number from 65535 to 0, to reduce the brightness of led from 100% to 0% or ON state to OFF state gradually.
Try It
- Change the value of frequency to 10 Hz, 100 Hz, etc and observe the output.
- Interface POT with microcontroller and try controlling the brightness of LED, depending on the position of Potentiometer.
Simulation
Not able to view the simulation
- Desktop or Laptop : Reload this page ( Ctrl+R )
- Mobile : Use Landscape Mode and reload the page
Extras
Components details
- Raspberry Pi Pico / Pico 2 : Pin Diagram
- Raspberry Pi Pico : Data Sheet
- Raspberry Pi Pico 2 : Data Sheet
- Raspberry Pi Pico W : Data Sheet
- Raspberry Pi Pico 2 W : Data Sheet
Modules / Libraries Used
-
machine
machinemodule contains specific attributes and methods related to hardware on a particular board. Here classPWMis imported to configure the GPIO pins as Pulse Width Modulator.- More Details
-
time
timemodule provides functions related to date & time, measuring time intervals and generating delays.- More Details