I only have 2 (3-state) pins available on a microcontroller and need to control 3 red LEDs, uC is running on 5V. I can only use passive components in addition.
To control four LEDs:
To get both LEDs on a particular pin lit, toggle the pin at a few hundred Hz.
Note that this setup requires that the forward voltage of the LEDs be less than Vcc/2.
Note also that the resistors consume power all of the time, not just when the LEDs are on.
This solution depends on the fact that 5V won't light the three LEDs in series. If necessary, you can add a silicon diode in series with one or more of the LEDs in order to increase the total forward voltage drop.
- If both pin A and pin B are tristate, all LEDs are off.
- If pin A is driven low, D1 is on.
- If pin A is driven high and pin B is driven low D2 is on.
- if pin B is driven high, D3 is on.
To have more than one LED lit at a time, you'll have to multiplex: turn them on one at a time, rapidly enough so that they appear to be continuously lit.
With two pins you can actually control four LEDs. To control two LEDs put a resistor from the MCU pin to limit the current and tie it to two LEDs - the cathode of one LED and the anode of the other LED. The free anode end of the first LED gets connected to the VCC of the MCU board through another resistor. The free cathode end of the second LED gets connected to GND (through another resistor). Now the LEDs will light alternately when the MCU pin is set high or low. Wire up the second MCU pin the same way and you now have control over a total of four LEDs.
To give the appearance of independent control of the each LED in the pair off the one of the port pins requires a little software work as follow.
If LED1 is the one with the anode to the port pin and LED2 is the one with the cathode to the port pin then do the following to establish the four states for two LEDs.
LED2 LED1 Action OFF OFF Set the port pin to tristate level. OFF ON Set the port pin low. ON OFF Set the port pin high. ON ON Toggle the port pin high and low at a frequency over about 120 Hz.
Repeat the same actions for the other port pin and you will be looking like you have four independent LEDs off two port pins.
This scheme works well for LEDs that have a forward voltage drop that is over half the VCC level. Red LEDs with a 2.1 VF will not work so great if the VCC is 5V for example. On a otherhand a green LED with a 2.5V VF will work great on a system with a VCC of 3.3V.
EDN published some related Ideas for Design here
Here's one of them:-
For typical values with D1 a yellow LED (2.2V on), D2 a red LED (1.9V on), and off voltages of 1.2V and 1.1V respectively, and on-currents of 8mA each, Vcc = 5.0V, the optimum values are
R1 = 300 ohms R2 = 330 ohms R3 = 1.2K ohms
Quiescent current is 2.7mA. To have both LEDs appear to be illuminated, toggle the output pin at 100Hz or greater.
I used the Excel solver in the original article, the code may be still available from EDN.
The additional degree of freedom afforded by the resistor R3 can avoid the limitations of both Dave Tweed and Michael Karas' circuits, though for the specific case of 2 red LEDs operating from a 5V supply, Dave Tweed's circuit is probably acceptable, but check the Vf carefully, it's not fine for some red LEDs, and may be marginal for others if the port pin doesn't pull all the way down or up.