When sharing some time with my daughter, we wired up an RGB LED bug that I got from FunGizmos. We learned together that this package was a common anode, but that the little "data sheet" on the baggy was inaccurate. In writing up the simplest of programs, I thought it might be useful as a generic Arduino example.
It touches on a couple things I don't see much of in other examples, such as common anode wiring, and making simple functions for repetitive tasks.
/*
* Cycles a three-LED common-anode package through the rainbow.
*
* Demonstrates fading multiple outputs, common-anode wiring, and the
* use of functions to repeat similar tasks.
*
* Often, individual LEDs are wired in an "active high" configuration, so
* a LOW output will cause the LED to turn off, and a HIGH output will
* allow current to flow through the lamp. Sometimes, packages of LEDs
* will have a single ground pin for all of the diodes, called a "common
* cathode." These work on this same HIGH=ON, LOW=OFF principle. You
* just have to connect all of the positive legs of the diodes to their
* respective output pins, and the common cathode goes to the ground.
*
* Other packages use the opposite wiring plan, offering a "common
* anode." You have to wire the shared pin to the +VDC source, and put
* the resistors and diodes below the source. To light the lamp, you set
* the output LOW, and to extinguish the lamp, you set the output HIGH.
* Signals which do useful things when given a low value are sometimes
* called "active low."
*
* Wire a common-anode RGB LED package such that the anode connects to
* the Arduino's +5V pin, and the other pins go to three PWM outputs to
* allow for fading: digital output pins 3, 5 and 6. Don't forget to
* put a resistor in series with every LED, or you'll burn it out!
*
* Ed Halley <ed@halley.cc>
*/
// Select which PWM-capable pins are to be used.
int redPin = 5;
int greenPin = 6;
int bluePin = 3;
// A function to fade UP an active-low or common-anode output.
// Optionally, specify a short delay value to slow the loop.
// (This would fade down an LED that was active high.)
//
void fadeUp(int pin, int d = 10)
{
int i;
for (i = 255; i >= 0; i--)
{
analogWrite(pin, i);
delay(d);
}
}
// A function to fade DOWN an active-low or common-anode output.
// Optionally, specify a short delay value to slow the loop.
// (This would fade up an LED that was active high.)
//
void fadeDown(int pin, int d = 20)
{
int i;
for (i = 0; i <= 255; i++)
{
analogWrite(pin, i);
delay(d);
}
}
// Set up our outputs, and give full high values so the
// LEDs start off. Then fade in the blue LED.
//
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
fadeUp(bluePin);
}
// The cycle of ramps will go through all of the primary
// and secondary hues in the RGB rainbow, and repeat.
//
void loop()
{
fadeUp(greenPin);
fadeDown(bluePin);
fadeUp(redPin);
fadeDown(greenPin);
fadeUp(bluePin);
fadeDown(redPin);
}
Just wanted to thank you for this wonderful tutorial! I just got my Arduino and found your explanation on hooking up the anode and cathode to be very helpful for a beginner like myself! Cheers!
The self styled internet police are still alive and kicking I see. Seems to me whether it is offensive should be decided and commented on by the person to whom it is intended.
I see Australia is blanketed by mass media demanding action on internet content which really is just an excuse for censureship of the media and you really don't have much hope of finding out who is pushing all the hype. It often is the Government itself
I would suggest you think hard and long before you commit yourselves to support that..
What are your thoughts on that people ?
Halley, you have taught me something valuable: I've had to deal with the problem of devices having an 'inverted input requirement', if you will, in a few cases. Most recently (and both in the same project), I wired up some common anode RGB LEDs as you did and fretted for a couple hours on why the colors were so weird. I also have a fan with a PWM input wire. In both cases, a LOW yields 100% output and HIGH is OFF. I ended up doing all the calculations normally, but then flipped the output values with outputPWM = map(outputPWM,0,255,255,0);
I assumed this was a n00b issue that "everyone else already knew how to do" and I just needed to get through it and move on. But it seems that no matter what the challenge is - small or large - chances are someone else needs to know too.
EDIT: HA! I just noticed this post is over a year old. :
I do hope my "first impression" NEDM64's comment on "post pics" was correct , but as an old Grandpa who has pics of grand-kids in my radio/computer room and enjoy looking at other folk's pics - I took it as a statement of simple enjoyment of grand-kids.
After reading followup posts I can understand how it could be taken as in-appropriate. In light of his follow up statement, I will assume my original impression was correct - hope so anyway.
[edit]I just noticed the original post was over a yr old - well, valid anyway[/edit]
Ken H>
Halley, great example. I am a newbie and just getting started. One of my first projects is to play with an RGB LED and this is just what I was looking for. Thanks,