RGB LED

RGB LED

If you know a little about light and art knowledge, you can understand how to use RGB three colors: Red, Green, and Blue colors to display almost every colors. (In art, it may be RYB: Red, Yellow, and Blue colors, but the principle are similar.) In some professional drawing software, there is an RGB option when we choose colors. Therefore, we can use the PWM technology to control the LED’s brightness of 3 different colors to display all kinds of color as we like.

First, let’s learn about the principle of the RGB LED. An RGB LED is composed of three LEDs with different colors (red, green, and blue). There are two kinds of RGB LED: one comes with three independent LEDs and six pins (every LED has two pins.); the other is a 4-Pin one, either all cathodes or anodes of the three LEDs in the RGB are connected for common use. Thus the latter kind can be further categorized into two types: common cathode and common anode RGB LED.

Except for cathode or anode for common use, these two kinds of RGB LED are the same. Then which one to use? Well, it depends on your circuit design or personal preference. In this kit, a common cathode one is provided, and its pin figure and schematic diagram are shown as below:

How to tell the pins apart?

Method 1: The RGB LED has four pins, the longest of which is GND. Then by the GND’s two sides, the only pin on one side is the Red; on the other side, the one closer to GND is Green, and the other is Blue.

Method 2: Touch the plastic shell of the RGB, and you can find a section. The pin closest to the section surface is taken as the first pin, and you can mark the pins in order from the first: red, GND, green, and blue.

Method 3: Use a diode-check or the conductivity test function of a multimeter, or just an AA battery. Connect the longest pin (GND) to the cathode, and try connecting the anode to other pins separately. Then you will know their color.

The forward voltage drops of the three pins to the GND are 1.8 (red), 2.5 (blue), and 2.3 (green).

Connect each of the three pins to a current-limiting resistor with a same resistance and provide a same voltage to each, and you can find that the brightness rank will be like: Red > Green > Blue. Thus we need to provide three resistors with different resistances.

Connect the LED’s GND to the Raspberry Pi’s, and connect other pins to three resistors and GPIO ports. Next step is completing the code. One step before finishing!

Save a copy of the code in the last chapter and name as rgb_led.py.

For screen users, you can use the Python IDLE. Click File -> Save as in the upper left corner, or just press Ctrl + Shift + S.

For remote login, you can use the cp command: cp breath_led.py rgb_led.py.

Since we connect the LED’s anode to the I/O port here, certainly the LED will be on when the pin is high level, and will be off if it’s low. Now it is logical. So we can just change 100-value back to value in set_led_value(), and add the pin18 and pin27 in the code for green and blue:

import RPi.GPIO as GPIO

import time

r_pin = 17 # Connect R to 17

g_pin = 18 # Connect G to 18

b_pin = 27 # Connect B to 27

freq = 100

GPIO.setmode(GPIO.BCM)

GPIO.setup(r_pin, GPIO.OUT) # Set Pins to output

GPIO.setup(g_pin, GPIO.OUT)

GPIO.setup(b_pin, GPIO.OUT)

r_p = GPIO.PWM(r_pin, freq) # Set up PWM

g_p = GPIO.PWM(g_pin, freq)

b_p = GPIO.PWM(b_pin, freq)

r_p.start(0) # Start PWM with value 0

g_p.start(0)

b_p.start(0)

def set_color(r_value, g_value, b_value): # Add 3 value to control color

r_p.ChangeDutyCycle(r_value)

g_p.ChangeDutyCycle(g_value)

b_p.ChangeDutyCycle(b_value)

print "SunFounder Blue!" # Print a message

set_color(0, 60, 80) # Set color to (0, 60, 80)

time.sleep(5) # sleep 5 second before done

In this code, we change set_led_value to set_color, and use three input parameters here, representing the brightness of R, G, and B LEDs. In the following line, print means to print the content behind it on the Python Shell. The Shell will run it and show the content.

In some drawing software, you may find the RGB value ranges from 0 to 255, or the value is represented by letters like FF, but the value in the code here is just 0 to 100.

It’s easy to solve this. We can map 0~100 to 0~255 via algorithm. In this way, though the precision may be reduced, you can at least display the desired color by the RGB value.

Write such a function:

def map_value(x, in_min, in_max, out_min, out_max):

return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

