ADMFactory
Tutorials from a developer perspective

RGB LED on Raspberry Pi using Python

by ADM

 

 


In this article will make RGB LED to change colors using Raspberry Pi and Python. The LED will change 8 different colors.

The colors used are:

Color name Hex Value Color
Red 0xFF0000
Green 0x00FF00
Blue 0x0000FF
Yellow 0xFFFF00
Cyan 0x00FFFF
Pink 0xFF00FF
White 0xFFFFFF
Violet 0x9400D3

RGB LED

The LED is the abbreviation of light emitting diode. The LED has two electrodes, a positive electrode and a negative electrode. It lights up only when a forward current passes. The RGB LEDs consist of three LEDs in different colors: red, green and blue. These three colored LEDs are capable of producing any color. Tri-color LEDs with red, green, and blue emitters, in general use a four-wire connection with one common lead (anode or cathode).

The symbol for common anode RGB LED:

the symbol for common cathode RGB LED:

For this tutorial will going to use a common anode RGB LED.

Components

You will need the following components:

Note: you can easily find all components on Amazon or Ebay (in my case, I bought a "Raspberry Pi starter kit" containing a lot of components).

Schematics

To do the schematics I used Fritzing software. The longest pin is the common anode of the three LEDs. The pin is connected to the +3.3V pin of the Raspberry Pi, and the rest pins are connected to pin 11, pin 12, and pin 13 of Raspberry Pi with a current limiting resistor between (220Ω). Using these pins, we can control the color of the RGB LED by 3-channel PWM signal. If you need more details on PWM, please check Breathing Light LED on Raspberry Pi using Python tutorial.

And the physical visualization of the circuit.

Hardware

Now it's time to connect everything according to the diagram presented in the section above.

Software

Key functions

Note: all the code will be written directly on Raspberry PI console using vim editor. I've chosen this method because the example is simple and doesn't require a long time to edit. A better approach is to edit the source code locally, on your PC, using your preferable editor and then upload the code to Raspberry PI.

Login to Raspberry PI using ssh command from Linux and MacOS or using putty from windows, then open the vim editor using the following command:

$ sudo vim rgbLed.py

Copy/paste the following code.

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

colors = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0x00FFFF, 0xFF00FF, 0xFFFFFF, 0x9400D3]
pins = {'pin_R':11, 'pin_G':12, 'pin_B':13}  # pins is a dict

GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
for i in pins:
        GPIO.setup(pins[i], GPIO.OUT)   # Set pins' mode is output
        GPIO.output(pins[i], GPIO.HIGH) # Set pins to high(+3.3V) to off led

p_R = GPIO.PWM(pins['pin_R'], 2000)  # set Frequece to 2KHz
p_G = GPIO.PWM(pins['pin_G'], 2000)
p_B = GPIO.PWM(pins['pin_B'], 2000)

p_R.start(0)      # Initial duty Cycle = 0(leds off)
p_G.start(0)
p_B.start(0)

def map(x, in_min, in_max, out_min, out_max):
        return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

def setColor(col):   # For example : col = 0x112233
        R_val = (col & 0x110000) >> 16
        G_val = (col & 0x001100) >> 8
        B_val = (col & 0x000011) >> 0

        R_val = map(R_val, 0, 255, 0, 100)
        G_val = map(G_val, 0, 255, 0, 100)
        B_val = map(B_val, 0, 255, 0, 100)

        p_R.ChangeDutyCycle(100-R_val)     # Change duty cycle
        p_G.ChangeDutyCycle(100-G_val)
        p_B.ChangeDutyCycle(100-B_val)

try:
        while True:
                for col in colors:
                        setColor(col)
                        time.sleep(1.0)
except KeyboardInterrupt:
        p_R.stop()
        p_G.stop()
        p_B.stop()
        for i in pins:
                GPIO.output(pins[i], GPIO.HIGH)    # Turn off all leds
        GPIO.cleanup()

Test

To run the application execute the following command:

$ sudo python rgbLed.py

Now you should see the RGB LED flashing colors, based on the list provided.