Can somebody please explain wiring of RGB LED

I used an ordinary LED wired as per this schematic without any problem;

Next I tried to use an RGB LED wired as per the next schematic. The RGB LED was supplied with an Arduino and the instructions were to attach the common leg of the LED to +5V and the other three legs to pins 9, 10 and 11 via 270ohm resistors;

My problem is that I just don't understand why the two circuits are so different.
In the first it looks (to me) as is the digital output pins are +Ve as they connect to the LED then via the resistor to GND.
In the RGB case it looks (to me) as if the pins are -Ve, because the common end of the LED is attached to +Ve.
Also the diode symbols in the two diagrams appear to indicate current is flowing in different directions, how can this be since the pins are always outputs?

Do I have to program or configure the output pins in some different way to use the RGB LED?

A simple explanation for a non-electronic person would be appreciated.

If you had purchased a common-cathode RGB LED, then the circuit will be identical to the single LED case. In your case, since current only flows from anode to cathode, you have to output LOW (pulling pin to ground) to direct current from anode to cathode. Output doesn't necessarily mean current flow out of it. Output means you can change the pin between HIGH and LOW as you like. If you direct the output to HIGH in the single LED case, you are sourcing current from the output, flowing current from the pin through the LED. If you direct the output to LOW in the RGB LED case, you are sinking current to the output, meaning flowing current to the pin through the LED. Arduino pins can both source and sink when they are outputting. I hope that is clear enough for you :slight_smile:

Hello liudr,

Your explanation was very clear. I modified my code and now my sketch functions as I wished, not only that but I think I have a basic understanding of why it works so you have taught me a lot.

If I am understanding correctly the “arrow” of the diode symbol is pointing in the direction in which positive current can flow.
It appears that by convention the direction of electric current is opposite to the direction of electron flow.
So the “anode” terminal of the LED is the terminal where positive current may flow into the LED from outside.

In the simple LED case:
When I put my output pin HIGH it is positive with respect to ground and positive current flows from the output pin into the anode of the LED towards ground in the direction of the arrow and turns the LED on.
If I were to make my output LOW, pulling my output pin down to ground (or even if I were to make it slightly negative), my LED would be off as positive current cannot flow against the direction of the arrow.

In the RGB LED case:
When I put my output pin LOW it is negative with respect to the +5V and positive current flows from the +5V into the anode of the RGB LED towards the output pin in the direction of the arrow and turns the RGB LED on.
If I were to make my output HIGH, pulling it up to +5V (or even slightly more +Ve), my RGB LED would be off as positive current cannot flow towards my output pin against the direction of the arrow.

There is a slight practical problem in using the common anode RGB LED because initially the outputs will be low causing the LED to give a brief flash until they are set high (off) in setup. I cannot think of a way round this.

radman,

You are a good learner! Anyone will enjoy sharing their knowledge with you.

Comments on the current not flowing: in the simple LED case, when you set output to LOW, both ends of the LED will be at the same voltage, (GND) so there won't be any current even if current could flow against the arrow on the diode. That applies to the common anode LED too.

I think, if you want the LEDs to be off before your program runs, you could add a simple inverter (an NPN transistor). This way when your pin is low, the inverter outputs high and that prohibits the LED from getting lit. It's more parts but it also allows you to pass more current through the LEDs than you can with an arduino pin, as long as the LED is able to function with that amount of current.

Here is an example:

You will need 2n2222 (npn transistor), a 10kohm for R1 and a 1kohm for R2. The Vin hooks up the the arduino pin that used to directly drive the LED. The Vout then replaces the role of an arduino pin to drive the cathode of the LED.

radman:
If I am understanding correctly the “arrow” of the diode symbol is pointing in the direction in which positive current can flow.
It appears that by convention the direction of electric current is opposite to the direction of electron flow.
So the “anode” terminal of the LED is the terminal where positive current may flow into the LED from outside.

You can blame Benjamin Franklin for this.

If he named electrons electons, then we can all have protrons, positons, cyclotons and synchrotons. :smiley:

radman:
Hello liudr,
There is a slight practical problem in using the common anode RGB LED because initially the outputs will be low causing the LED to give a brief flash until they are set high (off) in setup. I cannot think of a way round this.

The solution is to digitalWrite a HIGH to the pin before you call pinMode to make it an output.

"40 - 50" what?
40 mA is the ABSOLUTE maximum per-pin current.
You shouldn't be going near this figure.

Posting vague, unit less numbers is a complete waste of time.

The solution is to digitalWrite a HIGH to the pin before you call pinMode to make it an output.

Thanks dc42 doing the write before making the pin an output does eliminate the brief flash. I did not think there would be a programming solution so that is one to remember :slight_smile:

liudr's electronic solution is also of interest Ing Electronica: Transistor Logic Inverter. Although it is a step too far for me at the moment I would like to get the circuit clear so that I can come back to it later.

You will need 2n2222 (npn transistor), a 10kohm for R1 and a 1kohm for R2. The Vin hooks up the the arduino pin that used to directly drive the LED. The Vout then replaces the role of an arduino pin to drive the cathode of the LED.

Here is my attempt at a schematic for the circuit;

Have I got it right (I just wired one color for simplicity)?
What I have not connected is Vcc and the third leg of the transistor (the emitter ?) what should these go to?

I don't need to make the LED brighter so I have kept the 270ohm resistor going to the LED.
Will that be sufficient to protect the LED by limiting the current?

That transistor solution seems like overkill to me.
If you want the LEDs off while the ATMega starts up, just use a 10K pullup resistor on each cathode.
On reset, all IO pins are inputs per the Atmel datasheet, the pullup resistor will keep the output high until you call

pinMode (ledR, OUTPUT);
digitalWrite (ledR, HIGH);

during setup.
The arduino only needs 0.5mA of its safe drive low capability, the other 19.5mA will sink the LED current.
With a 2V Vforward LED, and 270 ohm resistor: (5V-2V)/270 ohm = the LED is only seeing 11mA, so the IO pin is well within safe limits.

IMO even a pullup is overkill for driving a LED. After a reset, the pins default to inputs and will not sink any significant amount of current. The brief flash that @radman saw was probably due to the time delay between changing the pin to an output and setting it high, that is why I suggested setting it high before making it an output.

I guess the software way is the way to go. The transistor is overkill :open_mouth:

But if you want to try, the "?" is GND, and the "Vcc?" is Vcc.

The brief flash that @radman saw was probably due to the time delay between changing the pin to an output and setting it high

I confirmed this was the case by inserting a delay after making the pin an output and the LED came on fully. Setting the pin high before making it an output is the simplest solution - thanks DC42

I am still interested in @liudr's solution though because it gets me into using a transistor and making an inverter, but I am still not sure of the circuit. My Uno board has no pins on it marked Vcc. Is Vcc the 5V pin, which the anode of the LED is already connected to?
Present circuit is;

Also the board has three GND pins. Two are together in a section marked "power" the other is at the end of the digital pins. Am I correct that these grounds are all the same or are there separate grounds for power and for I/O?

Yes, Vcc and +5v are the same, and the 3 GND pins are connected together on the board.

You don't need resistor R2, and at 10K the value of R1 is on the high side, but your circuit is basically correct.

dc42:
You don't need resistor R2, and at 10K the value of R1 is on the high side, but your circuit is basically correct.

In this diagram you will need R2 but if your connect the transistor's collector directly to the 270ohm resistor then you don't need an R2. In this case, lose the VCC and R2 together.

You could eliminate any pullups or down or transistors if you have an extra pin, instead of getting 5v from another pin set to high
then when you startup both pins are inputs and no current flows until you make the source pin high and the sink pin low,