A simple Raspberry Pi project to change the color of an RGB LED by pressing a button. This project is written in Python and uses the gpiozero library.
- Press a button to set a random color on an RGB LED.
- Uses a common-anode RGB LED.
- Simple and clean code thanks to the
gpiozerolibrary.
- Raspberry Pi (any model with GPIO pins)
- A common-anode RGB LED
- A push button
- Resistors for the LED (e.g., 3 x 220Ω)
- A breadboard and jumper wires
- Connect the common anode pin of the LED to a 3.3V pin on the Raspberry Pi.
- Connect the Red, Green, and Blue cathode pins of the LED to the following GPIO pins through a resistor for each:
- Red: GPIO 17
- Green: GPIO 27
- Blue: GPIO 22
- Connect one leg of the push button to GPIO 26.
- Connect the other leg of the push button to a Ground (GND) pin.
-
Install
gpiozero: Ifgpiozerois not already installed on your Raspberry Pi OS, you can install it withpip:pip install gpiozero
Or using
apt:sudo apt update sudo apt install python3-gpiozero
-
Download the code: Make sure you have the
main.pyfile on your Raspberry Pi. -
Run the script: Execute the script from your terminal:
python3 main.py
-
Usage:
- The program will start, and the LED will be off.
- Press the button to change the LED to a new random color.
- Press
Ctrl+Cin the terminal to stop the program.