Help with RGB LEDs

Status
Not open for further replies.

PwnToaster

New Member
Hello, I am still fairly new to this so please bare with me.

For my first project I want to have 5 RGB LEDs to fade slowly from color to color. I plan on using a ATMega168 for the PWM. Right, I need to know if my connections shown in my schematic will work. I know that a microcontroller by itself can run a single led from its pins. Since I need to run 5, I used the ULN2003 IC. I found this info **broken link removed**.
Here's my schematic:
**broken link removed**

Thanks for your help!
 
(how will you make PWN without capacitors ? and with only 1 bit for each color ?)
your schematic will only make them change into diferent colors but it will not fade, to fade you need PWM to slowlly increase and decrease the voltage on each color.

The i.c. in your design is an power darlington driver, i guess... it is meant to drive dc motors and step motor coils... but it will work for your design, you only need to change the the pins used to control the colors to pins that have PWM outputs, or just make 3 simple 8Bit / 4Bit D/A conv. that with your i.c. will work very nice

have fun
 
Last edited:
(how will you make PWN without capacitors ? and with only 1 bit for each color ?)
I believe the ATMega168 can generate PWM without capacitors. I'm having trouble finding which pins are for PWM. I need three, obliviously. Thanks for all of your help!
 
Hello, I am still fairly new to this so please bare with me.
Bare with you?! What kind of forum do you think this is, anyway? I think you meant, "bear with me," but in any case, I'm open-minded!
**broken link removed**
 
Last edited:
I believe the ATMega168 can generate PWM without capacitors. I'm having trouble finding which pins are for PWM. I need three, obliviously. Thanks for all of your help!
The ATMega168 has 6 PWM output pins.

On a 28-pin DIP they are:
PD6 - OC0A - Pin 12
PD5 - OC0B - Pin 11
PB1 - OC1A - Pin 15
PB2 - OC1B - Pin 16
PB3 - OC2A - Pin 17
PD3 - OC2B - Pin 5
 
Last edited:
Thanks, that helps a lot! Here's an updated picture of my schematic. If I put that button on pretty much any pin from PC0 - PC5, I will be able to detect when it's pushed right? Please excuse my noobiness.
 
Thanks, that helps a lot! Here's an updated picture of my schematic. If I put that button on pretty much any pin from PC0 - PC5, I will be able to detect when it's pushed right? Please excuse my noobiness.
Yes. Don't forget a 10K pullup or pulldown resistor on the switch to hold it high or low until it's pressed. If you don't put one, the pin will float when the button isn't pressed, causing randomness. The thing will act like you pressed the button just from you getting your finger near it or bumping the table. Stuff like that.

If you pull it high, then when you press the button the switch should connect to ground.

If you pull it low, then when you press the button the switch should connect to 5V.

Your pullup/pulldown holds the MCU pin at the switch-off voltage level. When you press the button, the switch connects directly to 5V or ground and overpowers the pullup/pulldown.

Have a look at how the pulldown resistors are connected on the schematic on **broken link removed**.
 
Last edited:
Thanks again for your help. I believe the ATMega168 has internal pull down resistors, so an external resistor is not needed, but thanks for reminding me to enable them!
 
Thanks again for your help. I believe the ATMega168 has internal pull down resistors, so an external resistor is not needed, but thanks for reminding me to enable them!
That's probably true. I'm too lazy to look at the datasheet right now. PICs have internal pullups on some ports too.
 
I got another question (I'm full of 'em ). What;s the difference between a common cathode and a common anode. I know that a common cathode shares ground and a common anode shares VCC, but whats the advantage of each? I was planning on using common cathode LEDs, but after referring to **broken link removed** page, i saw he used common anodes LEDs. Are they necessary?
 
As far as I know there's no advantage or disadvantage. Use whatever suits your circuit, or whatever you have. It's like NPN or PNP transistors. Both work just as good as the other. You pick the one you need to suit what you're doing.

Your message inspired me to dig out the RGB LED I bought last order and tinker with it. I'm putting up an article on my site now. It'll be ready in 10 or 15 minutes. Nothing terribly earth shaking. Just a simple little newb article.
 
