9
\$\begingroup\$

I am currentlly running out of IO ports on my Arduino board. Yes, I also have several IO extendor boards. The constraint isn't the number of IO ports, it's the wiring to the remote 'Station' via RJ45/Cat5 cables, which only has eight wires.

To simplify, each 'Station' has two LEDs, requiring two IO ports. These LEDs always toggle. When one is lit, the other is not. Each 'Station' has access to ground and 3.3 V. I would like to know how I can control two LEDs that will always toggle using just one IO port. When the IO is HIGH red is lit and green is unlit, when the IO is LOW, red is unlit and green is lit.

\$\endgroup\$
5
  • 5
    \$\begingroup\$ Connect one LED + resistor between the I/O pin and ground and the other LED + resistor between the same I/O pin and VCC. \$\endgroup\$
    – StarCat
    Commented Aug 21 at 18:22
  • \$\begingroup\$ How much current does each LED use? \$\endgroup\$ Commented Aug 21 at 18:55
  • 1
    \$\begingroup\$ Does the wire that drives the come directly from your Arduino or from GPIO expander? In the latter case, the GPIO expander type is important, as it may cause limitations what kind of circuits are possible, depending on the output type. It may invalidate a lot of the given answers. \$\endgroup\$
    – Justme
    Commented Aug 22 at 5:04
  • 2
    \$\begingroup\$ And nobody suggests a NOT gate? Why not? \$\endgroup\$ Commented Aug 22 at 8:48
  • 1
    \$\begingroup\$ @ThomasWeller, because it requires an unnecesary extra component. Not gates are used in the answer, electronics.stackexchange.com/a/723025/73158. \$\endgroup\$
    – Transistor
    Commented Aug 22 at 15:54

6 Answers 6

19
\$\begingroup\$

schematic

simulate this circuit – Schematic created using CircuitLab

A circuit I have used many times with chips that provide a high/low status output (eg battery charger chips). Note that a bicolour LED can also be used.

Looks similar to Transistor's answer, but avoids the situation where (if the GPIO is not driven) that both LEDs would glow dimly.

When GPIO is high, bottom LED lights. When GPIO is low, top LED lights. When GPIO is floating, neither lights.

\$\endgroup\$
11
  • 1
    \$\begingroup\$ Snap! lednique.com/gpio-tricks/1-gpio-bi-colour-2-pin-led \$\endgroup\$
    – Transistor
    Commented Aug 21 at 22:45
  • 2
    \$\begingroup\$ (Red should need at least 1.6 V, green 1.9 - sums to > 3.3.) \$\endgroup\$
    – greybeard
    Commented Aug 22 at 5:43
  • 1
    \$\begingroup\$ @greybeard Why would you sum the Vf of the LEDs? they are not in series. \$\endgroup\$
    – Blup1980
    Commented Aug 22 at 6:59
  • 1
    \$\begingroup\$ Thanks! Once you see the answer, it became obvious. Here's what I did: RED LED: Anode to GPIO, Cathode to Ground. GRN LED: Anode to 3.3V, Cathode to GPIO. SImple. Elegent. AWSOME! \$\endgroup\$
    – Dan
    Commented Aug 22 at 14:39
  • 1
    \$\begingroup\$ With 3.3V supply, it seems like the resistor middle point would be at 1.65 V and you couldn't use this with anything except red leds. \$\endgroup\$
    – jpa
    Commented Aug 23 at 8:56
18
\$\begingroup\$

enter image description here

Figure 1. Image source (mine) from LEDnique.com.

  • When GPIO is pulled low R1 and L1 conduct.
  • When GPIO is pulled high R2 and L2 conduct.

The schematic shows 5 V but, depending on the LEDs, it will work on 3.3 V.

