Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 18 of 18

Thread: Two LEDs from a Single Pin

  1. #1
    Senior Member Wozzy's Avatar
    Join Date
    Jan 2013
    Location
    Philadelphia, Pennsylvania USA
    Posts
    311

    Two LEDs from a Single Pin

    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:
    Click image for larger version. 

Name:	Capture1.JPG 
Views:	82 
Size:	15.9 KB 
ID:	8204
    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
    Click image for larger version. 

Name:	Capture 2.JPG 
Views:	84 
Size:	81.2 KB 
ID:	8205

    I'm no EE, so does anyone see any problems with using this method to expand the number of LEDs available?
    --Bob
    Last edited by Wozzy; 09-17-2016 at 04:54 AM.

  2. #2
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,580
    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/

  3. #3
    Junior Member
    Join Date
    Sep 2016
    Posts
    1
    This great!

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    5,192
    Quote Originally Posted by GremlinWrangler View Post
    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 by defragster; 09-17-2016 at 07:03 PM.

  5. #5
    Senior Member Wozzy's Avatar
    Join Date
    Jan 2013
    Location
    Philadelphia, Pennsylvania USA
    Posts
    311
    Quote Originally Posted by defragster View Post
    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 by Wozzy; 09-17-2016 at 07:41 PM.

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    5,192
    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?

  7. #7
    Senior Member Wozzy's Avatar
    Join Date
    Jan 2013
    Location
    Philadelphia, Pennsylvania USA
    Posts
    311
    Similarly, a bicolor LED (red-green) shows red or green depending in the output pin state or amber with a PWM signal.

  8. #8
    Cool, but what are those diodes for (in the diagram)? A LED is a diode itself

  9. #9
    Senior Member
    Join Date
    Jan 2013
    Posts
    843
    Quote Originally Posted by Pazi View Post
    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.

  10. #10
    Quote Originally Posted by tni View Post
    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.

  11. #11
    Senior Member Wozzy's Avatar
    Join Date
    Jan 2013
    Location
    Philadelphia, Pennsylvania USA
    Posts
    311
    Quote Originally Posted by Wozzy View Post
    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.

  12. #12
    Senior Member Pensive's Avatar
    Join Date
    Aug 2014
    Location
    Basingstoke, UK
    Posts
    537
    brilliant......or.....ws2812b....

  13. #13
    Senior Member Wozzy's Avatar
    Join Date
    Jan 2013
    Location
    Philadelphia, Pennsylvania USA
    Posts
    311
    Yes.... ummmm.... well..... OK, you win! 1000+ LEDs from one pin and any color you wish.

  14. #14
    Junior Member
    Join Date
    Nov 2017
    Posts
    2
    Can I use this solution for Arduino Uno Wifi, 5V, I/O pin 40mA ? If yes, then what type of diodes to use ?

  15. #15
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,580
    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.

  16. #16
    Junior Member
    Join Date
    Nov 2017
    Posts
    2
    Thanks for reply. I found other way for this, without the diodes, just the resistors.

  17. #17
    If you need to drive more LEDs, methinks a 3 to 8 (74138) or 4 to 16 (74154) demultiplexers provide much cleaner solutions.

  18. #18
    The idea is great, looks like I have read somewhere: https://en.wikipedia.org/wiki/Charlieplexing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •