Menu Close

RGB LED with Raspberry Pi

This tutorial of Robo India explains how to use RGB LED with Raspberry Pi. You can set any color to RGB LED and can transform from one color to another with different speed.

1. Introduction:

An RGB LED is a combination of 3 LEDs RED, Green and Blue. These three colors Red, green and blue can make any color. By varying supplied voltage to RGB LEDs different colors are formed.

An RGB LED has 4 pin interfaces. 3 Pins are for Red, Blue and Green. There is a common pins for all three LEDs

An RGB LED can be two types-

  1. Common Anode:-Anode (+) pin is common.
  2. Common Cathode:- (Cathode-/GND) pin is common.

1.1 Common Anode Type

1.2 Common Cathode Type

1.3 Hardware required

S.No.ItemQuantity
1 Raspberry Pi 1
2 Pi Wedge B + 1
3 Breadboard 1
4 RGB LED 1
5 Resistor 1k 3
6 Jumper Male to male 4

2. Building Circuit

Schematic of circuit

Layout of circuit

3. Programming:

Once the circuit part is done, Raspberry Pi is needed to be programmed. Here is the python code to run this circuit.

You may download this code (Python code) from here.

import time, sys
import RPi.GPIO as GPIO

redPin = 18   		#Set to appropriate GPIO
greenPin = 22	 #Should be set in the 
bluePin = 37  	#GPIO.BOARD format

def blink(pin):
    GPIO.setmode(GPIO.BOARD)
    
    GPIO.setup(pin, GPIO.OUT)
    GPIO.output(pin, GPIO.HIGH)
    
def turnOff(pin):
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(pin, GPIO.OUT)
    GPIO.output(pin, GPIO.LOW)
    
def redOn():
    blink(redPin)

def redOff():
    turnOff(redPin)

def greenOn():
    blink(greenPin)

def greenOff():
    turnOff(greenPin)

def blueOn():
    blink(bluePin)

def blueOff():
    turnOff(bluePin)

def yellowOn():
    blink(redPin)
    blink(greenPin)

def yellowOff():
    turnOff(redPin)
    turnOff(greenPin)

def cyanOn():
    blink(greenPin)
    blink(bluePin)

def cyanOff():
    turnOff(greenPin)
    turnOff(bluePin)

def magentaOn():
    blink(redPin)
    blink(bluePin)

def magentaOff():
    turnOff(redPin)
    turnOff(bluePin)

def whiteOn():
    blink(redPin)
    blink(greenPin)
    blink(bluePin)

def whiteOff():
    turnOff(redPin)
    turnOff(greenPin)
    turnOff(bluePin)
    
print("""Ensure the following GPIO connections: R-18, G-22, B-37
Colors: Red, Green, Blue, Yellow, Cyan, Magenta, and White
Use the format: color on/color off""")

def main():
    while True:
        cmd = raw_input("-->")

4. Output

Python Coding with Raspberry Pi connects your project to the real world.The easiest introduction to Python is through IDLE, a Python development environment.

Open IDLE (Python Development Environment) from the Desktop or applications menu:

Python 3 is the newest version. Write the above code and save your code on desktop or any other location. Open Command Prompt. Write cd location_of_program > Press Enter.

Then write Python 3 file_name.py > Press Enter. (.py is file extension).

After successful uploading of the code, the RGB LED generates two different colors in a time interval of 1 second. Try different set of RGB to get different colors.

5. Troubleshooting

LED is not glowing: LED may be common cathode type, pull it out and check it.

Generate new colors: Try new combination RGB values. The color will be from RGB palette.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

Leave a Reply