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: When the switch is off, the red LED is on, but if the switch is on, the green LED is on.
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: When the switch is off, the red LED is on, but if the switch is on, the green LED is on.
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.
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 5 V logic):
simulate this circuit – Schematic created using CircuitLab
When the IO pin is 5 V, D2 will light up; when the IO pin is 0 V, 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.
simulate this circuit – Schematic created using CircuitLab
You could also try this.
This would also work.
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:
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:
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.
Good 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.