Standalone PWM controller for RGB LEDs using 12F6xx PIC
Want to build an RGB LED controller that you can program with your own custom sequences and effects? Then read on.
The RGB LED controller has proved to be very popular project and has been the most frequently downloaded code on the site since it was made available.
Circuit Description
The circuit itself is fairly straightforward. Diode D1 provides reverse polarity protection for the board in case the power supply is connected backwards. C1/C2 and IC2 take the incoming 12 volt supply and provide a regulated 5 volt supply required by the PIC microcontroller.
The red, green and blue LEDs are arranged in three parallel strings of three LEDs. Resistors R1, 2 and 3 limit the current through the LEDs to a safe value when using a 12 volt power supply. The low side of each LED string connects to a BC547 NPN transistor which is used to switch the LEDs on and off. These transistors are in turn controlled by the PIC microcontroller which drives each of the red, green and blue channel transistors with a PWM signal to control the average brightness of the LEDs. Switch S1 is used to select different effect sequences. The firmware program running on the PIC microcontroller is the smart part of the circuit and determines what colours are generated and how they fade from one colour to the next.
The three colours of LEDs are positioned on the PCB in an irregular arrangement to improve the colour mixing effect when placed behind / inside a diffuser such as a frosted glass globe.
The controller uses (RGB) Red, Green and Blue high brightness LEDs that are pulse width modulated (PWM) to vary the intensity of each colour LED. This allows effectively any colour to be generated with rapid changing strobe effects, fast and slow colour fades as well as static colours. The data used to set and change the colours is held in an easy to edit file so if you don't like the sequences provided with it, you can modify the sequence data include file yourself and reprogram with your own sequences. (you will need a PIC programmer and some practical knowledge of microcontrollers and programming if you want to do this.)
Power Supply Requirements
The RGB LED Moodlight requires a 12 volt regulated DC power supply rated for 200mA or higher. This is important, a non-regulated 12 volt supply may actually output 14 or 15 volts and this will damage the LEDs over time unless you alter the current limiting resistors. The power supply must also output DC not AC.
To summarise then, you need a 12 volt DC regulated power supply capable of delivering at least 200mA of output current (a higher current rating is fine, but it must be 12 volts DC)
Component | Description |
R_RED | PK 100 120R 0.25W CF RESISTOR (RC) |
R_GREEN. R_BLUE | PACK 100 68R 0.25W CF RESISTOR (RC) |
C1 | 100N 2.5MM X7R DIELEC.CERAMIC (RC) |
C2 | 5MM MICROMIN. 47UF 16V ELECTROLYTIC (RC) |
C3 | 220N 5MM Y5V DIELECT.CERAMIC (RC) |
socket for U1 | 8 PIN 0.3IN DIL SOCKET (RC) |
U1 | PIC12F629-I/P (RC) ( Needs programming ) |
U2 | DA78L05 V REG +5V 100MA TO-92 TRU (RC) |
D1 | 1N4148 SIGNAL DIODE 75V 150MA (TRU) RC |
Q1,2,3 | 2N7000 N CHANNEL MOSFET (RC) |
S1 | TACTILE SWITCH 6X6MM HEIGHT 7MM (RC) |
Red LED | LED 5MM HB RED 20000MCD (RC) |
Green LED | LED 5MM HB GREEN 30000MCD (RC) |
Blue LED | LED 5MM HB BLUE 12000MCD (RC) |
Not shown on the schematic but you may also want to buy | |
DC Power connector | NICKEL 2.1MM DC POWER SOCKET (RC) |
Power supply | 5W SWITCH MODE PLUGTOP PSU 12V 400MA RC |
PIC Programmer | PICKIT2 STARTER KIT (RC) |
Operation
When the PIC is first powered on after programming, it should start running the first RGB sequence found. If you're using the original sequences supplied with the code here it will run a sequence of fading red thru blue thru green repeating.
User control of the RGB Driver is done using the S1 switch which performs multiple functions as described in the following section.
Single press to Hold / Run current sequence
You can press S1 at any time to stop the sequence running and hold the colour being displayed at that moment in time. Pressing S1 again will start the sequence running.
If the controller is powered off while in the hold state when it is next powered on it will remain in the hold state displaying the same colour.
Double press to Select Next Sequence
(press S1 twice less than 0.5 second apart; think 'double-click' computer mouse button)
Step through all available sequences. When the last sequence has been reached it will go back to the first available sequence. Each time the S1 switch is 'double clicked' the RGB LED PWM values are set back to 0 (LEDs off) and the new sequence will start running.
When stepping through the sequences it always starts each new sequence in the Run state, even if it was previously in a Hold state
( the last sequences is indicated by 3 short blinks of the blue and green LEDs repeating)
Press and hold to enter / exit sleep state
Press and hold S1 switch for about 1.2 seconds to put the PIC into sleep mode. Once in sleep mode, press the S1 switch for about 2 seconds then release it to wake the PIC from sleep. If the S1 button isn't held for two seconds the PIC returns to sleep
About 10 seconds after the S1 switch is last pressed the currently selected sequence number, RGB colour values and Hold state are saved to non-volatile EEPROM memory. When the RGB LED driver is next powered on, the saved sequence number is read back and will automatically start running the sequence. If it was in a Hold state at power off it will power on and remain in the 'Hold' state until S1 is pressed again.
Anytime the PIC is put into sleep mode by holding S1 switch down, the currently selected sequence, displayed colour and Hold state will be saved to EEPROM.
Firmware
The HEX file is ready to program directly into a PIC 12F629. The zip file contains the source code which you can modify or just view to see how it works. If you are going to modify the code I recommend you download and install the Microchip MPLAB IDE which will allow you to edit, modify and program the PIC seamlessly.
Description Filename Download link
Source code rgb101g3.zip, v3.0.3, 14/09/2010 download
HEX file ready
to program into
the PIC rgb101g3_main.HEX, v3.0.3,
14/10/2010 download
If you need a PIC Programmer it is strongly recommend the Microchip PICKit 2, this is available from suppliers world wide or direct from Microchip. It's reasonably cheap to buy and reliable.
Format of the Sequence Data
The data used by the application for the RGB sequences is held in the file 'sequenceData.inc' You can edit this file to add, remove or change the data provided. You must ensure that it follows the format described. In particular pay attention to the 'end of sequence' and 'end of all data' markers and also ensure that each line of sequence data contains five comma separated entries. (see screen dump below)
A really useful on-line utility for simulating the sequences can be found here:RGB LED Simulator
(thanks to Marek 'Marki' Podmaka for creating and sharing this simulator)
In the screen dump above note the 'end_of_sequence' markers circled in red and the 'end_of_all_data' marker circled in purple.
You must have at least one sequence present up to a maximum of 256 individual sequences, although you're likely to run out of available memory on the PIC before you reach this limit.
Each line of data starts with a 'dt' (data table) assembler directive.
All data is specified using decimal values.
Each data value must be separated by a comma
The sequence data on each line has five fields:
Fade Rate: speed the colours fade from the current values to the new values. Each step occurs at an interval of 5ms x Fade Rate.
Fade Rate value of 0 indicates the RGB values will be updated immediately without fading.
Fade Rate value must not be set to 255 except to indicate end of sequence.
Hold Time: after fade completes, delay before moving to next line of data. Interval is 50mS x Hold Time
Hold Time value of 255 following a Fade Rate of 255 indicates end_of_all_sequence data.
Red PWM value. 0 = 0% (LED off) through to 255 = 100% (LED fully on)
Green PWM value. 0 = 0% (LED off) through to 255 = 100% (LED fully on)
Blue PWM value. 0 = 0% (LED off) through to 255 = 100% (LED fully on)
Typically changes in LED brightness are more noticeable between 0 and 128 than from 128 to 255.
End of the current sequence data is indicated by the Fade Rate field being set to '255'. When the application encounters this it restarts the sequence from the beginning.
At the end of all available sequence data both the Fade Rate and Hold Time fields must be set to '255'
After editing sequenceData.inc the file should be saved and the rgb101g3_main.asm reassembled. The resulting rgb101g3_main.hex file can them be programmed into the PIC
Reference:
http://picprojects.org.uk/projects/rgb/index.htm
The HEX file is ready to program directly into a PIC 12F629. The zip file contains the source code which you can modify or just view to see how it works. If you are going to modify the code I recommend you download and install the Microchip MPLAB IDE which will allow you to edit, modify and program the PIC seamlessly.
Description Filename Download link
Source code rgb101g3.zip, v3.0.3, 14/09/2010 download
HEX file ready
to program into
the PIC rgb101g3_main.HEX, v3.0.3,
14/10/2010 download
If you need a PIC Programmer it is strongly recommend the Microchip PICKit 2, this is available from suppliers world wide or direct from Microchip. It's reasonably cheap to buy and reliable.
Format of the Sequence Data
The data used by the application for the RGB sequences is held in the file 'sequenceData.inc' You can edit this file to add, remove or change the data provided. You must ensure that it follows the format described. In particular pay attention to the 'end of sequence' and 'end of all data' markers and also ensure that each line of sequence data contains five comma separated entries. (see screen dump below)
A really useful on-line utility for simulating the sequences can be found here:RGB LED Simulator
(thanks to Marek 'Marki' Podmaka for creating and sharing this simulator)
In the screen dump above note the 'end_of_sequence' markers circled in red and the 'end_of_all_data' marker circled in purple.
You must have at least one sequence present up to a maximum of 256 individual sequences, although you're likely to run out of available memory on the PIC before you reach this limit.
Each line of data starts with a 'dt' (data table) assembler directive.
All data is specified using decimal values.
Each data value must be separated by a comma
The sequence data on each line has five fields:
Fade Rate: speed the colours fade from the current values to the new values. Each step occurs at an interval of 5ms x Fade Rate.
Fade Rate value of 0 indicates the RGB values will be updated immediately without fading.
Fade Rate value must not be set to 255 except to indicate end of sequence.
Hold Time: after fade completes, delay before moving to next line of data. Interval is 50mS x Hold Time
Hold Time value of 255 following a Fade Rate of 255 indicates end_of_all_sequence data.
Red PWM value. 0 = 0% (LED off) through to 255 = 100% (LED fully on)
Green PWM value. 0 = 0% (LED off) through to 255 = 100% (LED fully on)
Blue PWM value. 0 = 0% (LED off) through to 255 = 100% (LED fully on)
Typically changes in LED brightness are more noticeable between 0 and 128 than from 128 to 255.
End of the current sequence data is indicated by the Fade Rate field being set to '255'. When the application encounters this it restarts the sequence from the beginning.
At the end of all available sequence data both the Fade Rate and Hold Time fields must be set to '255'
After editing sequenceData.inc the file should be saved and the rgb101g3_main.asm reassembled. The resulting rgb101g3_main.hex file can them be programmed into the PIC
Reference:
http://picprojects.org.uk/projects/rgb/index.htm
0 comments:
Post a Comment