Skip to content

LED Brightness Control on Raspberry Pi Pico with MicroPython & PWM

blink led

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.
  • 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

Circuit Diagram

fig-Connection Diagram

📂 Code

from machine import Pin, PWM
from time import sleep_ms

# Led is connected to GPIO 2
# setting PWM frequency to 1k Hz and duty cycle to 0
led = PWM(2, freq=1000, duty_u16=0)


while True:

    # Increasing the duty cyle value from 0% to 100%
    for i in range(65536):
        led.duty_u16(i)

    sleep_ms(1000)

    # Decreasing the duty cyle value from 0% to 100%
    for i in range(65535, -1, -1):
        led.duty_u16(i)

    sleep_ms(1000)

Code Explanation

👉 Imports

from machine import Pin, PWM
from time import sleep_ms
  • Importing PWM class from machine module to generate PWM signal in specified GPIO.
  • time module for creating delay.

👉 PWM pin configuration.

led = PWM(2, freq=1000, duty_u16=0)
  • 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

while True:
  • Continuous loop for varying led brightness achieved using while True:

👉 Increasing the LED brightness

    # Increasing the duty cyle value from 0% to 100%
    for i in range(65536):
        led.duty_u16(i)
  • 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

    # Decreasing the duty cyle value from 100% to 0%
    for i in range(65535, -1, -1):
        led.duty_u16(i)
  • 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

Modules / Libraries Used

  • machine

    • machine module contains specific attributes and methods related to hardware on a particular board. Here class PWM is imported to configure the GPIO pins as Pulse Width Modulator.
    • More Details
  • time

    • time module provides functions related to date & time, measuring time intervals and generating delays.
    • More Details