I have an esp8266-12e that I want to activate the reset side of a latching relay. The problem is that the 12e only puts out 12mA/pin and the relay needs more than that. I am using a transistor on the set side from a rf antenna signal. I only have one 3.3v power source. Is there a way to have two pins go HIGH at the exact same time and be connected to the reset side of the relay so there is enough current? Would this work, would it hurt anything, and how would I write the code for it and how would I wire it up?

I have tried the code where one pin is written HIGH right after the other like this

digitalWrite (5,HIGH);
digital Write (6,HIGH);

but I think pin 5 goes of before pin 6. Any way to get them to go off at the same time?

Thanks for the help

  • 2
    No that's not a good idea. You need to use a transistor to switch the relay. You can drive the relay from an output pin.
    – Delta_G
    Commented May 13, 2020 at 0:17
  • I think you will need a transistor. On separate question that you asked, you can turn two digital pins on at the same time through port manipulation.
    – hcheung
    Commented May 13, 2020 at 2:07
  • get power from power supply. use a pin to switch the power over a transistor
    – Juraj
    Commented May 13, 2020 at 5:14
  • @hcheung Port manipulation like that isn't available on the ESP8266 where all the hardware interaction is performed through an SDK and the internal port -> pin arrangements aren't known (if they even exist). Better is to use the tri-state functionality of the pins if you must do it at all.
    – Majenko
    Commented May 13, 2020 at 20:21

1 Answer 1


That is not a good idea.

Isolating the relay from the IO pin is not just about the current limit of an IO pin, it's also about preventing back-EMF spikes from the relay coil(s) from damaging the delicate internals of the chip.

Instead you must at least use a transistor and a back-EMF protecting diode.

However - if you did want to theoretically have two pins safely controlled to provide double the current (though I stress again, do not do this for driving a relay), it is possible.

But it's not straight forward.

It's not possible on the ESP8266 to use port manipulation to make two pins change state at the same time. However it is possible to go via a third state that makes it safe.

Note: this will only be "safe" for IO pins that do not do strange things at bootup, and many of the pins on the ESP8266 do strange things.

IO pins actually have three states, not two:

  • High
  • Low
  • High Impedance

The latter is the state the pin is in when in INPUT mode. And we can use that to our advantage.

When in High Impedance mode it's like the pin is disconnected. So we could:

  1. Make sure both pins are in INPUT mode
  2. Switch one pin to OUTPUT and set it to the desired state
  3. Switch it back to INPUT mode (keeping the desired state latched)
  4. Switch the other pin to OUTPUT and set it to the desired state
  5. Set the first pin back to OUTPUT mode again.

This can be slightly compressed as we don't really need to set both pins to INPUT at the start, only one. So here's some code that might do it:

void digitalDoubleWrite(int pina, int pinb, bool level) {
    pinMode(pina, INPUT);
    pinMode(pinb, OUTPUT);
    digitalWrite(pinb, level);
    pinMode(pinb, INPUT);
    pinMode(pina, OUTPUT);
    digitalWrite(pina, level);
    pinMode(pinb, OUTPUT);

You might ask why we'd want to switch to OUTPUT just to latch a level into the pin. Well, that's because of a quirk of the Arduino API. A historical artefact that is lingering in there to this day:

  • digitalWrite() on a pin set to INPUT mode was used to turn the internal pullup resistor on and off

It's actually how the AVR's IO ports control the pullup resistors internally, and that has most often been copied in software for other chips to maintain backward compatibility with old code. So as a result we have to switch to OUTPUT mode so that the API code knows that we mean to set the output latch state and not the pullup resistor state.

  • Thank you for your answers. They are all very valuable. I will make this one as the answer as it provides the most information for anyone wishing to get information. Commented May 13, 2020 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.