Using One GPIO to Control Two LEDs with Four States
It's common in embedded systems to encounter a shortage of GPIO resources. To maximize the utilization of GPIO pins, this section explores how to control two LEDs and display four states using a single GPIO pin.
Key Characteristics of LEDs
- Unidirectional conductivity
- Threshold voltage: an LED only conducts above a certain forward voltage, which varies by material
Parallel Configuration
- High-impedance state: both LEDs are off
- High level: red LED turns on
- Low level: yellow LED turns on
- Square wave signal (>100 Hz): both LEDs alternate and appear simultaneously lit due to persistence of vision
Limitation: With a 5V power supply, only low-threshold LEDs can be effectively driven.
Series Configuration
- High-impedance state: both LEDs are off (total forward voltage exceeds supply voltage)
- High level: blue LED lights up
- Low level: green LED lights up
- Square wave signal (>100 Hz): both LEDs appear lit
Summary
- If total LED forward voltage > supply voltage ¡ú use series
- If total LED forward voltage < supply voltage ¡ú use parallel
LED Driving Techniques
Using NPN and PNP Transistors
When GPIO drive strength is low, one NPN and one PNP transistor can be used to drive two LEDs alternately.
Using Two NPN Transistors
Two NPN transistors can provide inverted signals at their collectors, allowing control of two LEDs with alternating behavior from a single GPIO.
Direct GPIO Drive
Two LEDs connected directly to a GPIO¡ªone pulled up, the other pulled down¡ªcan light up based on high or low output levels.
Driving with Two NPN Transistors and Two GPIOs
If higher drive current is needed, two GPIOs and two NPN transistors can be used to drive different colored LEDs for scenarios like standby and active indicators.
Typical LED Forward Voltages
- Red: 1.6¨C2.0 V
- Green: 2.2¨C2.5 V
- Blue: 2.5¨C3.1 V
Note: The 2014 Nobel Prize in Physics was awarded to the inventors of the blue LED, enabling modern energy-efficient lighting.
Using Two GPIOs to Control Three LEDs
Using combinations of two GPIO states, it¡¯s possible to control three distinct LED states via different wiring methods.
- Low + Low: all LEDs off
- Low + High: LED2 or LED6 on
- High + Low: LED3 or LED5 on
- High + High: LED1, LED4, or dual LEDs on
By adjusting wiring or LED placement, a variety of control logic configurations can be implemented.
Using Three GPIOs to Control Six LEDs
This technique uses all three GPIO states¡ªhigh, low, and high-impedance¡ªto control six LEDs from just three GPIO pins. High-impedance means the pin effectively disconnects from the circuit, allowing current routing control.
LED Control States
Each LED is lit by setting a unique pair of GPIOs to high and low, with the third set to high-impedance. This results in six possible combinations.
Scalability
- 2 GPIOs ¡ú 2 LEDs
- 3 GPIOs ¡ú 6 LEDs
- 4 GPIOs ¡ú 12 LEDs
- n GPIOs ¡ú n¡Á(n?1) LEDs
Charlieplexing Principle
This multiplexing method takes advantage of:
- Three GPIO states: high, low, and high-impedance
- LED unidirectional conduction
Each pair of GPIOs connects two LEDs in opposite directions. To light a specific LED, configure the associated pins to high and low, and leave the rest in high-impedance.
Two GPIOs Example
With only high and low states, two GPIOs can still drive two LEDs by alternating direction.
Three GPIOs Example
Each GPIO pair includes two LEDs with opposing polarity. All six LEDs can be independently controlled.
Design Considerations
- Ensure MCU GPIOs can source/sink the required current
- Open or short circuits can create incorrect LED behavior or damage the MCU