Overview
In this guide, we will learn the Controlling of RGB LED using Raspberry Pi Pico Board with MicroPython Code. We will control the three primary colors of the RGB LED to display different brightness randomly, so as to realize the colorful light effect.
Components Required
In this guide, I used Elecrow Raspberry Pi Pico Starter Kit to test different Modules. 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 Board -1
2. RGB LED Module – 1
3. Breadboard -1
4. Jumper Wires – 4
RGB LED
The R, G, and B in RGB LED stand for red, green, and blue respectively. In theory, you can use some combination of these three colors to create any color; Through PWM voltage input can adjust the intensity of the three primary colors (red/green/blue), so as to achieve the full-color mixing effect.
Circuit Diagram & Hardware Setup
We are using RGB LED Module here for interfacing with Raspberry Pi Pico. In case you want to use an RGB LED only, then use a 220-ohm resistor with each, R, G & B Pin.
Connect the R, G & B Pin of the RGB LED to Raspberry Pi Pico GP2, GP3 & GP4 Pin respectively. Connect the GND Pin of RGB LED to Pico GND Pin.
MicroPython Code/Program
The following code will simulate the each LED and using the PWM function, the brightness of each LED is randomly generated.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from machine import Pin,PWM import utime import random Led_R = PWM(Pin(2)) Led_G = PWM(Pin(3)) Led_B = PWM(Pin(4)) # Define the frequency Led_R.freq(2000) Led_G.freq(2000) Led_B.freq(2000) if __name__ == "__main__": while True: # range of random numbers R=random.randint(0,65535) G=random.randint(0,65535) B=random.randint(0,65535) print(R,G,B) Led_R.duty_u16(R) Led_G.duty_u16(G) Led_B.duty_u16(B) utime.sleep_ms(100) |
Copy the above MicroPython code to Thonny Editor and run the Script.
You will be able to see random color changes in the RGB Module.










