2 LED with common anode. How do I turn on separately?

I'm a newbie...I have a project with 2 LEDs that have the anode soldered together. I want to control each separately. Is my following technique correct??? I set pin 9 as output and connected to the shared anodes (with resisters). I set one LED cathode to pin 2 as OUTPUT and another LED cathode to pin 3 as OUTPUT. When I set pin 9 to HIGH, both LEDs turn on. To turn each off, I set pin 2 to HIGH or pin 3 to HIGH. Is there a better way? Thanks.

I can't follow word schematics.

An LED can be connected between ground and an output pin. Setting the pin HIHG will illuminate it.

Or you can connect the LED between Vcc and an output pin. Setting the pin LOW will illuminate it.

As long as you also have a series current-limiting resistor in the path, neither is any better than the other.

So you should be able to use two output pins no matter whether the LEDs are wired together at the anode or the cathode.

a7

2 Likes

Yes

Connect the common anodes to 5V and each cathode to an Arduino pin via its own resistor. Set the pinMode() of the 2 pins to OUTPUT

Control each LED by changing the state of the pins. HIGH will turn the LED off and LOW will turn it on

1 Like

Welcome to the group !

  • Before asking your questions, please read all the posting guidelines, follow these guidelines when you post your questions.
1 Like

Thanks everyone. This confirmed what I had in mind. I appreciate the comments.

Thanks....better approach by using 5V to the anodes. Saves a pin. And this confirms that I can set the pins to HIGH or LOW to control. Appreciate the help.

Thanks. This approach saves me a pin. Appreciate the comment.

.......then you run the risk of damage to the Arduino by exceeding the pin max current.

1. Yes! It saves one DPin; but, it is at cost of two MCU instructions to turn off the LEDs,

2. If using DPin-9 to hook the ca-pin, then the following single line code is enough to turn off both LEDs:

digitalWrite(9, LOW);

3. If using 5V for the ca-pin of the LEDs, then the following two lines are needed to turn off both LEDs.

digitalWrite(2, LOW);
digitalWrite(3, LOW);

4. However, using 5V for the ca-pin is recommended as, if you wish, you can connect more LEDs in parallel without any shortage of driving current for the LEDs.

If connecting DPin-9 at the ca-pin of the LEDs, you may not be able to connect further LED in parallel as DPin-9 can at best source 20 mA current which is good enough to drive two LEDs. (One LED takes about 10 mA - 15 mA current, with ~= 2.5 V drop across it, to emit good amount of light.)

To GM and Bluejets: Thanks for the follow-up info. I was worried that I might damage the arduino by drawing too much current. Will use that 5V and not pin 9.

  • Nick
1 Like

You will still sink the current to arduino pin, so it really doesn't change anything. In both cases you need series resistors to protect Arduino (and LED).

1 Like

Someone else might say a typical Vf is 1.8 volts, and for any LED worth paying for, 7 mA might be quite enough current.

Takeaway: LEDs are different, they have specifications which can be engineered against and the only real test of what current will make what light and exhibit what Vf is just that: a real test with the LEDs you happened to grab from wherever.

I use 470 ohm resistors, measure about 7 mA current and see a nicely bright lamp at 1.7 volts across the LED. At least that's what I am getting out of the big bag of 470 ohm resistors I have and the bigger bag of all-the-same but jellybean red LEDs.

Which reminds me to add different colours of LED will have very different Vfs.

a7

1 Like
  • Add a 220 ohm resistor in series with a 5k potentiometer in series with the LED, apply voltage.
    Adjust the potentiometer until you reach the desired brightness.
    Measure the value of the 220 ohm resistor and the potentiometer.
    Use a standard value resistor close to the measurement.
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.