Not many people know that it is possible to control 2 LEDs individually with just a single digital output pin. See the video below for proof. These 2 LEDs are both connected to the same pin, digital pin D5 on a Wemos D1 mini.

How is this magic possible? Take a look at the circuit diagram. As you can see we have both of the LEDs connected to the same digital pin. However, one LED has its cathode (negative leg) connected to ground and the other one is connected to 5v (or 3v on a Wemos D1).

To light up LED 1, make the digital pin an output and make the pin HIGH. Electricity will flow from the digital pin to ground, lighting up LED 1. To light up LED 2, make the digital pin go LOW. Now the electricity will flow from the 5v (0r 3,3v) pin to ground via the digital pin that has been pulled low.

To turn both LEDs off, make the digital pin an INPUT, which will prevent the voltage from going anywhere and both LEDs now turn off.

So, how do you light up both LEDs? Well, it is not possible. However, you can trick your eye into seeing they are both on by turning each LED on in quick succession very very fast.

Take a look at the entire code below and you will see how to turn both LEDs on at the same time in the do…while loop.

``````unsigned long counter;

void setup() {
pinMode(D5, OUTPUT);
}

void loop() {
// Turn LED A on B off
pinMode(D5, OUTPUT);
digitalWrite(D5, HIGH);
delay(1000);

// Turn LED B on A off
digitalWrite(D5, LOW);
delay(1000);

// Turn LED A and B on (very fast)
counter = millis();
do
{

digitalWrite(D5, HIGH);
digitalWrite(D5, LOW);
} while ((millis() - counter) < 1000);

// Turn LED A and B off
pinMode(D5, INPUT);
delay(1000);
}``````

The only disadvantage to this is when both LEDs are apparently on they re slightly dimmer then when on individually. However, for the sake of saving 50% of he number of pins this is a small trade-off.