Holooly Plus Logo

Question 9.9: You want to control the color of an RGB LED....

You want to control the color of an RGB LED.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Use PWM to control the power to each of the red, green, and blue channels of an RGB LED.

To make this recipe, you will need:

• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)
• Three 1kΩ resistors (see “Resistors and Capacitors” on page 380)
• RGB common cathode LED (“Opto-Electronics” on page 381)
• A Pi Plate (see Recipe 8.20) or Humble Pi (Recipe 8.19) to make a more permanent project (Optional)

Figure 9-10 shows how you can connect your RBG LED on a breadboard. Make sure that the LED is the correct way around; the longest lead should be the second lead from the top of the breadboard. This connection is called the common cathode, as the negative connections (cathodes) of the red, green, and blue LEDs within the LED case have all their negative sides connected together to reduce the number of pins needed in the package.

If you want to make a more permanent design, you can solder this up on a Pi Plate or Humble Pi. See Recipe 8.19 to learn how.

The upcoming program has three sliders to control the red, green, and blue channels of the LED (Figure 9-11).

Open an editor (nano or IDLE) and paste in the following code. As with all the program examples in this book, you can also download the program from the Code section of http://www.raspberrypicookbook.com, where it is called gui_sliderRGB.py.

from Tkinter import *
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
pwmRed = GPIO.PWM(18, 500)
pwmRed.start(100)
pwmGreen = GPIO.PWM(23, 500)
pwmGreen.start(100)
pwmBlue = GPIO.PWM(24, 500)
pwmBlue.start(100)
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()

Label(frame, text=’Red’).grid(row=0, column=0)
Label(frame, text=’Green’).grid(row=1, column=0)
Label(frame, text=’Blue’).grid(row=2, column=0)
scaleRed = Scale(frame, from_=0, to=100,
orient=HORIZONTAL, command=self.updateRed)
scaleRed.grid(row=0, column=1)
scaleGreen = Scale(frame, from_=0, to=100,
orient=HORIZONTAL, command=self.updateGreen)
scaleGreen.grid(row=1, column=1)
scaleBlue = Scale(frame, from_=0, to=100,
orient=HORIZONTAL, command=self.updateBlue)
scaleBlue.grid(row=2, column=1)
def updateRed(self, duty):
pwmRed.ChangeDutyCycle(float(duty))
def updateGreen(self, duty):
pwmGreen.ChangeDutyCycle(float(duty))
def updateBlue(self, duty):
pwmBlue.ChangeDutyCycle(float(duty))
root = Tk()
root.wm_title(‘RGB LED Control’)
app = App(root)
root.geometry(“200×150+0+0”)
root.mainloop()

Discussion

The code is similar in operation to the control for a single PWM channel, described in Recipe 9.8. However, in this case, you need three PWM channels and three sliders, one for each color.

The type of RGB LED used here is a common cathode. If you have the common anode type, then you can still use it, but connect the common anode to the 3.3V pin on the GPIO connector. You will then find that the slider becomes reversed, so a setting of 100 becomes off and 0 becomes full on.

When you are selecting an LED for this project, LEDs labeled diffused are best because it allows the colors to be mixed better.

See Also

If you just want to control one PWM channel, see Recipe 9.8.

For more information on using a breadboard and jumper wires with the Raspberry Pi, see Recipe 8.10.

Table A-3. Prototyping equipment
Description Suppliers
M-M jumper wires SparkFun: PRT-08431, Adafruit: 759
M-F jumper wires SparkFun: PRT-09140, Adafruit: 825
F-F jumper wires SparkFun: PRT-08430, Adafruit: 794
Half-sized breadboard SparkFun: PRT-09567 Adafruit: 64
Pi Cobbler Adafruit: 1105
Table A-4. Resistors and capacitors
270Ω 0.25W resistor Mouser: 293-270-RC
470Ω 0.25W resistor Mouser: 293-470-RC
1kΩ 0.25W resistor Mouser: 293-1k-RC
3.3kΩ 0.25W resistor Mouser: 293-3.3k-RC
4.7kΩ 0.25W resistor Mouser: 293-4.7k-RC
10 kΩ trimpot Adafruit: 356, SparkFun: COM-09806, Mouser: 652-3362F-1-103LF
Photoresistor Adafruit: 161, SparkFun: SEN-09088
220nF capacitor MCM: 31-0610, Mouser: 80-C322C224M5U5HA
Table A-7. Opto-electronics
5mm red LED SparkFun: COM-09590, Adafruit: 299
RGB common cathode LED SparkFun: COM-11120
TSOP38238 IR sensor SparkFun: SEN-10266, Adafruit: 157
9.10
9.11

Related Answered Questions

Question: 9.11

Verified Answer:

Assuming you have a 5V volt meter, you can use a P...
Question: 9.10

Verified Answer:

The way to do this is to use a technique called Ch...
Question: 9.8

Verified Answer:

Using the Tkinter user interface framework, write ...
Question: 9.7

Verified Answer:

Using the Tkinter user interface framework, write ...
Question: 9.5

Verified Answer:

Use a relay and small transistor. Figure 9-5 shows...
Question: 9.1

Verified Answer:

Connect an LED (see “Opto-Electronics” on page 381...
Question: 9.3

Verified Answer:

Use a piezo-electric buzzer connected to a GPIO pi...
Question: 9.4

Verified Answer:

These high-power LEDs use far too much current to ...
Question: 9.6

Verified Answer:

Use a PowerSwitch Tail II (see “Modules” on page 3...