New Library for BiColor LEDs

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.
Downloads & more info can be found at Arduino Playground - BiColorLED.
This thread is for general discussion of the library itself. Post bugs, suggestions, comments, etc. here.

by passing AC current through

And where does this AC come from?

PaulS:

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.

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.

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.

PaulS:

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.

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.

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

Docedison:
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...

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

in BiColorLED.h please change

#include "WProgram.h"

to

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

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

No problem, it was a minor thing.

Thank you for sharing your library.

wolfgang42:
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

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.

teckel:
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.

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.

Hi, requesting assistance.

Opened the 1.4 file from the ZIP and am immediately getting error

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
BiColorLED:13: error: 'BiColorLED' does not name a type
BiColorLED.pde: In function 'void loop()':
BiColorLED:23: error: 'led' was not declared in this scope
BiColorLED:42: error: 'led' was not declared in this scope

Please advise

Mike

Dear wolfgang42:

I am using the BiColorLED library (version 1.4) that you wrote, and it works very well. Thank you very much for sharing it!

Have you made any progress with adding support for varying the ratio of red to green? If so, I would be interested in trying a new version with that feature.

Thanks again!

Sincerely,

Mark Schilke