5
\$\begingroup\$

I have a green LED and a red LED. I want a sort of switch to turn either the red LED or the green LED on. For example:

Where if the switch is off, the red LED is on but if the switch is on, the green LED is on.

enter image description here

\$\endgroup\$
4
  • 5
    \$\begingroup\$ Use a changeover switch aka SPDT and make sure each LED has a resistor in series. \$\endgroup\$
    – Andy aka
    Nov 23 '20 at 18:52
  • 2
    \$\begingroup\$ one resistor is enough if you have the changeover switch. \$\endgroup\$
    – user287001
    Nov 23 '20 at 19:03
  • 5
    \$\begingroup\$ @user287001 I would recommend separate resistors. The forward voltages are pretty different, and red LED's tend to appear very bright compared to green with the same current. The red LED will likely need a much larger resistor then green. \$\endgroup\$
    – mkeith
    Nov 23 '20 at 21:15
  • \$\begingroup\$ I'm surprised only one person has brought up the trick that exploits the different forward voltages. If you put these LEDs in parallel, the red one will turn on; disconnect the red one, and the green one will turn on. (Note: may need to check your specific LED datasheets - but that answer also added a diode to the green one to increase it further) \$\endgroup\$
    – user253751
    Nov 24 '20 at 11:34
5
\$\begingroup\$

A collection from another thread, posted here for the middle schematic. Pin 6 would be the GPIO pin. For better brightness matching, swap the red and green LED positions.

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ Circuit 2 (or 3) is a trick to use less parts, by exploiting the fact that the green LED has a higher forward voltage, and adding another diode to make it even higher. Good idea. \$\endgroup\$
    – user253751
    Nov 24 '20 at 11:33
  • \$\begingroup\$ Please provide a link or citation for the "another thread" where you found these images. We need to give credit to the original creator. \$\endgroup\$ Nov 25 '20 at 14:22
  • \$\begingroup\$ I am the original creator. Those a re my schematics. \$\endgroup\$
    – AnalogKid
    Nov 25 '20 at 14:30
12
\$\begingroup\$

You mention an IO pin; this is how you could do it with one IO pin if you are using a microcontroller (assuming one that uses 5V logic):

schematic

simulate this circuit – Schematic created using CircuitLab

When the IO pin is 5V, D2 will light up; when the IO pin is 0V, D1 will light up.

Note that you will have to calculate the actual resistor values; different types and colours of LED require different resistors. There are plenty of examples about that show how to calculate these values.

Also, make sure you don't sink/source too much current; the maximum current an IO pin can handle varies from microcontroller to microcontroller.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Note that in this scenario, there are situations where both LEDs will turn on (if the microcontroller is reset, for example) \$\endgroup\$
    – BeB00
    Nov 23 '20 at 21:32
  • \$\begingroup\$ @BeB00: Yes, if the IO pin goes high impedance about 5mA will flow through both LEDs for the LEDs I assumed in the example. \$\endgroup\$
    – ocrdu
    Nov 23 '20 at 21:39
  • 1
    \$\begingroup\$ @BeB00 That could be considered a feature! Set the pin to input if you want to turn on both LEDs. \$\endgroup\$
    – Hearth
    Nov 24 '20 at 19:23
  • \$\begingroup\$ But you can't turn off both LEDs with this solutions without removing power. \$\endgroup\$
    – NStorm
    Nov 25 '20 at 9:47
  • \$\begingroup\$ @NStorm: That's another feature; it doubles as a power-on indicator. But seriously, I don't think turning off both LEDs and have independent currents while power is on is doable with one IO pin without extra components. Easy with two IO pins, though. \$\endgroup\$
    – ocrdu
    Nov 25 '20 at 13:59
5
\$\begingroup\$

schematic

simulate this circuit – Schematic created using CircuitLab

You could also try this.

schematic

simulate this circuit

This would also work.

schematic

simulate this circuit

