1.5k
In this example we will display the red, green and blue colours on an RGB LED
I used a breakout which came as part of a kit, it was a common anode type
Here is a schematic that shows how to connect this to your Raspberry Pi B
Code
Written in python and requires the RPi.GPIO library to be installed
[codesyntax lang=”python”]
import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) #Color blue : GPIO Pin 37 GPIO.setup(26,GPIO.OUT) #Color green : GPIO Pin 35 GPIO.setup(19,GPIO.OUT) #Color red : GPIO Pin 33 GPIO.setup(13,GPIO.OUT) while True: #all off GPIO.output(13,GPIO.HIGH) GPIO.output(19,GPIO.HIGH) GPIO.output(26,GPIO.HIGH) #RED ON GPIO.output(13,GPIO.LOW) time.sleep(1) #RED OFF GPIO.output(13,GPIO.HIGH) time.sleep(0.1) #GREEN ON GPIO.output(19,GPIO.LOW) time.sleep(1) #GREEN OFF GPIO.output(19,GPIO.HIGH) time.sleep(0.1) #BLUE ON GPIO.output(26,GPIO.LOW) time.sleep(1) #BLUE OFF GPIO.output(26,GPIO.HIGH) time.sleep(0.1)
[/codesyntax]
Save as rgb.py and run with the following sudo python rgb.py