Go Down

### Topic: New Library for BiColor LEDs (Read 9697 times)previous topic - next topic

#### wolfgang42

##### Aug 01, 2012, 05:38 pm
I just wrote and uploaded a new library for controlling bicolor LEDs. It lets you drive both solid colors, as well as allowing you to put the two together "simultaneously" by passing AC current through, rapidly turning on one color, then the other.
This thread is for general discussion of the library itself. Post bugs, suggestions, comments, etc. here.

#### PaulS

#1
##### Aug 01, 2012, 06:18 pm
Quote
by passing AC current through

And where does this AC come from?
The art of getting good answers lies in asking good questions.

#### wolfgang42

#2
##### Aug 01, 2012, 07:28 pm

Quote
by passing AC current through

And where does this AC come from?

To turn the LED red, pin1 is set HIGH and pin2 is set low, so the power goes one way. To turn it green, the polarity of the two pins is swapped, so the power goes the other way. Yellow is produced by rapidly switching between red and green, producing AC. The AC doesn't come from a 60Hz wall outlet, but is manually generated by the Arduino.

I hope that's clear; if it's not I can try to explain it better.

#### wolfgang42

#3
##### Aug 25, 2012, 06:22 am
I just released version 1.1 of this library. The new version adds support for alternating between two colours. You can download it from the playground page.

#### PaulS

#4
##### Aug 26, 2012, 05:44 pm
Quote
To turn the LED red, pin1 is set HIGH and pin2 is set low, so the power goes one way. To turn it green, the polarity of the two pins is swapped, so the power goes the other way. Yellow is produced by rapidly switching between red and green, producing AC

You are not producing AC. AC is alternating current, where the voltage changes from positive to negative 50 or 60 times per second. All you are doing is switching which way the current, at +5V, is flowing.
The art of getting good answers lies in asking good questions.

#### wolfgang42

#5
##### Aug 26, 2012, 08:10 pm

Quote
To turn the LED red, pin1 is set HIGH and pin2 is set low, so the power goes one way. To turn it green, the polarity of the two pins is swapped, so the power goes the other way. Yellow is produced by rapidly switching between red and green, producing AC

You are not producing AC. AC is alternating current, where the voltage changes from positive to negative 50 or 60 times per second. All you are doing is switching which way the current, at +5V, is flowing.

I refer you to Merriam-Webster's definition of Alternating Current: http://www.merriam-webster.com/dictionary/alternating+current.
Quote
an electric current that reverses its direction at regularly recurring intervals

• An electric current: This is electricity we're working with, even at 5V.

• that reverses its direction: in other words, which way the current is flowing

• at regularly recurring intervals: If you call drive() at regularly recurring intervals (which you should, to make the yellow look right), then the current reverses at regularly recurring intervals.

If there's any part of this you don't understand, have a look at my explanation above, or hook the output of my library up to an oscilloscope and see for yourself.

#### Docedison

#6
##### Aug 30, 2012, 10:31 pmLast Edit: Aug 30, 2012, 11:59 pm by Docedison Reason: 1
Pulsating dc and ac are the same, a variable flow of electrons. The primary difference is the "Reference"... If "AC" is "Referenced" from the most negative level them you could say "It''s Just pulsating DC.
Whether it is sinusoidal, square rectangular (duty cycle not 50%) triangular.
Come to think about the definition a little and you will see that the prime requirement for AC... The one Difference that sets AC apart from noise is just Periodicity.
It is the periodicity that sets it apart and allows it to do useful and predictable work is it's periodicity.
If you put in place a device that passes AC only, a capacitor or a transformer will pass your "DC" very well.
As to the library it does produce a signal that makes the LED light up Yellow... So the difference is?.. The LED works the same as if it had AC on it
Just the point of reference... Put it in the right place and your "DC" signal becomes "AC".
Place a diode in series and you remove 1/2 of the DC signal... Just as a diode would with AC... and you have DC again... pulsating but of one polarity... The signal cannot pull down when the input goes to it's lowest point because the diode will not conduct in the reverse direction.

Doc
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

#### wolfgang42

#7
##### Aug 30, 2012, 10:44 pm

Pulsating dc and ac are the same, a variable flow of electrons. The primary difference is the "Reference"... If "AC" is "Referenced" from the most negative level them you could say "It''s Just pulsating DC.
Whether it is sinusoidal, square rectangular (duty cycle not 50%) triangular.
Come to think about the definition a little and you will see that the prime requirement for AC... The one Difference that sets AC apart from noise is just Periodicity.
It is the periodicity that sets it apart and allows it to do useful and predictable work is it's periodicity.
Noise can be called AC... Pink noise (audio) and White noise (Full Spectrum) and random impulse noise are AC but because of the lack of predictability, little real work can be done with them.
If you put in place a device that passes AC only, a capacitor or a transformer will pass your "DC" very well. it will also filter the signal due to its reactance or response to an "AC" signal... again the Periodicity.
As to the library it does produce a signal that makes the LED light up Yellow... So the difference is?
Just the point of reference... Put it in the right place and your "DC" signal becomes "AC".
Place a diode in series and you remove 1/2 of the DC signal... Just as a diode would with AC... and you have DC again... pulsating but of one polarity... The signal cannot pull down when the input goes to it's lowest point because the diode will not conduct in the reverse direction.

OK, that mostly lost me. I just know the library works by reversing the direction of the power at regular intervals...

#### Docedison

