I am using a ESP8266, specifically the ESP-12-E :

enter image description here

I have a Relay, SRD-05VDC-SL-C connected to GPIO pin 4.

It is wired in Normally Open(with my device to be powered wired into the top and middle pins counting from the top to the bottom of the picture below):

enter image description here

In my arduino code when I issue:

  pinMode(4, OUTPUT);

This seems to pull the pin LOW, which is the active state of the relay, powering it, which turns my device on before my control code has even had chance to run. I want to get the pin for output but keep it floating.

How do I setup my pin for output, but leave it 'floating' from code, or keep it high / floating, so that I have to explicitly issue:

digitalWrite(4, LOW);

Before low is sent to activate my relay?

Hope this makes sense!

  • use digitalWrite(4, HIGH); before pinMode(4, OUTPUT);
    – Trevor_G
    Jan 26, 2018 at 1:47
  • 1
    @Trevor_G Wait what. That's actually the answer. RenegadeAndy, have you tried anything at all?
    – Harry Svensson
    Jan 26, 2018 at 2:21
  • @HarrySvensson no. I didnt expect the act of calling the pinMode() function to actually cause the pin to go to LOW.
    – RenegadeAndy
    Jan 26, 2018 at 2:30
  • 1
    @HarrySvensson Yes, it never feels right adding one sentence answers...
    – Trevor_G
    Jan 26, 2018 at 2:53
  • 2
    GPIO4 is LOW during reset (and thus boot), so an active-low relay will erroneously click on power-on if wired to GPIO4. use GPIO 3 or maybe 2 or 16. you also might want to set pinMode(n, INPUT_PULLUP) to lock it into floating until needed.
    – dandavis
    Jan 26, 2018 at 5:34

1 Answer 1


The answer thanks to Trevor_G was:

use digitalWrite(4, HIGH); before pinMode(4, OUTPUT);

  • Yes this really helped
    – madu
    Feb 21, 2021 at 5:06

Your Answer

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