\$\endgroup\$
2
  • 2
    \$\begingroup\$ This is cool but if you are using two IO pins you can just use one to control each LED separately. Seems like that would be more straightforward. Also, I do recommend using different resistors for the two LED's because they have different forward voltages, and also, it will probably be better to run more current through the green LED than the red one. \$\endgroup\$
    – mkeith
    Nov 23 '20 at 22:44
  • \$\begingroup\$ @mkeith You are correct. I changed my answer to reduce it back down to one IO pin. \$\endgroup\$
    – user4574
    Nov 23 '20 at 23:03
4
\$\begingroup\$

On editing your question, I see that what you really want to do is to switch the LEDs from a microprocessor output.

The easiest way to do that is in your program. Use two GPIOs, and always switch one off when the other is on.

If you can't change the program, then you could do something like this:

schematic

simulate this circuit – Schematic created using CircuitLab

That turns the green LED on when the GPIO is high, and it turns the red LED on when the GPIO is low. Only one or the other will be on, never both.


Original answer:

What you need is a single pole double throw (SPDT) switch.

The circuit looks like this:

schematic

simulate this circuit

One LED will always be turned on.

The resistors are not optional. You must have them to keep the LEDs from burning out. There are plenty of posts on this site about calculating the resistor values given the battery voltage, the LED forward voltage, and the LED current - I'm not going to go into that here.

The resistors are useful for a second reason, as well.

Red and green LEDs don't light equally bright for the same current. With separate resistors, you can tweak the current to make the LEDs approximately equally bright.

\$\endgroup\$
1
\$\begingroup\$

enter image description hereGood challenge for other solutions! If one of the two LEDs will always 'on' I suggest another circuit:

A) the two LEDs connected in parallel but in opposite polarity; B) two resistors (chose the value) connected in series between +V and GND; B) one side of the couple of LED is to be connected to the GPIO; C) the other side of the LED couple is to be connected to the middle of the resistors series.

Only if the GPIO turn in 'Z' state, none LED will be light; the will current continue to flow in the two resistors but at a reduced intensity, depending by the sum of the two resistors, surely lower than when a LED is ON.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ I don't think you actually want there to be a direct connection from R1 to R2. You need to break that wire. Which makes this solution almost the same as ocrdu's answer. \$\endgroup\$
    – mkeith
    Nov 23 '20 at 23:01
  • \$\begingroup\$ @mkeith: this avoids the situation that both light up when the GPIO is high impedance. It's the way I have done it when needed. Although it is probably more wasteful of power. \$\endgroup\$
    – user85471
    Nov 24 '20 at 4:55
  • 2
    \$\begingroup\$ It is not just wasteful of power, it constrains the solution space so that the currents are not independent. With the resistor values you have shown, R1, R2 and the supply can be converted into a Thevenin equivalent where Vthev is 2V, and Rthev is 132 Ohms. It is a very strange way to do it. But I guess it could work in some cases. \$\endgroup\$
    – mkeith
    Nov 24 '20 at 5:58
  • \$\begingroup\$ No, no, no! You are wrong. I'm not I that must change my circuit, but are you that need to look at it with more attention. The solution in the ocrdu's answer, the current continue to flow in both LED if you release the GPIO to the 'Z' state. If you pay attention to my solution, this not happens. In my solution the LED cannot be lighted together! In my solution, the only bad thing is that when GPIO is in 'Z' state, a reduced current flows anyway but only through in the resistors in series between themselves. So, the two resistors are to be left as in my design and is easy understand why. \$\endgroup\$
    – umlibero
    Nov 25 '20 at 11:12
  • \$\begingroup\$ There is not energy wasted! The question was: how to light a LED or another alternatively! So, there must be ALWAYS a LED in ON condition and this is the way to sink energy. The case of High Impedance (ASA 'Z' state) is a condition out of the terms of the question, but is a condition that I want declare for correctness: a condition that will never true. So how you can tell that this condition will be a waste of energy? My circuit never falls in this condition if (as declared) one LED at least it's ON. \$\endgroup\$
    – umlibero
    Nov 25 '20 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.