Home Code RGB Example

RGB Example

by shedboy71

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

rgb breakout

rgb breakout

Here is a schematic that shows how to connect this to your Raspberry Pi B

Pi and RGB Led schematic

Pi and RGB Led schematic

Code

Written in python and requires the RPi.GPIO library to be installed

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)

Save as rgb.py and run with the following sudo python rgb.py

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More