Practical PIC Projects

 

 

RGB LED PWM Driver
Standalone PWM
controller for  RGB LEDs
using 12F6xx PIC


The RGB LED Colour wash has proved to be very popular and has been the most frequently downloaded code on the site since it was made available.  The section in the original PIC Projects page for the LED colour wash got so long I decided to give it a page of its own.  If you want to build your own RGB LED controller read on.

For 2006 I've completely rewritten the application.  It's now much easier to add, edit and change the sequence data.  It also now has a sleep function so a battery operated version can be built that doesn't need a power switch.  The new code runs on the 12F629, 12F675 and the newer 12F683 which, with 2K of program memory has plenty of room for user sequences.

I now also have working an RGB PWM driver that is controlled over a serial interface.  Each RGB driver will receive data from a PCs RS232 interface, or standalone controller and they will all run in sync with each other.
(Full details here)

Finally here's some photo's of the controller software described here operating with Kingbright XPower LED modules. These LED's are really bright. Note: the output driver has been modified to use BS170 mosfets.
(XPower LEDs are available from http://www.rapidelectronics.co.uk/)

Another LED I've tried is made by Lamina Ceramics. Their latest product is called the Atlas and the RGB version has 6 LED dies (2 x red, 2 x green, 2 x blue) in a single package.  It operates at 350mA and is the best 350mA LED I've seen, the brightness is exceptional.  Available in the UK from http://www.leds.co.uk/ (the Atlas LED is not currently listed on the web site but if you contact them they can supply it)
 

More details of this and the other developments will be appearing on this page over the next few months. August 2006.


Description

The original RGB PWM driver application that I wrote in 2004 had a few shortcomings. Probably the biggest was that it was not easy to add to or change the sequences.  This new version addresses that problem, is more flexible and now includes the ability to put the PIC to 'sleep' and 'wake' it again using the sequence select switch, eliminating the need for an on/off switch in battery powered applications.

The circuit 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.

The code can be assembled for use with the following PICs: 12F629, 12F675, 12F683.  Just select the correct processor in the MPLAB IDE before assembling.

If you are currently using the old version of the RGB PWM code (light10.asm) you can reprogram the PIC with the new version of code as it is fully compatible with the original PCB.


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 red-fade out, green-fade out, blue-fade out repeatedly.
     
  • Press the SW1 sequence select switch to step through all available sequences. When the last sequence has been reached it will go back to the first available sequence.  Each time the SW1 switch is pressed the RGB LED PWM values are set back to 0 (LEDs off)
     
  • Press and hold SW1 switch for about 1.2 seconds to put the PIC into sleep mode.  Once in sleep mode, press the SW1 switch for about 2 seconds then release it to wake the PIC from sleep. If the SW1 button isn't held for two seconds the PIC returns to sleep - this prevents the circuit from being accidentally turned on.  When  operating the RGB controller from batteries (without a 78L05 regulator) a power on/off switch is no longer needed since the PIC uses only a few microamps when 'sleeping' 
     
  • About 10 seconds after the SW1 sequence switch is last pressed the currently selected sequence number is 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.
     
  • Anytime the PIC is put into sleep mode by holding SW1 switch down, the currently selected sequence is also saved to EEPROM.

Code, Construction & PCB

  • Source Code (rgbsa-inet.asm - 23/06/2006)
    The source code comprises a single .ASM file and four .INC files.  All five files are required and should be extracted to a single directory on your computer.  To reassemble the code, open the rgbsa-inet.asm file, then select Project - Quick Build from the MPLAB IDE menu bar.
     

  • Programmer ready .HEX files
    Hex for 12F629 and 12F675 (right-click Save As)
    Hex for 12F683 (right-click Save As)
    (please read the FAQ below)
     

  • Schematic JPEG , PDF
    If you build the battery version shown in the schematic, the power on/off switch is no longer needed since the SW1 switch can be used to put the PIC into sleep mode and wake it up again
     

  • Veroboard prototype Front, Back

  • PCB version Front, Back, Lit
    The LEDs and switch are tacked onto the track side of the PCB with rest of the components mounted normally, this looks much better when it's mounted in a light fitting. These photo's show the original PCB design.  The artwork below is the third revision I've done and puts the switch in a better position on the board.
     

  • RGB controller using a single RGB LED on breadboard

  • PCB Overlay (pdf) 

  • PCB Layout Rev3 (pdf)


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)

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.

  1. Each line of data starts with a 'dt' (data table) assembler directive.

  2. All data is specified using decimal values.

  3. Each data value must be separated by a comma

  4. The sequence data on each line has five fields:

    1. 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. (see d. below)
         

    2. 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.
         

    3. Red, Green and Blue PWM values. 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.
       

  5. 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.

  6. 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 rgbsa-inet.asm reassembled. The resulting rgbsa-inet.hex file can them be programmed into the PIC.


