How to brighten or dim an LED with a Raspberry Pi Pico using Pulse Width Modulation

One can use Pulse Width Modulation (PWM) with a Raspberry Pi Pico to brighten and dim an LED. Here’s how…

This blog takes a look at how one can brighten and dim an LED with a Raspberry Pi Pico and Pulse Width Modulation

How to brighten or dim an LED with a Raspberry Pi Pico using Pulse Width Modulation

Mar 04, 2021    By Team YoungWonks *

How can one brighten or dim an LED with the Raspberry Pi Pico using Pulse Width Modulation (PWM)? With the Raspberry Pi Pico now available in the market, this is a rather relevant query now. In this blog, we shall take a look at writing a program with the Raspberry Pi Pico and using PWM to increase and reduce the brightness of an LED. 


What is Raspberry Pi Pico? 

Raspberry Pi refers to a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation in association with Broadcom. The boards have been made keeping in mind the promotion of teaching basic computer science to kids. The latest offering by Raspberry Pi is the Raspberry Pi Pico, a new flexible IoT board. Essentially, it is a microcontroller board built on silicon and designed at the Raspberry Pi Foundation. 

Priced at merely $4, Raspberry Pi Pico is smaller than the average Pi and indicates how the foundation is now looking to branch out into microcontrollers and custom silicon.


Raspberry Pi Pico pinout  

In order to use it well, it is recommended to get well acquainted with the Raspberry Pi Pico pinout. The diagram below explains this. 

raspberry pi pico gpio pinout diagram  

Raspberry Pi Pico projects 

As a microcontroller, the Raspberry Pi Pico can be used in many projects, be it Internet of Things (IoT), Adafruit Neopixel projects, data logging, small to medium scale robotics, projects needing interfacing with cameras, analog sensing (using environment sensors) and more.  


Brightening and dimming an LED with a Raspberry Pi Pico using PWM

Now we are going to learn how to brighten or dim an LED using Pulse Width Modulation (PWM). PWM, also known as pulse-duration modulation (PDM), essentially is a method of reducing the average power delivered by an electrical signal, by effectively breaking it down into discrete parts.


To do this, we will need the following - a Raspberry Pi Pico on a breadboard, an LED, a resistor and a wire. 
1 raspberrypi pico breadboard wires led resistor


Take a look at this diagram to help you build a circuit. 
2 raspberrypi pico diagram circuit  


Use this wire diagram to help you verify and complete the circuit. 
3 raspberrypi pico wire diagram


Now connect the LED to the breadboard. 
4 raspberrypi pico connection led breadboard


Next connect the negative end of the LED to ground through a resistor. 
5 raspberrypi pico connection negative led resistor ground


And connect the positive end of the anode to a GPIO configured as an output. 
6 raspberrypi pico positive gpio anode


Moving on to the code, we begin by importing the dependencies. Type import utime for some delays. 
7 raspberrypi pico import utime dependencies  


Also type import Pin and PWM from the machine module. 
8 raspberrypi pico import pin PMW


Next we create a pin object.
9 raspberrypi pico create pin object


Then apply a PWM on it. 
10 raspberrypi pico apply PMW


Set the PWM frequency to 1000. 
11 raspberrypi pico PMW frequency 1000


Then create variables for the duty cycle and the direction. 
12 raspberrypi pico variables duty cycle direction


Next create a large loop that repeats many times. 
13 raspberrypi pico loop


In this loop, we shall alternate between increasing or decreasing the duty cycle. So we increase the duty cycle from zero to 255 after which we change the direction and decrease it to zero. 
14 raspberrypi pico loop duty cycle


We also use the squared value of the duty cycle to change the PWM so that we can see the brightness go up quickly and decrease slowly.
15 raspberrypi pico brightness pmw


After this we use a time delay to be able to see what is going on. 
16 raspberrypi pico time delay


You should now be able to see the LED’s brightness increase and decrease. 
17 raspberrypi pico brightness increase


18 raspberrypi pico brightness decrease


You can now put this to use in many of your maker projects. 


Watch the video below to take a quick look at how one can work with the newly launched Raspberry Pi Pico and use PWM to brighten and dim an LED: 

how to brighten or dim an led with a raspberry pi pico using pwm


Enhance Your Learning with YoungWonks

If you're intrigued by the possibilities of controlling LEDs with a Raspberry Pi Pico and want to deepen your understanding of electronics and programming, YoungWonks offers a wide array of resources to support your learning. Our Coding Classes for Kids are designed to nurture young minds, fostering a healthy curiosity towards technology and its myriad applications. For those specifically interested in mastering Python, our Python Coding Classes for Kids provide a solid foundation, equipping students with the skills to tackle various programming challenges. Additionally, for learners keen on exploring more about hardware programming, our Raspberry Pi, Arduino and Game Development Coding Classes offer a comprehensive curriculum that bridges theory with hands-on practice, allowing students to bring their imaginative ideas to life.

*Contributors: Written by Vidya Prabhu; Lead image by: Leonel Cruz

This blog is presented to you by YoungWonks. The leading coding program for kids and teens.

YoungWonks offers instructor led one-on-one online classes and in-person classes with 4:1 student teacher ratio.

Sign up for a free trial class by filling out the form below:

By clicking the "Submit" button above, you agree to the privacy policy
Share on Facebook Share on Facebook Share on Twitter Share on Twitter
Schedule a free trial class help