RGB Example

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

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

Pi and RGB Led schematic

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

Related posts

TLV493D magnetic sensor and Raspberry Pi 4 python example

SHT40 Digital Humidity Sensor and Raspberry Pi 4 python example

LTR390 UV Light Sensor a Raspberry Pi 4 in python

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