Saving the Internal Oscillator calibration instruction.

This only applies to the 12F629 & 12F675.  The 12F683 uses a different method for calibration

See my tip to make sure you will never loose the OSCCAL setting

Rather than explaining this myself, here's the section from the 12F675 datasheet.  Your programmer software should read this value from the PIC, then merge it with the .HEX file before programming. 


FAQ:

  • Multiple RGB lights not staying in sync
    This note follows a query I had from someone who built these RGB lights.  Because the timing is generated from the internal 4Mhz oscillator and there is no way to synchronize each device with others, even if they are running the same sequence, the colours quickly get out of sync with each other. 

    If you're building a light bar or any application where you need more than one unit and they need to stay in sync you should build the 'serial controlled RGB driver'.
     

  • Oscillator Calibration Instruction
    I've had a number of enquiries from people who can't get the code to work and it has transpired that they / their programmer has erased the OSCAL value. Without the calibration RETLW instruction the code crashes so you must ensure that it is present or the code will not run.

  • How can I recalibrate the internal oscillator on a 12F629 or 12F675?

    Microchip have published an application note and software to allow you to recalibrate the internal oscillator and this will of course allow you to find the correct value if you've accidentally erased it.
    http://ww1.microchip.com/downloads/en/AppNotes/00250a.pdf

    Search on Microchip's website for "an250.zip" for the application code.

    Alternatively just insert a RETLW 0x80 instruction at address 0x3FF.  This won't calibrate the oscillator correctly but the code will now run.

    See my tip to make sure you will never loose the OSCCAL setting

  • Verify Errors after programming
    The RGB light programme code uses the PICs EEPROM to store the last used mode.  When the PIC is powered up for the first time after it has been programmed it checks to see if the last sequence value saved in EEPROM is less than or equal to the number of available sequences. If it is not, the code set the value in the EEPROM to 0.

    I had a series of e-mails from a guy in Greece who had lots of problems with "Verify 0000!" errors.  I eventually built the programmer hardware he was using and ran the software to try and find out what was happening.  It turned out that this particular combination of programmer/software was powering up the PIC between writing the code into the PIC and verifying it.  This caused the EEPROM to get initialised to "00" but the software was still expecting "FF" so the verify failed.

    The programmer hardware was PLMS OziPIC'er  and the software was IC-Prog 1.05D.  I must stress that apart from this idiosyncrasy this is a perfectly capable PIC programming setup and once aware of what is happening it is easy to account for and work around. 

    (A special thank you to Panagiotis from Athens - thanks to the Internet, his excellent English and much patience we got to the bottom of this problem despite the language barrier and the distance)

  • Editing, adding changing the RGB sequence data.

    All the sequence data is now held in the sequenceData.inc file so it is really easy to add your own sequences.  The application works out how many sequences are present, where they start and finish and takes care of page boundary crossing. All you have to do is put the correctly formatted data into the file, save it then reassemble.

    Please take your time when editing the sequenceData.inc file since errors are likely to cause the RGB Driver to do unexpected things or crash.

    If you see an 'Warning [220]' error during assembly like that shown below, you've added more sequence data than the PIC has available memory.


    Clean: Deleting intermediary and output files.
    Clean: Done.
    Executing: "C:\Program Files\Microchip\MPASM Suite\MPAsmWin.exe" /q /p12F675 "rgbsa-inet.asm" /l"rgbsa-inet.lst" /e"rgbsa-inet.err"
    Warning[220] C:\CODE\RGBSA-INET.ASM 158 : Address exceeds maximum range for this processor.


    If you see an Error [112] message during assembly check for missing comma separators in the sequenceData.inc file

    Clean: Deleting intermediary and output files.
    Clean: Done.
    Executing: "C:\Program Files\Microchip\MPASM Suite\MPAsmWin.exe" /q /p12F675 "rgbsa-inet.asm" /l"rgbsa-inet.lst" /e"rgbsa-inet.err"
    Error[112] C:\CODE\SEQUENCEDATA.INC 56 : Missing operator

  • Comments or problems with this code / project? Please send an e-mail to:

     



Conditions of use

You may only use the software provided here for your private, non-commercial use.


Disclaimer

All information and software on this website are provided "as is", and without warranty of any kind, either express or implied.

In no event shall I be liable to you or any third party for any consequential, incidental, direct, indirect, special, punitive or other damages arising out of the use or inability to use the software and/ or hardware on this web site.