- Published: Saturday, 04 April 2009
- Written by Digital DIY
- Hits: 7199
Red Green Blue (RGB) LED's are a marvel to watch in action, and I have got to say, they are worth the effort to play around with. The programming side of things does take a little thought, primarily because you need to control 3 different Pulse Width Modulated (PWM) signals to control the brightness of each LED (Red Green and Blue).
RGB LED's have 4 pins, 3 of them are for a specific colour (either Red Green or Blue), and the forth is a common anode or cathode. I prefer to use the common anode devices, as I like to use the ULN2003 to drive the LED's. This way I can hook up many LED's in parallel to get some very powerful and amazing lighting effects.
The LED's I am using are Common Anode so that I can use the ULN2003 to drive many in parallel. Below is the pin-out;
The above diagram utilises the ULN2003 to drive much higher current devices then what the PIC can handle so with this design, you could put a lot of RGB LED's in parallel for some really stunning lighting effects.
The RGB LED's from have a forward voltage drop of about 2 Volts for the Red LED, and 3.5 Volts for both the Green and Blue LED's. To ensure that I am running them at full brightness (20mA for each colour), I can calculate the resistance required for each resistor by the following equation;
R = (V - Vf - Vce) / I
Where R = Resistance in ohms, V = Supply Voltage, Vf = LED Forward Bias Voltage, Vce = ULN2003 voltage drop, I = Current
To get the maximum brightness from your LED's, replace the resistors with the following (increase the current if more LED's
are used in parallel);
Red resistor = (5 - 2 - 0.9) / 0.020 = 105 ohm or greater
Green resistor = (5 - 3.5 - 0.9) / 0.020 = 30 ohm or greater
Blue resistor = (5 - 3.5 - 0.9) / 0.020 = 30 ohm or greater
Because the program is interrupt driven, I want to take as little time as possible to service the interrupt. One way is to minimise the code, another is to maximise the clock speed, so I chose a little of both.
The reason why I chose an Interrupt driven routine, is so that once the registers and interrupt is setup, you can do anything you want in the main program, and not worry about the performance of the RGB LED. In the circuit above, I use the 18F4550 with a 20Mhz Crystal Oscillator and PLL enabled (a couple of config settings) to achieve 48Mhz CPU Clock speed, handy.
The program has to incorporate the control of three different PWM signals (one for each colour). I wanted to maintain as much brightness control as possible, and decided to make the Duty Cycle editable from 0 t0 100% by simply editing a register from 0 to 255.
In the example below, I am using the 18F4550 with a 20Mhz external oscillator, and with a few PLL config settings, the CPU will operate at 48Mhz (12 million instructions per second). The Timer 2 interrupt will be serviced every 50uS, that is 20Khz. With this in mind, as each complete PWM signal takes 256 timer cycles, this means that a PWM pulse will be driven 20000 / 256 = 78.125 Hz. Anything above 25Hz will look fluent to the human eye!
This might not be the most efficient way to create three PWM signals, but it allows complete diversity in the fact that you can control the brightness of each LED colour component on the fly while doing anything else! The program also uses the random number user library to create random numbers from 0 to 255.
The 19mS delay between increments/decrements of the colours is to control the time it takes going from one extreme to the other, in this case, 19mS ensures it takes just under 5 seconds for all 256 brightness levels to change.
Download the library: RandGen