Two LEDs from a Single Pin

Status
Not open for further replies.

Wozzy

Well-known member
I saw someone lamenting on here the other day about not having enough pins for LEDs.
It reminded me a trick I learned from back in my PIC chip days.

One can use the fact that Teensy has tri-state pins to control 2 LEDs from a single pin.
You need to use a PWM capable pin if you need the case where both LEDs are ON.

Here's a diagram That shows how to hook up the LEDs:
Capture1.JPG
The additional diodes drop the voltage slightly to keep the LEDs from dimly glowing when in the off state.

Here is the Code that will control the Pin Modes to individually light the 2 LEDs
Code:
//  This example code is in the public domain.

const int ledPin = 20;   // MUST USE A PIN THAT IS PWM CAPABLE (FOR BOTH LED A & LED B ON CASE)

void setup() {

  analogWriteResolution(8);                  // SET PWM TO *8 BITS RESOLUTION
  analogWriteFrequency(ledPin, 187500);      // 187500 IS IDEAL FREQUENCY FOR 8-BIT RESOLUTION WITH TEENSY 3.X at 96MHz)
  Serial.begin(9600);                        // START THE SERIAL PORT
  while (!Serial && (millis ()  <=  5000));  // WAIT UP TO 5000 MILLISECONDS FOR SERIAL OUTPUT CONSOLE
}

void loop() { 
  pinMode(ledPin, OUTPUT);          // SET PIN TO OUTPUT MODE
  digitalWrite(ledPin, HIGH);       // PIN IS SOURCING 3.3 V
  //analogWrite(ledPin, 255);        // (ALTERNATE) SET PWM OUTPUT TO 100% DUTY CYCLE
  Serial.println("LED A ON   LED B OFF");
  delay(1000);
  
  pinMode(ledPin, OUTPUT);          // SET PIN TO OUTPUT MODE
  digitalWrite(ledPin, LOW);        // PIN IS SINKING 3.3V
  //analogWrite(ledPin, 0);        // (ALTERNATE) SET PWM OUTPUT TO 0% DUTY CYCLE
  Serial.println("LED A OFF  LED B ON");
  delay(1000);
    
  pinMode(ledPin, OUTPUT);        // SET PIN TO OUTPUT MODE
  analogWrite(ledPin, 127);       // SET PWM OUTPUT TO 50% DUTY CYCLE
  Serial.println("LED A ON   LED B ON");
  delay(1000);
   
  pinMode(ledPin, INPUT);          // SET PIN TO HI-Z INOUT MODE
  Serial.println("LED A OFF  LED B OFF");
  delay(1000);

Here is photographic evidence that it works and here is a VIDEO of it in action
Capture 2.JPG

I'm no EE, so does anyone see any problems with using this method to expand the number of LEDs available?
--Bob
 
Last edited:
The trick is called Charlieplexing:
https://en.wikipedia.org/wiki/Charlieplexing

And works for more than one two LEDs if you don't mind bending your brain a bit. Easy for a single LED, can be made to time share multiple but not a great choice if you might need all the LEDs on at once.

example use:
http://www.evilmadscientist.com/2009/a-bulbdial-clock/

AFAIK - This isn't quite Charlieplexing? This two LED example (from the linked wiki page) shows two signal pins required for two LEDs: Charlieplexing#Complementary_drive
Only one LED is on at a time in this case - until you swap the state of both pins in succession.

What Wozzy shows is a single pin to two LED's with a common ground [and added diodes] - and the PWM on that pin controls the power to get both to light. It seems when both LEDs are on they would get reduced brightness over either being on full time though? <edit>: With what Wozzy shows those two pins could indeed present 4 LEDS, not just two

Charlieplexing is cool though - I saw an example on a AT_tiny AVR unit that used its few pins to drive an 8x8 matrix, then gong farther allowed for brightness control on the individual LEDs. The net effect was an etch a sketch where you controlled a cursor with a joystick and could turn each pixel 'on or off', and tell where the cursor was too. It also did Space Invaders. I should dig that up - it had a cool name "Binary Angle Modulation to drive the LEDs"

Correction - this was on a system with One "MAX7219 LED Driver IC using LedControl and 3 pins" [LedControl is in the Teensy sources], the light drive intensity was also adjusted based on ambient room light: digispark/tutorials/probetashield
 
Last edited:
What Wozzy shows is a single pin to two LED's with a common ground [and added diodes] - and the PWM on that pin controls the power to get both to light. It seems when both LEDs are on they would get reduced brightness over either being on full time though?

defragster: Yes the 3rd case using PWM appears slightly less bright. You can see that in the linked video.

Also something I noticed... If you view the video in slow motion you can see the LED brightness change in two stages.
I'm not sure if this is a video artifact, or if it is due to switching pin states. I'll have to check it with an oscilloscope to see if it is real.

Edit: Watching more carefully the two stage brightness step only occurs when changing the pin from input to output or output to input,
 
Last edited:
Wozzy: indeed the video - I missed - does show a brightness drop going single to double.

Re: Staged brightness - will be interesting what the scope shows - I wonder if the diode has some effect as it fires up? Maybe it would change with fast slew on the output?
 
Cool, but what are those diodes for (in the diagram)? A LED is a diode itself ;)
In the off state, you would have 3.3V across 2 LEDs in series. For LEDs with a low forward voltage, that's enough to light them. Those diodes drop the voltage a bit to prevent that.
 
In the off state, you would have 3.3V across 2 LEDs in series. For LEDs with a low forward voltage, that's enough to light them. Those diodes drop the voltage a bit to prevent that.

Good point. And said so, right in the start. I didn't read well enough...

When Charlieplexing, would not need the diodes.
 
Also something I noticed... If you view the video in slow motion you can see the LED brightness change in two stages.
I'm not sure if this is a video artifact, or if it is due to switching pin states. I'll have to check it with an oscilloscope to see if it is real.

I finally did check this with my scope and it is real.
There's a glitch that occurs when transitioning from PINMODE(#,OUTPUT), DigitalWrite(#,LOW) to PinMode(##,INPUT).
The duration of the glitch is not constant, and varies from 0 to about 80ms.
See this post in a different thread.
 
Yes.... ummmm.... well..... OK, you win! 1000+ LEDs from one pin and any color you wish.
 
Can I use this solution for Arduino Uno Wifi, 5V, I/O pin 40mA ? If yes, then what type of diodes to use ?
 
You can use the same trick using ordinary 0.5V signal diodes, but you should probably prototype up a 5V circuit (teensy is 3.3V) to confirm your LEDs don't light dimly (especially if using red), to test just series both your LEDs and the diodes on 5V and see if they light (they shouldn't). If they do light then you need to either add another diode or look for something with a slight higher voltage drop.
 
If you need to drive more LEDs, methinks a 3 to 8 (74138) or 4 to 16 (74154) demultiplexers provide much cleaner solutions.
 
Status
Not open for further replies.
Back
Top