\$\endgroup\$
20
  • 1
    \$\begingroup\$ You could consider that a feature, e.g. when a microcontroller is initialising or has failed with the GPIO set in a power up default of input (as is common), both LEDs will light indicating an obvious fault condition. \$\endgroup\$
    – rolinger
    Commented Aug 22 at 11:25
  • 7
    \$\begingroup\$ @Graham, we're only trying to light one LED at a time - not both. \$\endgroup\$
    – Transistor
    Commented Aug 22 at 11:31
  • 2
    \$\begingroup\$ @Graham Why? With the GPIO (and VCC) at 3.3V, R2-L2 sees 3.3V and R1-L1 sees 0V; and with the GPIO at 0V, it's the reverse. \$\endgroup\$
    – Sneftel
    Commented Aug 22 at 11:45
  • 4
    \$\begingroup\$ @Dan lemme Guess: Arduino? Just as a note: Never ever (and I mean NEVER) run a LED without some sort of current limiting device. "It works" is no excuse. I know, you're seeing a lot of that crap in Arduino Forums but we're here on Electrical Engineering Stack Exchange and to stuff the right way :D \$\endgroup\$
    – kruemi
    Commented Aug 23 at 6:56
  • 1
    \$\begingroup\$ 3.3V is even an advantage here. With GPIO floating, current would have to flow through both LEDs, and 3.3V is not enough for that. \$\endgroup\$
    – MSalters
    Commented Aug 23 at 9:09
6
\$\begingroup\$

Often, a GPIO pin cannot source as much current as it can sink. If this is the case, here is a trick that relies on sink current only. The center schematic is the one for your application. Your GPIO pin replaces the gate, and is connected directly to D3.

The combined forward voltage of D4 plus D5 is greater than the Vf of D3. When the D3 cathode is low, D3 effectively "shorts out" D4-D5. The same circuit works with the LEDs transposed.

enter image description here

\$\endgroup\$
6
\$\begingroup\$

Basic idea

A minimalistic circuit solution can be implemented with only three elements - a resistor and two LEDs; it is only necessary that the LED forward (threshold) voltages meet the requirements VLED1 + VLED2 > Vcc and VLED1 (VLED2) < Vcc. In the conceptual schematics below, I have modeled the LEDs by forward-biased "ideal" diodes with 2 V threshold voltages.

Conceptual 3.3 V diode circuit

The microcontroller output (not shown in the schematics below) consists of two complementary transistors which may not connect the output to the supply rails (high output impedance) or connect it either to ground (low output voltage) or to Vcc (high output voltage). I have modeled it here in the simplest possible way with a piece of wire.

High output impedance

In this case, the left hand resistor end is unconnected (floating). Although the string of two LEDs in series is connected between the supply rails, both LEDs are off since the overall threshold voltage of the string is higher than the supply voltage. No currents flow in the circuit.

schematic

simulate this circuit – Schematic created using CircuitLab

Low output voltage

Now the resistor is connected to ground; so a current flows through LED1 and it is on.

schematic

simulate this circuit

Middle output voltage

During the transition, when the voltage is between Vcc - VLED1 (1.3 V) and VLED2 (2 V), the circuit state is similar to the high impedance state - both LEDs are off and no currents flow.

schematic

simulate this circuit

We can see it in the DC sweep simulation.

STEP 1_led_3.3V

This state is not essential in such a digital application because the microcontroller output voltage quickly jumps this 0.7 V "dead" zone, but it may have some analog application (LED voltage indicator).

High output voltage

Here the resistor is connected to Vcc; so a current flows through LED2 and now it is on.

schematic

simulate this circuit

All diode versions

Finally, let's investigate and compare the three versions proposed in the answers here.

Version 1: R-LED circuits shunted

Here, two R-LED circuits are connected in series to the power supply. The input voltage source (microcontroller output) shunts one circuit so its current is diverted and the LED is off, and connects the other circuit to the power supply so a current flows and the LED is on.

schematic

simulate this circuit

STEP 2.1

(+) Reliable LED shutdown

(-) Redundant resistor

(-) Requires VLED1 + VLED2 > Vcc

Version 2: LEDs shunted

In this case, only the two LEDs are connected in series to the power supply, and the two resistors are replaced by one common resistor. The input voltage source connects the resistor in parallel to one LED so its current is diverted and the LED is off, and connects the other LED to the power supply so a current flows and the LED is on.

schematic

simulate this circuit

STEP 2.2

(+) Only one resistor

(-) Requires VLED1 + VLED2 > Vcc

Version 3: Back-to-back LEDs

Here, a network of two LEDs in parallel, back-to-back, is connected between the input voltage source and the output of a .5x voltage divider supplied by Vcc. Thus, the right hand end of the LED network is "lifted" with Vcc/2, so the voltage across it tries to change from -Vcc/2 to Vcc/2 (but actually is limited to the voltage of the forward-biased LED).