#8
##### Aug 30, 2012, 11:52 pmLast Edit: Aug 31, 2012, 12:04 am by Docedison Reason: 1
I apologize for "loosing" you but my "Rant" was about users that ASSUME that they KNOW everything.
If for example, you consider a 60Hz 10V PP signal... AC Right?, NO WRONG, a repetitive signal of 10 Volts amplitude only... repeating @ a 16.67 Ms interval.
The AC signal requires a reference of +5V. to recover the "Negative" part. It is still a 10V PP signal.
The basic concept of AC is a negative going signal... A Voltage that alternates polarity.
However polarity is a thing related to a "reference" point, usually 0 volts.
The point is that this signal Will pass through devices made for AC only.
Your alternating dc voltage will pass through a transformer... DC won't. Even a single pin from an Arduino coupled through a capacitor
with a repeating pulse present will pass both the transformer and the capacitor and be present at the other transformer winding... DC won't.
The capacitor isn't required but I included it to drive home the point about AC vs DC.
So it clearly isn't DC any more. To prove it is simple. It requires an external "reference" a pair of equal value resistors from Vcc to ground.
Measure from the center point of the resistors to the pin outputting the "Pulse" you will measure an AC 5 V PP signal.... at least on an Oscilloscope.
A DVM  will "average" or really mangle the signal But it is AC.

Doc
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

#### PaulDriver

#9
##### Dec 13, 2012, 11:20 am
in BiColorLED.h please change

Code: [Select]
`#include "WProgram.h"`

to

Code: [Select]
`#if defined(ARDUINO) && ARDUINO >= 100#include "Arduino.h"#else#include "WProgram.h"#endif`

For Arduino 1.x support

And to help PaulS and anyone else Doc lost, you must look at the current from the viewpoint of the Bi-Polar LED pair, not the Arduino.  To the the Arduino, it's just DC, BUT to the pair of diodes being driven by the Arduino, it's A/C.

#### wolfgang42

#10
##### Dec 13, 2012, 01:57 pm
Version 1.2 released with Arduino 1.0 support. Thanks PaulDriver for the patch!

#### PaulDriver

#11
##### Jan 01, 2013, 07:53 pm
No problem, it was a minor thing.

Thank you for sharing your library.

#### teckel

#12
##### Jan 23, 2013, 04:33 am

Version 1.2 released with Arduino 1.0 support. Thanks PaulDriver for the patch!

I also have a library (toneAC) that does "AC" to drive a speaker at almost twice the volume as the standard tone library.  This is possible because I alternate the 5 volts between two pins.  In my case, it's designed to be extremely fast, so I use the Arduino's PWM pins and timer 1.  This also allows for perfect switching between the two pins without any programming slowing things down.

As a bonus, my library can also drive a bicolor two pin LED as yours does (one of my example sketches included with the library controls a bicolor LED with a pot to adjust the cycle speed).  You may want to check out my source.  As toneAC is designed for ultra speed and accuracy, you must use the timer 1 controlled PWM pins.  It also is totally driven totally by port registers for the fastest and smallest code.  Looking at my library may assist you.  I also have a NewTone library thats a modified version of toneAC but allows you to specify what pin you want to drive a speaker with.  This also may assist you with your library.

While writing library using port registers and timers may be a little more challenging at first, it's really not that hard once you do it a few times.  And, the benefits are many.  Very small code size, very fast, color switching and duty cycle can all be done in the background, no reason for delay statements which can kill a project, etc.

Best of luck with your project!

Tim
My hardware: Arduino, Teensy 2.0, Teensy 3.2 & custom ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My Projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

#### wolfgang42

#13
##### Feb 09, 2014, 12:13 am
I've just released version 1.3 of the BiColorLED library, which fixes the Blink example (I left debugging code in--oops). I've also put the code on GitHub.

I also have a library (toneAC) that does "AC" to drive a speaker at almost twice the volume as the standard tone library.  This is possible because I alternate the 5 volts between two pins.  In my case, it's designed to be extremely fast, so I use the Arduino's PWM pins and timer 1.  This also allows for perfect switching between the two pins without any programming slowing things down.

As a bonus, my library can also drive a bicolor two pin LED as yours does (one of my example sketches included with the library controls a bicolor LED with a pot to adjust the cycle speed).  You may want to check out my source.  As toneAC is designed for ultra speed and accuracy, you must use the timer 1 controlled PWM pins.  It also is totally driven totally by port registers for the fastest and smallest code.  Looking at my library may assist you.  I also have a NewTone library thats a modified version of toneAC but allows you to specify what pin you want to drive a speaker with.  This also may assist you with your library.

While writing library using port registers and timers may be a little more challenging at first, it's really not that hard once you do it a few times.  And, the benefits are many.  Very small code size, very fast, color switching and duty cycle can all be done in the background, no reason for delay statements which can kill a project, etc.

Best of luck with your project!

Tim

I'm not sure how I missed this post before; I probably overlooked the email.

The BiColorLED library is intentionally simplistic: it does something very simple, and does it well. It started out as part of one project, and then I decided to turn it into a library rather than doing a copy-paste. The original project was avoiding delay statements for other reasons, and the timer was being used for something else in the new one. I If anybody needs the features that your library provides (background operation, ultra-fast switching, color ranges) I would suggest that they use that one instead. Not that I don't like the idea of doing those things, but there's no point in reinventing the wheel, and the BiColorLED library tries to stay as simple as possible.

#### wolfgang42

#14
##### Apr 12, 2014, 07:41 pm
And another update, version 1.4. This one adds an overloaded setColor(color1, color2) method, and support for different delays on the two blink colors (to allow flashing/winking). As usual, see the Arduino wiki or GitHub to download.

Go Up