Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Tichomir Dunlop
Published © CC BY-NC-ND

RGB LED: Change Color Through Python

Changing colors of an RGB LED using Python. Make it go red, green or blue. Improvise by mixing colors!

IntermediateFull instructions provided30 minutes12,756
RGB LED: Change Color Through Python

Things used in this project

Hardware components

Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Raspberry Pi 1 Model B+
Raspberry Pi 1 Model B+
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×4
RGB Diffused Common Cathode
RGB Diffused Common Cathode
During construction, I had issues with this. The green part of the cathode was not working, but the rest of the program was. I tired doing the same with a smaller RGB Diffused Common Cathode, but it seemed that it had too much electrical input. (At least it did not explode, as with two of my RG's!)
×1
Resistor 475 ohm
Resistor 475 ohm
I use 470 ohm, but they were not on the component list. 475 ohm should do.
×3

Software apps and online services

Putty
WinSCP

Story

Read more

Schematics

Fritzing Schematic (.jpg file)

Fritzing Schematic of Breadboard

Fritzing Schematic (.fzz file)

Fritzing Schematic of Breadboard setup.

Code

Python Script

Python
Critical script for this project, will not work without. Note: Press Ctrl+C to clean up, raw_input() will NOT work in Python 3. Please run in Python 2.
import RPi.GPIO as GPIO
red_pin = 18
green_pin = 23
blue_pin = 24
GPIO.setup(red_pin, GPIO.OUT)
GPIO.setup(green_pin, GPIO.OUT)
GPIO.setup(blue_pin, GPIO.OUT)
GPIO.output(red_pin, False)
GPIO.output(green_pin, False)
GPIO.output(blue_pin, False)
try:         
    while True:
        UserInput = raw_input()
	UserInput = str(UserInput)
        if UserInput == "red":
            GPIO.output(red_pin, True)
            GPIO.output(green_pin, False)
            GPIO.output(blue_pin, False)
        elif UserInput == "green":
            GPIO.output(red_pin, False)
            GPIO.output(green_pin, True)
            GPIO.output(blue_pin, False)
        elif UserInput == "blue":
            GPIO.output(red_pin, False)
            GPIO.output(green_pin, False)
            GPIO.output(blue_pin, True)
	else:
		print("Only red, green, and blue are valid colors.")
finally:  
    print("Cleaning up.")
    GPIO.cleanup()

Credits

Tichomir Dunlop
5 projects • 2 followers
Hello! I am an beginner on the Raspberry Pi and have a model 3 and B+. I know some Python Programming Language as well.
Contact

Comments

Please log in or sign up to comment.