Help needed: Bi-color LED fading for Christmas tree

Hello everyone, have a noob question and would like some pointers on how to get started on a project.

I recently got a christmas tree that has the 2 pin bicolor LED's, but its got a piss poor controller which can only blink the 2 colors back and forth.

I'm trying to setup a controller which can:

  • drive 2 pin bi-color LED's running on 29v, powered by a 29v 0.6A adapter
  • fade between one color to another using PWM
  • Have control over the time it takes to fade from one polarity to another

I'll probably need a DC step down buck converter to drive the arduino, and I've found a very good example for driving the color fading through (GitHub - wolfgang42/bicolorled: Library for driving a bicolor LED with Arduino). The tricky part is figuring out how to make use of the PWM voltage to drive LED's from its own power supply.

I'm a programmer used to OOP and I'd love to spend the time and learn how to get started, and would like to ask for some pointers on where to look for projects that have similar properties, whether on the PWM aspect or on driving LED's with it's own power supply. Also any suggestions on hardware would be appreciated.

Thank you in advance for any suggestions and pointers!

-Lanzer

The tricky part is figuring out how to make use of the PWM voltage to drive LED's from its own power supply.

You will need an H-bridge to be able to drive the led-pins in both directions.

sterretje:
You will need an H-bridge to be able to drive the led-pins in both directions.

Oh, sounds like the L298N is exactly what I need! Thank you!

Took a little while to learn all the basics and how to debug, but everything is up and running thanks to your help!

I've put together a short guide for noobs like me who wants to do the same thing:

[Arduino LED Christmas Light Controller | Fun In the Garage

](Arduino LED Christmas Light Controller | Fun In the Garage)

Yes, our Christmas tree is still up! :smiley:

Haha, great job! Not only to get it working but also the post and having the tree still up :stuck_out_tongue:

Couple remarks/questions:
-You could have used the hardware PWM just fine :slight_smile:

  • I'm missing a schematic :wink:

  • Uhmm:

const int pinCount = 2;
const byte pins[pinCount] = {5};

So using pin 0 then?

I have no idea what the hardware PWM is, can you elaborate? (like using analogWrite?)

Makes sense to include the schematic too. Will slap something together in Photoshop :slight_smile:

You caught the part that I had trouble with and took a while to debug.

The pins array has just one value so the pin count variable should be 1 instead of 2, but the moment I set that value to 1, the color would fade out but not back in. Somehow defining a count of 2 would suppress the issue, and I left it that way.

Turns out the bug is caused by the portion where I told handlePWM to ignore all blinking logic if the brightness is at 100 or zero. During the 5 seconds where brightness is at 100 or 0, It omits the logic but the TickCounter kept incrementing. So by the time it resume the logic, TickCounter had ran away and became a negative value and screwed everything up.

And of course if there's only one pin I really should get rid of the array and loops, though I have a lighted tree topper and I might add another pin later.

All those nasty issues are fixed, and the fading is nice and smooth now. :slight_smile: Thanks for looking over the code!

analogWrite() IS the hardware PWM. And yes, that name is stupid and should have been pwmWrite() or something :slight_smile:

And with a schematic I can look over the code in more detail if you like :slight_smile:

Analog write is definitely a confusing name. Took a while for me to figure out that it's actually digital signal output.

I've slapped together a schematic, hope it works:

If I can actually use analog write but somehow be able to have pin 5 be the opposite polarity, then I'd be in business.

Yeah, you can do that if you connect them to the same timer. I would use timer1 or timer2 for that so you can also increase the PWM frequency. If you connect it to pin 9 and pin 10 you can use the following in setup:

TCCR1A = (TCCR1A & 0b00001111) | COM1A1 | COM1B1 | COM1B0; //makes the two outputs inverse
TCCR1B = (TCCR1B & 0b11111000) | 0x02; //sets PWM frequency to 3,9kHz

Now if you set pin 9 and 10 to the same duty cycle (via analogWrite()) they will be each others inverse.