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:
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
Here is photographic evidence that it works and here is a VIDEO of it in action
I'm no EE, so does anyone see any problems with using this method to expand the number of LEDs available?
--Bob
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:
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
I'm no EE, so does anyone see any problems with using this method to expand the number of LEDs available?
--Bob
Last edited: