IAN LANG ELECTRONICS

In this article, as the title suggests, we are going to look at  RGB LEDs. Specifically we are going to look at

5mm Pirahna Super Flux LEDs  which you can buy from Oomlout in a pack of three for the surprisingly moderate price of £2:00 a pack. The link on the left takes you to their webpage for it.

 

So, what is it?  It's an LED, or rather three small LEDs under one dome, that can shine Red,Green and Blue (RGB- see there's a reason for everything) at the same time, and if you have those colours shining at various hues at the same time you can make more colours out of them, including white. (But not brown. There's an IEEE medal and probably a fortune waiting for the man or woman who can make an LED that shines convincingly brown.)

 

Below is a photograph of what they look like physically and a schematic of what they look like electrically:

 

RGB LEDS

Go Back Oomlout RGB rgbphoto rgbschem

Immediately you will notice that all three share a common anode (that is, a positive lead) and each has it's own cathode (negative). The next problem comes in deciphering the pinouts for them.

rgbdrawing

So here it is. If you put the diagonal slot down and to the right you'll find that the anode (to which you supply 5V) is bottom left, the red cathode top left, the green top right and the blue bottom left where the cut-out is.

The fact that it is common anode makes it less easy than otherwise. But the problems are not insurmountable, just awkward. Below we look at our sketch and wiring.Let's start off nice and simple with some flashies.

flashiewiring

You'll notice that in the case of set1 and set 2  the cathodes are connected via resistors (one each, this is important as you'll blow them up if you don't ) to pins on the Arduino that are capable of PWM. I tried it on digital pins that are not capable of PWM and the end result was that the Arduino ignored auto-start, and I have no idea why. Best to leave them like this then. The value of those resistors is 270 ohms. I don't think you can get away with any smaller, and being as I've only got three I did not experiment with it- the LEDs in the package are very small, and so 270 ohms seemed sailing a bit close to the wind and I stuck to it. All three anodes are fed from the same power supply which is the 5V pin of your Arduino- they do work on 3.3V as well if you want them a bit less bright. Here's a sketch:

 

int ledset1[]={3,5,6};//RGB

int ledset2[]={9,10,11};//RGB

int ledset3[]={A0,A1,A2};//RGB

String state1="red";

int red []={1,0,0};

int green[]={0,1,0};

int blue[]={0,0,1};

int skipper=0;

void setup(){

  for(int t=0;t<3;t++){pinMode (ledset1[t],1);pinMode (ledset2[t],1);pinMode (ledset3[t],1);}

delay(1000);

for(int j=0;j<3;j++){pinMode (ledset1[j],0);}

delay(250);

for(int j=0;j<3;j++){pinMode (ledset2[j],0);}

delay(250);

for(int j=0;j<3;j++){pinMode (ledset3[j],0);}

delay(2000);

}

void loop(){

if (state1=="red"){for(int t=0;t<3;t++){pinMode(ledset1[t],red[t]);pinMode(ledset2[t],green[t]);pinMode(ledset3[t],blue[t]);}}//ok

if (state1=="blue"){for(int t=0;t<3;t++){pinMode(ledset1[t],blue[t]);pinMode(ledset2[t],red[t]);pinMode(ledset3[t],green[t]);}}

if (state1=="green"){for(int t=0;t<3;t++){pinMode(ledset1[t],green[t]);};for(int t=0;t<3;t++){pinMode(ledset2[t],blue[t]);}for(int t=0;t<3;t++){pinMode(ledset3[t],red[t]);}}

if(state1=="red"&&skipper==0){state1="blue";skipper=1;}

if(state1=="blue"&&skipper==0){state1="green";skipper=1;}

if(state1=="green"&&skipper==0){state1="red";skipper=1;}

delay(500);

skipper=0;

  }

 

Right then- if you've done the wiring and copied, pasted and uploaded that to your board, here's what you'll see. First of all, all the LEDs come on and shine a brightish white, then go off one by one. Then they come on as red, green and blue in that order. Then they start changing colours. At first glance, they look random, but if you watch, there's not one of the three that's the same colour at the same time. This is because they are following a pre-defined sequence. Watch one colour from set1 downwards (I find red easiest) and you'll find that when set1 changes set2 takes on the colour it had. When set2 changes, set3 takes on the colour it had. When set3 changes set 1 takes on the colour it had. Similarly, set1,set2 and set3 takes on the colour of the set before in a change. Let's canter through and see how it's done.

 

First off in the sketch we declare three arrays:

 

 

 

int ledset1[]={3,5,6};//RGB

int ledset2[]={9,10,11};//RGB

int ledset3[]={A0,A1,A2};//RGB

 

These contain details about which pins the cathodes in each set are attached to. It's much easier to work with them if you standardise the notation and I found the easiest way was to note them as red, green and blue (RGB). These arrays are going to be used to tell the Arduino which pin to work. Next is a funny-looking declaration:

 

String state1="red";

 

The way this is going to work is by looking at the current state (i.e which colour) ledset1 actually should be on the first cycle, then making it that colour, then giving the variable the next colour, then on the next cycle and all subsequent repeating the process. So we need this variable to tell the Arduino what to do with ledset1 next. Moreover, the other two sets are going to be affected by the state of ledset1 and so we need some way to measure it. Next come three more arrays:

 

int red []={1,0,0};

int green[]={0,1,0};

int blue[]={0,0,1};

 

These give instruction as to what colour each LED is going to shine. It works by changing pin states between output (1) and input (0)  and a cathode will only find a path to ground in the former case. Therefore, closing two of the paths gives only cathode path to ground, and the cathode path that finds ground (either red or green or blue) is the colour that will shine. The next and final declaration is of a variable of type int which is called skipper:

 

skipper=0;

 

Depending on who you are talking to skipper is known as a check-  flag-  or stop variable. I could in fact have used a boolean type here more effectively, but because I'm Old School I sometimes think in old school ways and I used a flag instead. It works in the same way as a boolean and can have the state 0 or 1 (because that's all the code allows it to have) and will do the same job, which is to lock out sections of code we don't want to operate on particular cycles.

 

