RGB Basic Color Changer For Raspberry Pi

I was experimenting with an RGB LED (common anode) and I came up with a simple color-changing button circuit and programming, made with the wonderful Raspberry Pi. I originally made the program wiht the Arduino Uno, but I decided to do the same thing in Python 3. Check out the pic. I used 470 Ohm resistors, 3 NPN transistos, and a pushbutton wired with a pull-up resistor. If you need more information on RGBs and transistor use check out https://electrothoughts.wordpress.com/general-rgb-and-transistor-use

The code is written in Python 3,  and it works on the B+ model. If you want to try this project with the regular Model B or A Pi, you may need to change which pins you use. See the code below! I’ll post the Arduino version of this as soon as I can!


## The RGB LED cycles through 6 common colors at the push of a button
## Materials
##   -RGB LED (common anode)
##   -3 NPN Transistors
##   -Pushbutton
##   -1K Ohm Resistor
##   -3 680 Ohm Resistors
##   -Assorted Wires
## * I would recommend viewing this in IDLE or another IDE or Drive Notepad so you can ##differentiate between comments and code *

import RPi.GPIO as GPIO, time

## It’s common to use all caps for constants; in this case the pin values are constants
## because they do not change throughout the entire program
## Because I’ll be referring back to the specific pins, I’m not making a list
## like COLOR = [21,20,16].
## I’m labeling each pin accordingly to the color
RED = 21
GREEN = 20
BLUE = 16
BUTTON1 = 12

## The standard button related variables
button1State = True
onOrOffState = False
previousB1 = True
pressed = False

## This color variable will be the current color to be turned on or off
## Its initial value is RED.
color = RED

GPIO.setup(RED, GPIO.OUT,initial = 0)
GPIO.setup(GREEN, GPIO.OUT,initial = 0)
GPIO.setup(BLUE, GPIO.OUT,initial = 0)


____while True:
________button1State = GPIO.input(BUTTON1)
________## Each time the button is pressed
________if (previousB1 != button1State and button1State == pressed):
____________## When these if statements are executed, a new color is choosen.
____________## If the color is RED, it becomes GREEN, if it’s GREEN it will be
____________## BLUE, and if it’s BLUE it will be RED.
____________if color == RED:
________________color = GREEN
________________if color == GREEN:
___________________color = BLUE
____________________color = RED
## The trick to this program is that the RGB never shuts off.
## The colors turn off and on in a cycle because a different
## color is turned off each time, so that there are always
## one or two colors on.
________onOrOffState = not onOrOffState
________GPIO.output(color, onOrOffState)
________previousB1 = button1State

except KeyboardInterrupt:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s