This function is a simple mathematic expression, which you do not need to dig into if it's hard to understand. Five parameters are involved: the first parameter is the value to be converted, the second and third, the original range, and the fourth and fifth, the mapped range. For example, to input an r_value within 0-255 and make the output one within 0-100 to ChangeDutyCycle, make the code: map_value(r_value,0,255,0,100).

And we need to modify the set_color function:

import RPi.GPIO as GPIO

import time

r_pin = 17

g_pin = 18

b_pin = 27

freq = 100

GPIO.setmode(GPIO.BCM)

GPIO.setup(r_pin, GPIO.OUT)

GPIO.setup(g_pin, GPIO.OUT)

GPIO.setup(b_pin, GPIO.OUT)

r_p = GPIO.PWM(r_pin, freq)

g_p = GPIO.PWM(g_pin, freq)

b_p = GPIO.PWM(b_pin, freq)

r_p.start(0)

g_p.start(0)

b_p.start(0)

# Add a map value function

def map_value(x, in_min, in_max, out_min, out_max):

return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

# map the value before output to ChangeDutyCycle

def set_color(r_value, g_value, b_value):

r_p.ChangeDutyCycle(map_value(r_value,0,255,0,100))

g_p.ChangeDutyCycle(map_value(g_value,0,255,0,100))

b_p.ChangeDutyCycle(map_value(b_value,0,255,0,100))

print "Orange!" # Print “Orange”

set_color(255, 100, 0) # Set color to 0~255 mode

time.sleep(5)

Save and run the code. Now, you can use any value within 0-255. What about the RGB value like 0xFF28AB? In fact, it's composed of three hexadecimal values. 0x marks a hexadecimal; the rest can be divided into three parts: FF, 28, and AB, representing the R, G, and B value respectively. It can be calculated by binary. Write a function to split them:

def rgb_value(value):

r_value = value >> 16

g_value = value >> 8 & 0xFF

b_Value = value & 0xFF

set_color(r_value, g_value, b_value)

Above are some binary operators. You can also use string to do so for easy understanding.

def rgb_value(value):

value = '%6X' % value

value = value.replace(" ", "0")

r_value = int(value[0:2], 16)

g_value = int(value[2:4], 16)

b_value = int(value[4:6], 16)

set_color(r_value, g_value, b_value)

The string will convert the input value to a 6-bit hexadecimal first. %X means converting the value to a hexadecimal, and "6" in the middle means 6-bit. If the first bit is 0 after converting, it will be left as a space in string, so add a replace line in the code below to change the space back to 0. Next, separate the 6 bits into three values equally, and input the three 2-bit numeral 0 to set_color.

Therefore the whole code should be like this:

import RPi.GPIO as GPIO

import time

r_pin = 17

g_pin = 18

b_pin = 27

freq = 100

GPIO.setmode(GPIO.BCM)

GPIO.setup(r_pin, GPIO.OUT)

GPIO.setup(g_pin, GPIO.OUT)

GPIO.setup(b_pin, GPIO.OUT)

r_p = GPIO.PWM(r_pin, freq)

g_p = GPIO.PWM(g_pin, freq)

b_p = GPIO.PWM(b_pin, freq)

r_p.start(0)

g_p.start(0)

b_p.start(0)

# Add a map value function

def map_value(x, in_min, in_max, out_min, out_max):

return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

# map the value before output to ChangeDutyCycle

def set_color(r_value, g_value, b_value):

r_p.ChangeDutyCycle(map_value(r_value,0,255,0,100))

g_p.ChangeDutyCycle(map_value(g_value,0,255,0,100))

b_p.ChangeDutyCycle(map_value(b_value,0,255,0,100))

def rgb_value(value):

value = '%6X' % value

value = value.replace(" ", "0")

r_value = int(value[0:2], 16)

g_value = int(value[2:4], 16)

b_value = int(value[4:6], 16)

set_color(r_value, g_value, b_value)

rgb_value(0xFFab84)

time.sleep(5)

Let’s search some color numbers online, and put the values in the code to try. The color may not be exactly the same as shown in the color card. Or input some values in random to see what colors it will display. Remember to add 0x before the values, or else it would be just 6-bit; only use a character within 0-9 and a-F. For example, the value above is a random one we use.

Well, we learned so much about an LED which is all about output. It's time to move on to the input part.

        // Your code here
    
Prime Robotics
Prime Robotics is a startup which focus on supplying right products to hobbyist, electronics enthusiast, students to help them in their Innovation.