2

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!

asked Jan 26, 2018 at 1:00
10
  • use digitalWrite(4, HIGH); before pinMode(4, OUTPUT); Commented Jan 26, 2018 at 1:47
  • 1
    @Trevor_G Wait what. That's actually the answer. RenegadeAndy, have you tried anything at all? Commented 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. Commented Jan 26, 2018 at 2:30
  • 1
    @HarrySvensson Yes, it never feels right adding one sentence answers... Commented 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. Commented Jan 26, 2018 at 5:34

1 Answer 1

2

The answer thanks to Trevor_G was:

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

answered Jan 28, 2018 at 14:51
1
  • Yes this really helped Commented Feb 21, 2021 at 5:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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