I prefer common anode devices simply because there's a much larger selection of parallel and serial/spi "sinking" drivers (ULN2803, MIC5821/5841, TPIC6C595, etc.) available to work with compared to "sourcing" drivers like the parallel MIC2981/2982.

Mike
 
PwnToaster, I just looked at your circuit and there is a problem with the way you want to drive the 5 rgb leds.
In the circuit you have shown GND connected to the common Anodes where it should be a positive voltage.
Also, you are driving them in parallel with only 3 resistors in total.
This would not work properly as each led has slightly different turn-on voltage and so some will glow brighter than others.

It needs a resistor for each led (15 in total) to ensure each one gets the same current.
Alternatively you could multiplex them to reduce the resistors to 3 but requires using 5 more outputs on the microcontroller.

It is not essential to use the hardware pwm , it can be done in software for this application.
 
Last edited:
I recently made up a RGB project with a PIC micro, although different controllers, perhaps the way I went about it might lend you some ideas?

Video of the project
**broken link removed**

Source code and explanations can be found here **broken link removed**

Also, you are driving them in parallel with only 3 resistors in total.
This would not work properly as each led has slightly different turn-on voltage and so some will glow brighter than others.

Its generally a very small difference in brightness, if noticeable at all with good LED's
 
I'm putting up an article on my site now. It'll be ready in 10 or 15 minutes. Nothing terribly earth shaking. Just a simple little newb article.

Futz,

Nice demo, as always. My only 'nit' would be to use a shadow variable to prevent turning on the LEDs when they have a duty cycle of 0. Updating the output latch from the shadow also produces consistant duty cycle durations. Your example (and another forum members example) will produce different "on times" for the three colors when they have been set to the same duty cycle. Of course this is not important in the context/scope of **broken link removed**.

Mike
Code:
;
;  Dim red, grn, blu As Byte    ' duty cycle, 0..255
;  Dim dcy As Byte              ' duty cycle counter, 0..255
;  Dim shadow As Byte           '
;
;  Interrupt rgb_interrupt()
;    t0if = 0                   ' clear timer0 interrupt flag
;    inc(dcy)                   ' increment duty cycle
;    if dcy = 0                 ' if new period
;      shadow = %01110000       ' set RC6..RC4 shadow bits
;    endif                      '
;    if red = dcy then          '
;      shadow.bit6 = 0          ' turn off RC6 shadow bit
;    endif                      '
;    if grn = dcy then          '
;      shadow.bit4 = 0          ' turn off RC4 shadow bit
;    endif                      '
;    if blu = dcy then          '
;      shadow.bit5 = 0          ' turn off RC5 shadow bit
;    endif                      ' 
;    LATC = shadow              ' update port from shadow
;  End Interrupt
;
rgb_interrupt                   ; 14 words
        bcf     INTCON,T0IF     ; clear timer 0 interrupt flag
        movlw   b'01110000'     ;
        infsnz  dcy,F           ; new period? no, skip, else
        movwf   shadow          ; set led bits in shadow
        movf    dcy,W           ; wreg = duty cycle, 0..255
        cpfsgt  red             ; (F) - (W), skip if (F) > (W)
        bcf     shadow,6        ; clear bit if (F) <= (W)
        cpfsgt  grn             ; grn - dcy, skip if grn > dcy
        bcf     shadow,4        ; clear bit if grn <= dcy
        cpfsgt  blu             ; blu - dcy, skip if blu > dcy
        bcf     shadow,5        ; clear bit if blu <= dcy
        movff   shadow,LATC     ; update port c from shadow
        retfie  FAST            ;
(1) this code / algorithm / method is public domain and anyone may use it as they wish...
 
Last edited:
Its generally a very small difference in brightness, if noticeable at all with good LED's
Ya, the LED I'm using has very equal red and green. The blue is just a little dimmer. I plan to lower the value of the resistor feeding blue to equalize things a bit. Should un-pinkify the magenta a little.
 
My only 'nit' would be to use a shadow variable to prevent turning on the LEDs when they have a duty cycle of 0. Updating the output latch from the shadow also produces consistant duty cycle durations.
Good plan. I'll improve my quick and dirty code.

Your example (and another forum members example) will produce different "on times" for the three colors when they have been set to the same duty cycle.
Only the tiniest bit different. But may as well fix it anyway with the shadow variable method.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…