Tuesday, December 18, 2012

Driving Two LEDs With One Microcontroller Pin

So you've designed your latest widget and you want to add some status indicators, but you've only got one pin left on your micro-controller to drive a couple of LEDs, what do you do?  There are a couple of options you can try, each with their own pros and cons.  The first option is simple, and will suffice for most situations.

LED Circuit
2 centre driven LEDs

The centre driven arrangement is an easy way to drive two LEDs from one pin.  R1 and R2 are selected to limit the current through D1 and D2 respectively.  Vin is connected to a micro-controller pin which typically has three states, output high, output low, and high impedance input which effectively disconnects Vin.  What happens to the LEDs in each of these cases?

  • When Vin is driven high, current will flow through D2 but not D1, lighting D2.
  • When Vin is driven low, current will flow through D1 but not D2, lighting D1.
  • When Vin is disconnected, current will flow through R1, R2, D1, D2.  In this case the forward voltage of both LEDs, and the increased total resistance will cause the current through the LEDs to be reduced, this means that the light output from the LEDs will be reduced, giving a dim glow.

With this arrangement you can drive either LED, turning on one or the other, but what if you want to turn off both LEDs?  Well, you can't.  The best you can do is set the micro-controller pin as an input and have both LEDs dimly lit.  However, by adding one resistor to the circuit above it's possible to turn on either LED or turn them both off.

LED Circuit
2 centre driven LEDs with resistor bypass

The circuit basically operates as before, but in this case when Vin is disconnected the small current that flowed though D1 an D2 causing them to dimly light, is now shunted though R3.  Component selection is critical though.  The voltage across R3 has to be sufficiently low, otherwise current would start to flow through D1 and D2.  Working out the required resistor values isn't too complicated, but three parameters need to be defined for each LED.

  • Vhigh, the operating voltage of the LED when fully lit.
  • Vlow,  the highest voltage across the LED that won't cause any discernible illumination.
  • On current, the operating current of the LED when fully lit.

With these values the circuit can be analysed for each possible input condition.  The expression for each result has been rearranged to give a formula for a particular resistor.  The reason for this will become clear later on.

Nodal analysis of an LED circuit
Vin is driven low

When Vin is low, current will flow though D1, R1, R2, R3, but not D2 as it will be reversed biased.

Nodal analysis of an LED circuit
Vin is driven high

When Vin is high, current will flow though D2, R1, R2, R3, but not D1 as it will be reversed biased.

Nodal analysis of an LED circuit
Vin is disconnected

When Vin isn't connected we want the voltage across R3 to be equal to the combined low voltages of D1 and D2.  This will ensure that they don't turn on.  The circuit could be designed to use a smaller resistor, but doing so would increase the current in all three states.

The problem is now defined by three equations in three unknowns R1, R2, R3.  Algebraic substitution could be used to solve the system of simultaneous equations, but the problem is equally solvable numerically.  The first step is to calculate approximate values for R1 and R2,  This can be done by subtracting the Vhigh value for the LED from Vcc and dividing the result by the On current.  These values of R1 and R2 are then used to calculate R3.  An iterative process is used where you use the value of two resistors to calculate the value of the third.  After a couple of iterations a well formed problem will quickly converge to an answer.

To assist with these calculations I have put together a spreadsheet that allows you to enter the LED parameters and Vcc to obtain values for R1, R2, and R3.

2LEDsResistorCalculator.ods

To demonstrate the process I assembled a small circuit with parts I had on hand.  The parameters of the LEDs I had were as follows.

LED 1 - Red
  • Vhigh  -  1.89 V
  • Vlow  -  1.35 V
  • OnCurrent  -  1 mA
LED 2 - Orange
  • Vhigh  -  1.76 V
  • Vlow  -  1.40 V
  • OnCurrent  - 1.5 mA
Entering this information into the spreadsheet returned three resistor values.  I didn't have these exact values, but using a combination of other resistors I was able to obtain the values in the brackets.
  • R1  -  2462 ohm  (2350 ohm)
  • R2  -  1876 ohm  (1950 ohm)
  • R3  -  5302 ohm  (4700 ohm)
Entering the values of the actual resistors I used gave the following results.

circuit measurements
Simulation results

The circuit was then assembled and the operating values measured.

LED circuit on a breadboard
Input disconnected. Both LEDs off
LED circuit on a breadboard
Input high.  LED 2, Orange On
LED circuit on a breadboard
Input low.  LED 1, Red On

  • LED1 on current  -  1.07 mA
  • LED2 on current  -  1.46 mA
  • Supply current LED1 On  - 1.36 mA
  • Supply current LED2 On  - 1.70 mA
  • Supply current LEDs Off  - 0.56 mA
The circuit operated correctly and the measured values agreed well with the calculations in the spreadsheet. 

LED circuit on a breadboard
Bypass resistor removed.  Input disconnected.

To illustrate the importance of the bypass resistor, R3, I removed it from the circuit and disconnected the input.  Without the bypass resistor the current passes through the LEDs illuminating both at once.

This circuit is reasonably flexible.  You can turn off both LEDs, turn on either, and if you wanted both lit up at once, just toggle the input between low and high.  The only downside to this circuit is that there is always a current flowing.  Even when the LEDs are off there's a small current, which may make this unsuitable for any type of battery powered device.  Ideally you should plan ahead and make sure you have enough output pins to control any indicator LEDs, but if you're in a jam, or just don't want to move up to the next device with a higher pin count, this will do the job.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.