Unsuccessful 3.3 V solution: Unfortunately, the (1.65 V) voltage across the forward-biased LED is insufficient to turn it on.

schematic

simulate this circuit

STEP 2.3.1

Successful 5 V solution: So, this configuration needs a 5 V power supply. Then a 2.5 voltage produced by the voltage divider is applied to the 2 V forward biased LED (the current is limited by the Thevenin divider resistance).

schematic

simulate this circuit

STEP 2.3.2

(+) Reliable LED shutdown

(-) Redundant resistor

(-) Requires VLED < Vcc/2

See also another related answer of mine.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Did you see some feature of this entire question that isn't a duplicate? When you find yourself copying this much of an existing answer, should set the "it's a dupe" spidey-sense tingling. \$\endgroup\$
    – Ben Voigt
    Commented Aug 22 at 21:24
  • \$\begingroup\$ @Ben Voigt, this is a 3.3 V version with adapted schematics, graphs, and text, and a new section added. At the end of my answer, I have attached a link to the original 5 V answer. So, there is nothing hidden. I would appreciate it more if you commented on the substance of the written. \$\endgroup\$ Commented Aug 23 at 4:24
4
\$\begingroup\$

Here is yet another scheme that could be useful with two-pin bipolar LEDs. The IC could be 1/3 of a 74HC14 hex ST inverter or something like a dual inverter.

schematic

simulate this circuit – Schematic created using CircuitLab

Input High = Red

Input Low = Green

Input High-Z = Yellow

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Interesting... Looks like (at input H-Z) the left HC14, C1 and R1 form a relaxation oscillator (due to HC14's hysteresis); so the yellow is a result of the fast switching between the red and green? \$\endgroup\$ Commented Aug 23 at 17:04
  • \$\begingroup\$ @Circuitfantasist Yes, exactly. \$\endgroup\$ Commented Aug 23 at 17:10
2
\$\begingroup\$

1 GPIO, 2 LEDs

For a single GPIO pin, there are two basic circuits. The first one uses two resistors and lets you light D1 or D2 alternatively, but they cannot be both turned off. The second circuit turns the LEDs off when GPIO is switched from output to input (i.e. becomes Hi-Z, high impedance).

GPIO LED GPIO LED
0 D1 0 D3
1 D2 1 D4
Z off

In either circuit, both LEDs can appear lit "simultaneously" by driving a square wave onto GPIO

schematic

simulate this circuit – Schematic created using CircuitLab

2 GPIO, 4 LEDs

Two GPIO pins can control 4 LEDs, including the OFF state.

GPIO1 GPIO2 LED
0 0 D1
1 0 D2
0 1 D3
1 1 D4
Z 0 or 1 off

schematic

simulate this circuit

The tristate detector outputs an active-low signal when GPIO1 is tri-state. See Tri-State Detection for more ideas.

schematic

simulate this circuit

2 GPIO, 8 LEDs

Fully utilizing 3-state logic lets us control 8 LEDs + an off state, since 32=8+1.

GPIO1 GPIO2 LED #G1TRI #G2TRI
0 0 D1 1 1
1 0 D2 1 1
0 1 D3 1 1
1 1 D4 1 1
Z 0 D5 0 1
Z 1 D6 0 1
0 Z D7 1 0
1 Z D8 1 0
Z Z off 0 0

schematic

simulate this circuit

\$\endgroup\$
2
  • \$\begingroup\$ Isn't it better in the transistor circuits to swap the base and emitter (CE configuration)? Because in this CB configuration, the LED current flows into/from the microcontroller output (as in diode circuits) and the transistor role becomes meaningless... \$\endgroup\$ Commented Aug 24 at 6:01
  • 2
    \$\begingroup\$ @Circuitfantasist The transistor pair acts as switches that are both off when GPIO is floating (Hi-Z). It’s OK for the microcontroller to drive the LEDs, they don’t need much current. With transistors, a Hi-Z condition will keep both LEDs off. I have not looked yet at how the CE circuit would work. The CB switch is a common building block though. It has an arbitrarily low forward voltage drop - it can be configured to be in mild saturation and retain high speed while not slowing down the logic. Good for logic level translators and current switches (ECL-style a bit). \$\endgroup\$ Commented Aug 24 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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