RGB LED Cycle Using Raspberry Pi Pico (MicroPython)

 

🔴🟢🔵 Button-Controlled RGB LED Cycle Using Raspberry Pi Pico (MicroPython)

Objective

  • To demonstrate how to use a push button to cycle through three different LEDs (Red, Green, and Blue) connected to a Raspberry Pi Pico.

  • To learn input reading, edge detection, and RGB control in MicroPython.


Components Required

S.N.ComponentQuantity
1Raspberry Pi Pico1
2Red LED1
3Green LED1
4Blue LED1
5Push Button1
6220Ω Resistors3 (optional)
7Breadboard + Jumper WiresAs needed
8Micro USB Cable1


Circuit Description

  • Red LED → GPIO 2

  • Green LED → GPIO 1

  • Blue LED → GPIO 0

  • All cathodes of the LEDs go to GND through 220Ω resistors.

  • Push Button → GPIO 15, with internal pull-down resistor enabled using Pin.PULL_DOWN.

:::::circuit diagram:::::

                                                               
::::Video Link::::





Working Principle

The MicroPython script continuously monitors the button state. When a button press (rising edge) is detected:

  1. A counter is incremented.

  2. Based on the value of the counter (1 to 3), it lights up one of the LEDs:

    • 1 = Red

    • 2 = Green

    • 3 = Blue

  3. After 3, the counter resets to 1, creating a looped LED cycle.

  4. A debounce delay is used to avoid repeated triggering from mechanical button noise.


Code: MicroPython

python
from machine import Pin from time import sleep # Define input button and output LEDs button = Pin(15, Pin.IN, Pin.PULL_DOWN) led0 = Pin(0, Pin.OUT) # Blue led1 = Pin(1, Pin.OUT) # Green led2 = Pin(2, Pin.OUT) # Red count = 0 last_button_state = 0 print("Welcome to Kailash Raspberry Pi Pico Series") # Function to turn off all LEDs def turn_off_leds(): led0.off() led1.off() led2.off() while True: current_state = button.value() # Detect rising edge of button press if current_state == 1 and last_button_state == 0: count += 1 if count > 3: count = 1 # reset to 1 to cycle again turn_off_leds() # turn off all LEDs before lighting the next if count == 1: print("Red") led2.on() elif count == 2: print("Green") led1.on() elif count == 3: print("Blue") led0.on() sleep(0.3) # debounce delay last_button_state = current_state sleep(0.01) # short delay to reduce CPU usage

Sample Output

mathematical
Welcome to Kailash Raspberry Pi Pico Series Red Green Blue Red ...

Applications

  • RGB LED control

  • Simple color-coded feedback system

  • Learn edge detection and state transitions in embedded systems


Advantages

  • Teaches button debouncing and state machine logic

  • Scalable to add more LEDs or modes

  • Beginner-friendly GPIO and logic structure


Limitations

  • No PWM (only full ON/OFF LEDs)

  • No long-press detection

  • Button bounces handled by fixed delay (not interrupt-based)


References

  • assistance by ChatGpt

  • Compiler is thonny application

  • Practical implementation tested and verified by Kailash


Conclusion

This simple project helps you grasp GPIO input-output interactions and logic flow on Raspberry Pi Pico. Use of RGB led .


Comments

Popular posts from this blog

Astable circuit using 555 timer ic

LDR base solar light tracking system using Transistor DIY project

Monostable circuit using 555 timer ic