On to the setup then. I've gone a long way round here to demonstrate the principle of working the cathodes, so strap yourself in because it's a bumpy ride. First off:

 

void setup(){

  for(int t=0;t<3;t++){pinMode (ledset1[t],1);pinMode (ledset2[t],1);pinMode (ledset3[t],1);}

 

Here's how my wife might react to looking at the above:

 

Wife:-   "Right.Good.Spiffing. Isn't that a load of maths?"

Me:- "No."

W.  "Isn't it?"

M. "No."

W. "Looks like a load of maths."

M." It's a for/next loop of three cycles which will open all three cathodes on each LED set to ground, thus allowing all the LEDs encased within each RGB set to shine, providing a white luminescence."

W. "Oh. Right then."

 

It is at this point that my wife will wander off and read a book in which the central character will probably be called "Milly". Milly will do absolutely nothing for forty pages, and then she will dump her boyfriend who is an accountant/ stockbroker/ architect/ junior manager in a bank  (believe me they are never dustmen, welders or bus drivers), give up her job and embark on a new life somewhere sunny. For two hundred pages Milly will do nothing interesting and then she will meet a new boyfriend and there won't be anything interesting in that either. Old boyfriend will turn up in sunny clime. swearing that he has changed his ways. Milly will spend next fifty pages in dilemma whilst deciding what to do and a beach will be involved somewhere as well as some girl who is called "Caroline" or something similar and who gives astoundingly bad advice. Then Milly will decide she doesn't want either bloke, up sticks and move back home doing the job she used to (undoubtedly something in an office, Milly and her ilk never work in a chip shop) but at a better firm for more pay and will begin dating a nice man from accounts.

 

For those of us who have no interest in Milly and her largely self-wrought tribulations, here's how that line works bit by bit. The for/next loop is assigned to variable t and told to loop as long as t is less than 3 and increment t by 1on each cycle:

 

  for(int t=0;t<3;t++){pinMode (ledset1[t],1);pinMode (ledset2[t],1);pinMode (ledset3[t],1);}

 

If we look in the middle you find pinMode (ledset1[ t ],1);  which in plain English means set whichever pin is specified in the array ledset1 at position t to output. This lets the cathode find a path to ground and the relevant colour to shine. Similarly ; pinMode (ledset2[t],1);pinMode (ledset3[t],1); opens the relevant cathode in the other two sets. If the value of t is 0, all the red cathodes open, if it's 1 all the green and if 2 all the blue. Mixing all the colours in this way produces white, so we get three shiny white LEDs. Now we've got to switch them off one by one. So:

 

for(int j=0;j<3;j++){pinMode (ledset1[j],0);}

delay(250);

 

which switches off the the pins that the cathodes are attached to one by one on the same set and then waits for 1/4 second before going on to ledset2 and doing exactly the same thing:

 

for(int j=0;j<3;j++){pinMode (ledset2[j],0);}

delay(250);

 

Then the final set:

 

for(int j=0;j<3;j++){pinMode (ledset3[j],0);}

delay(2000);

 

in which we wait 2 seconds before going on to the loop, and that's what we look at on the next page.

 

 

 

More>