Raspberry Pi Tutorial: How to Use a RGB LED
by Ardumotive_com in Circuits > Raspberry Pi
18877 Views, 6 Favorites, 0 Comments
Raspberry Pi Tutorial: How to Use a RGB LED
An RGB LED has 4 pins, one for each color (Red, Green, Blue) and a
common cathode. It has three different color-emitting diodes that can be combined to create all sorts of color! Any color is possible depending on how bright each diode is. In this tutorial you will learn how to use an RGB LED with Raspberry Pi and create unique color combinations.
Tutorial updates and more Raspberry Pi tutorials can be found here:
http://www.ardumotive.com/how-to-use-rgb-led-en.ht...
Let's get started!
What You Will Need - Hardware
For this tutorial you will need:
And of course any Raspberry Pi board with Raspbian OS installed.
The Circuit
The connections are pretty easy, see the image above with breadboard circuit schematic.
Python Code
Download the code from here and open it with Thonny Python IDE or run it from terminal.
1 | #libraries
import RPi.GPIO as GPIO
from time import sleep
#disable warnings (optional)
GPIO.setwarnings(False)
#Select GPIO Mode
GPIO.setmode(GPIO.BCM)
#set red,green and blue pins
redPin = 12
greenPin = 19
bluePin = 13
#set pins as outputs
GPIO.setup(redPin,GPIO.OUT)
GPIO.setup(greenPin,GPIO.OUT)
GPIO.setup(bluePin,GPIO.OUT)
def turnOff():
GPIO.output(redPin,GPIO.HIGH)
GPIO.output(greenPin,GPIO.HIGH)
GPIO.output(bluePin,GPIO.HIGH)
def white():
GPIO.output(redPin,GPIO.LOW)
GPIO.output(greenPin,GPIO.LOW)
GPIO.output(bluePin,GPIO.LOW)
def red():
GPIO.output(redPin,GPIO.LOW)
GPIO.output(greenPin,GPIO.HIGH)
GPIO.output(bluePin,GPIO.HIGH)
def green():
GPIO.output(redPin,GPIO.HIGH)
GPIO.output(greenPin,GPIO.LOW)
GPIO.output(bluePin,GPIO.HIGH)
def blue():
GPIO.output(redPin,GPIO.HIGH)
GPIO.output(greenPin,GPIO.HIGH)
GPIO.output(bluePin,GPIO.LOW)
def yellow():
GPIO.output(redPin,GPIO.LOW)
GPIO.output(greenPin,GPIO.LOW)
GPIO.output(bluePin,GPIO.HIGH)
def purple():
GPIO.output(redPin,GPIO.LOW)
GPIO.output(greenPin,GPIO.HIGH)
GPIO.output(bluePin,GPIO.LOW)
def lightBlue():
GPIO.output(redPin,GPIO.HIGH)
GPIO.output(greenPin,GPIO.LOW)
GPIO.output(bluePin,GPIO.LOW)
while True:
turnOff()
sleep(1) #1second
white()
sleep(1)
red()
sleep(1)
green()
sleep(1)
blue()
sleep(1)
yellow()
sleep(1)
purple()
sleep(1)
lightBlue()
sleep(1)
|
Downloads
Well Done
You have successfully completed one more Raspberry Pi "How to" tutorial and you learned how to use RGB LED!
I hope you liked this, let me know in the comments below.
Video in Greek language