You want to control the color of an RGB LED.
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) |
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 |