Raspberry Pi Pico - Fade LED

This guide shows you how to make a Raspberry Pi Pico control an LED to slowly brighten and dim. Here's what we will cover:

Raspberry Pi Pico Fade LED

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LED

Pinout

LED has two metal legs.

  • Cathode(-) pin: Connect it to GND (0V).
  • Anode(+) pin: Use it to control the LED's state.
LED Pinout

How It Works

After attaching the negative side, known as the cathode, to the ground (GND):

  • When you connect the ground (GND) to the anode (positive side) of the LED, the LED turns off.
  • When you connect the power supply (VCC) to the anode of the LED, the LED turns on.
  • By sending a Pulse Width Modulation (PWM) signal to the anode of the LED, you can adjust its brightness. The PWM value ranges from 0 to 255. The LED gets brighter with a higher PWM value and dimmer with a lower one.
    • If the PWM value is 0, the LED turns off, similar to connecting it to GND.
    • If the PWM value is 255, the LED is fully on, similar to connecting it to VCC.
    How LED Works

    ※ NOTE THAT:

    For most LEDs, you should attach a resistor between the positive terminal (anode) and the power source (VCC). The value of the resistor changes based on the LED's specifications.

    Raspberry Pi Pico - fade LED

    You can dim an LED using certain pins on the Raspberry Pi Pico that generate a PWM signal. First, attach the positive pin (+) of the LED to a pin on the Raspberry Pi Pico. Next, connect the negative pin (-) of the LED to the ground (GND). Then, configure the selected Raspberry Pi Pico pin to output a PWM signal.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico LED

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-fade-led """ from machine import Pin, PWM from time import sleep # Define pin LED_PIN = 13 # The Raspberry Pi Pico pin GP13 connected to the LED # Initialize PWM on the LED pin led = PWM(Pin(LED_PIN)) led.freq(1000) # Set the PWM frequency to 1kHz # Variables to control the LED brightness brightness = 0 # How bright the LED is fade_step = 5 # How many points to fade the LED by # Main loop while True: # Set the brightness of the LED led.duty_u16(int(brightness * 65535 / 255)) # Convert 0-255 range to 0-65535 # Change the brightness for next time through the loop brightness = brightness + fade_step # Reverse the direction of the fading at the ends of the fade if brightness <= 0 or brightness >= 255: fade_step = -fade_step # Wait for 30 milliseconds to see the dimming effect sleep(0.03)

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the LED according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Check out the LED state.

Code Explanation

The explanation is in the comments above in the Raspberry Pi Pico code.

※ NOTE THAT:

In the example, we used a function named sleep() to gradually change the light's brightness. But, this function causes the light adjustment to be less smooth and halts other parts of the program. Next, we will learn to fade the light smoothly while keeping the rest of the program running using LED library provided by DIYables.

How to fade in/out LED

With the LED library created by DIYables, fading LED in/out is a piece of cake, you can set the brightness range, and fade time.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-fade-led """ """ - It is created by DIYables to work with DIYables products, but also work with products from other brands. - Please consider purchasing products from [DIYables Store on Amazon](https://amazon.com/diyables) from to support our work. """ from DIYables_MicroPython_LED import LED, CTRL_ANODE, LED_IDLE led = LED(13, CTRL_ANODE) # create an LED object that attaches to pin 13 is_faded_in = False while True: led.loop() # MUST call the led.loop() function in loop() if led.get_state() == LED_IDLE: if not is_faded_in: print("FADING IN") led.fade(0, 255, 3000) # fade in from 0 to 255 in 3000ms, fade immediately # led.fade(0, 255, 3000, 1000) # fade in from 0 to 255 in 3000ms, fade after 1 second is_faded_in = True else: print("FADING OUT") led.fade(255, 0, 3000) # fade out from 255 to 0 in 3000ms, fade immediately # led.fade(255, 0, 3000, 1000) # fade out from 255 to 0 in 3000ms, fade after 1 second is_faded_in = False

Detailed Instructions

  • On Thonny IDE, Navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-LED”, then find the LED library created by DIYables.
  • Click on DIYables-MicroPython-LED, then click Install button to install LED library.
Raspberry Pi Pico LED library
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to main.py on your Raspberry Pi Pico
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Check out the LED state.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!