RGB LED Controller

Discussion in 'The Projects Forum' started by brmo, Jun 21, 2010.

  1. brmo

    Thread Starter New Member

    Jun 21, 2010
    3
    0
    I am seeking a circuit that will allow me to use RGB LED's and push different colors out of them via RGB or even hex. If its programmable once, then standalone, that would be the ultimate goal. My main issue is that i would like a button that would scroll through the available colors that i program in, then when hitting the end, scroll right back through. Also, a save into non-volatile memory would be great so that the color i last selected will be chosen the next time the circuit becomes active.

    Any ideas or links on how to accomplish this task?

    Thanks!
     
  2. Wendy

    Moderator

    Mar 24, 2008
    20,766
    2,536
    Can't help you with the code, but you'll need 3 different PWM signals, each will control the intensity of the particular color your interested in. I have some Voltage to PWM designs if you're interested.
     
  3. brmo

    Thread Starter New Member

    Jun 21, 2010
    3
    0
    Thanks much, that would be great.
     
  4. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    I threw together this SIRC RGB Accent Light Controller last week to see if I could generate 25-usec soft PWM "steps" while decoding Sony SIRC remote codes. The program works well but I still need to add 64 step gamma correction to smooth out color changes.

    You could probably use something like this circuit and program a few additional remote codes to save color settings and then "run" the saved settings in animation.

    Have you searched for existing projects or products which may already do everything you want?

    Regards, Mike
     
    Last edited: Jun 21, 2010
  5. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
  6. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    Here you go for a PIC10F200.

    Code ( (Unknown Language)):
    1. ;Soft PWM for RGB led, common anode
    2. #chip 10f200, 4
    3. #config INTRC_OSC, MCLRE_OFF    
    4.  
    5. #define Red GPIO.0
    6. #define Green GPIO.1
    7. #define Blue GPIO.2
    8. #define Button GPIO.3
    9. dir GPIO b'001000'
    10. GPIO = b'111'
    11.  
    12. red_duty = 0
    13. green_duty = 0
    14. blue_duty = 0
    15. color = 0
    16.  
    17. Main:
    18. If button = 0 Then
    19.     ButtonCount = 0
    20.     Do While Button = 0
    21.         wait 1 10ms
    22.         ButtonCount += 1
    23.     Loop
    24. end if
    25. If ButtonCount > 3 then
    26.     Call ChangeColor
    27.     ButtonCount = 0
    28. end if
    29. Call SoftPWM
    30. goto Main
    31.  
    32. sub ChangeColor
    33. If color = 6 Then color = 0
    34.     Select Case color
    35.         Case 0    ;Red
    36.             Red_Duty = 255
    37.             Green_Duty = 0
    38.             Blue_Duty = 0
    39.         Case 1    ;Green
    40.             Red_Duty = 0
    41.             Green_Duty = 255
    42.             Blue_Duty = 0
    43.         Case 2    ;Yellow
    44.             Red_Duty = 255
    45.             Green_Duty = 80
    46.             Blue_Duty = 0
    47.         Case 3    ;Blue
    48.             Red_Duty = 0
    49.             Green_Duty = 0
    50.             Blue_Duty = 255
    51.         Case 4    ;Purple
    52.             Red_Duty = 255
    53.             Green_Duty = 0
    54.             Blue_Duty = 55
    55.         Case 5    ;White
    56.             Red_Duty = 255
    57.             Green_Duty = 255
    58.             Blue_Duty = 255
    59.     end Select
    60. color += 1
    61. end sub
    62.  
    63. sub SoftPWM
    64.     For duty = 1 to 255
    65.         If red_duty > duty then
    66.             red = 0
    67.         else
    68.             red = 1
    69.         end if
    70.         If green_duty > duty then
    71.             green = 0
    72.         else
    73.             green = 1
    74.         end if        
    75.         If blue_duty > duty then
    76.             blue = 0
    77.         else
    78.             blue = 1
    79.         end if
    80.     Next
    81. end sub
     
    Last edited: Jun 22, 2010
  7. Wendy

    Moderator

    Mar 24, 2008
    20,766
    2,536
    I have an article in my blog,

    LEDs, 555s, Flashers, and Light Chasers

    I have a second copy in the forum I use to rewrite and check changes. The title is the same.

    LEDs, 555s, Flashers, and Light Chasers

    I've been working on a schematic using a DC voltage to PWM circuit to do this. At the moment it looks like this.

    [​IMG]

    I have another schematic in the works that will be a bit closer to what you want.

    Here is the base schematic for the concept.

    [​IMG]
     
  8. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    If you want memory, then a processor is definitely the way to go. Even if you just wanted some kind of random operation of the 3 colors, I'd still do it with a processor. (It would actually be pseudo-random, but you can have it follow a sequence that's so long that most likely nobody would notice.)
     
  9. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    My previously posted code has been corrected, the SoftPWM was not right, but didn't find out till started mixing colors:rolleyes:.
     
  10. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    nickelflipper,

    can you note which language that program is in, please?
     
  11. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    Sorry, .....it's GCBasic. Mostly holding up the code as a template for a state machine (i.e. button press menu), with a soft pwm for the rgb led.

    Down to just two spare variables with the supplied "high level" code on the PIC10F200.
     
  12. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    That's still pretty darn impressive when you consider that the 10F200 only has 255 words of program memory and 16 bytes of RAM.

    The software for that SIRC RGB controller uses 155 words of memory and 12 bytes of RAM so I think it might fit on a 10F200 after I add the gamma correction code.

    Regards, Mike
     
  13. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    Good luck with your port. These brain dead Pics make you think.

    Trying to get the button to work as a color and a blink rate changer, not quite there yet. Will post if I can get the pwm timing part down. Down to zero variables and 9 program words at the moment.
     
  14. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    Re: Braindead PICs was referring to the 10F's

    Here's code for selectable rgb color and blink rate. Still room for more, a random function? .... might be stretching it. Cutting loose of the delay library's saved a bunch of variables/ram.
    Code ( (Unknown Language)):
    1.  
    2. ;GCBasic program for RGB led (common anode)
    3. ;with selectable color and blink rate
    4. ;Kent Schafer 6/23/2010
    5.  
    6. #chip 10f200, 4
    7. #config INTRC_OSC, MCLRE_OFF    
    8.  
    9. #define Red GPIO.0
    10. #define Green GPIO.1
    11. #define Blue GPIO.2
    12. #define Button GPIO.3
    13. dir GPIO b'001000'
    14. GPIO = b'111'
    15.  
    16. red_duty = 255
    17. green_duty = 0
    18. blue_duty = 0
    19. color = 0
    20. BlinkRate = 0
    21. BlinkPeriod = 0
    22.  
    23. clrwdt    ;must do prior to changing prescale, per data sheet
    24. movlw    B'11000110'    ;asign prescaler to TMR0, prescale 1:128
    25. asm    option
    26.  
    27. Main:
    28. NoBlink:
    29. ;LED On time
    30. For Blinky1 = 1 to BlinkPeriod
    31.     Call SoftPWM
    32.     If button = 0 Then
    33.         ButtonCount = 0
    34.         Do While Button = 0
    35.             clrf TMR0    ;use TMR0 as a delay mechanism
    36.             Delay1:
    37.             btfss TMR0,7
    38.             goto Delay1
    39.             ButtonCount += 1
    40.         Loop
    41.         If ButtonCount > 3 Then
    42.             ;it's a long press to change BlinkRate
    43.             If ButtonCount > 25 then
    44.                 BlinkRate += 1
    45.                 Call Blink
    46.                 ButtonCount = 0
    47.             ;it's a short press to change the color
    48.             Else
    49.                 color += 1
    50.                 Call ChangeColor
    51.                 ButtonCount = 0
    52.             end if
    53.         end if
    54.     end if
    55. If BlinkRate = 0 Then goto NoBlink
    56. Next
    57.  
    58. ;LED Off Time
    59. GPIO = b'111'    ;turn off Common Anode rgb led
    60. For Blinky2 = 1 to BlinkPeriod
    61.     clrf TMR0
    62.     Delay3:
    63.     btfss TMR0,7
    64.     goto Delay3
    65.     If button = 0 Then
    66.         ButtonCount = 0
    67.         Do While Button = 0
    68.             clrf TMR0
    69.             Delay2:
    70.             btfss TMR0,7
    71.             goto Delay2
    72.             ButtonCount += 1
    73.         Loop
    74.         If ButtonCount > 3 Then
    75.             If ButtonCount > 25 then
    76.                 BlinkRate += 1
    77.                 Call Blink
    78.                 ButtonCount = 0
    79.             Else
    80.                 color += 1
    81.                 Call ChangeColor
    82.                 ButtonCount = 0
    83.             end if
    84.         end if
    85.     end if
    86. Next
    87. goto Main
    88.  
    89. ;***********************************
    90. sub Blink
    91. If BlinkRate = 5 Then BlinkRate = 0
    92. Select Case BlinkRate
    93.     Case 0
    94.         BlinkPeriod = 0
    95.     Case 1
    96.         BlinkPeriod = 5
    97.     Case 2
    98.         BlinkPeriod = 10
    99.     Case 3
    100.         BlinkPeriod = 20
    101.     Case 4
    102.         BlinkPeriod = 60
    103. end Select
    104. end sub
    105.  
    106. sub ChangeColor
    107. If color = 6 Then color = 0
    108.     Select Case color
    109.         Case 0    ;Red
    110.             Red_Duty = 255
    111.             Green_Duty = 0
    112.             Blue_Duty = 0
    113.         Case 1    ;Green
    114.             Red_Duty = 0
    115.             Green_Duty = 255
    116.             Blue_Duty = 0
    117.         Case 2    ;Yellow
    118.             Red_Duty = 255
    119.             Green_Duty = 80
    120.             Blue_Duty = 0
    121.         Case 3    ;Blue
    122.             Red_Duty = 0
    123.             Green_Duty = 0
    124.             Blue_Duty = 255
    125.         Case 4    ;Purple
    126.             Red_Duty = 255
    127.             Green_Duty = 0
    128.             Blue_Duty = 55
    129.         Case 5    ;White
    130.             Red_Duty = 255
    131.             Green_Duty = 255
    132.             Blue_Duty = 255
    133.     end Select
    134. end sub
    135.  
    136. sub SoftPWM
    137.     For duty = 1 to 255
    138.         If red_duty > duty then
    139.             red = 0
    140.         else
    141.             red = 1
    142.         end if
    143.         If green_duty > duty then
    144.             green = 0
    145.         else
    146.             green = 1
    147.         end if        
    148.         If blue_duty > duty then
    149.             blue = 0
    150.         else
    151.             blue = 1
    152.         end if
    153.     Next
    154. end sub
     
  15. brmo

    Thread Starter New Member

    Jun 21, 2010
    3
    0
    nickelflipper,

    you are amazing. The blink feature is a great touch that i never thought about. Not that its needed, but what would need to change if the LED's had a common cathode instead? I just have 10 or so common cathode's lying around.

    Thanks to all of you, this community has been great and i hope to return the favor.
     
  16. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    This has been a great project for the PIC10F, thanks for the motivation.

    One thing to consider for the blink feature, is to turn off the GPIO/leds and put the PIC to sleep, to save on batteries.

    For common cathode operation, just invert the sense of the GPIO, which would be red, green, blue in the SoftPWM sub. Using a pre-compiler conditional like #ifdef ComCathode would make for a proper library.

    A pseudo random function fits easily. With a ramp function, it might work just like those solar pond lights.
     
Loading...