ESP01 GPIO active when the board is turning on

Hello everyone, I hope you are doing well.
I have an IOT project where an ESP01 is connected to a relay module (as in the image). ESP01 will check the state of a field in my MQTT, and open a gate.

But when the power goes out and the ESP01 restarts, the gate opens by itself. That is, GPIO0 would need to be HIGH when ESP01 is starting to prevent this from happening. How can I resolve this?
Hello everyone, I hope you are doing well.
I have an IOT project where an ESP01 is connected to a relay module (as in the image). ESP01 will check the state of a field in my Firebase, and open a gate.

But when the power goes out and the ESP01 restarts, the gate opens by itself. That is, GPIO0 would need to be HIGH when ESP01 is starting to prevent this from happening. How can I resolve this?

I tried to put GPIO0 as HIGH in the setup() method, but it didn't solve it

This may be the schematic for your ESP-01 relay module: https://github.com/IOT-MCU/ESP-01S-Relay-v4.0/blob/master/ESP-01S%20Relay%20v4.0.pdf

Pin GPIO0 appears to be pulled high at a normal restart according to this: https://github.com/tttapa/ESP8266/blob/master/A%20Beginner's%20Guide%20to%20the%20ESP8266%20-%20article/PDF/A-Beginner's-Guide-to-the-ESP8266.pdf

However, if you put the ESP-01 into programming mode then that needs GPIO0 to be pulled LOW and that would trigger the relay.

Putting something in setup() won't help because it is executed too late in this case. You could try an external 10k pullup resistor on GPIO0 to 3.3v but that should (must?) already be there.

Add the program you are running on the ESP-01 to this thread.

1 Like
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

// Set these to run example.
#define FIREBASE_HOST "my-host.firebaseio.com"
#define FIREBASE_AUTH "auth_token"
#define WIFI_SSID "Tenda"
#define WIFI_PASSWORD "24446666688888888"

const int relay = 0;

void setup() {
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.println("Em processo de conexão");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.println("Conexão executada!");
  Serial.println(WiFi.localIP());
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

int status = 1;

void loop() {
  status = Firebase.getInt("status");
  delay(500);
  if(status == 1){
    digitalWrite(relay, LOW);
    delay(800);
    digitalWrite(relay, HIGH);
  }  
  Firebase.setInt("status", 0);
  delay(500);
  digitalWrite(relay, HIGH);
}

This is my code.
As you said, putting digitalWrite in the setup doesn't work.
To place the resistor, just solder on pins 3.3V and GPIO0 ?

Before you start soldering, maybe describe exactly the circumstance that you get this unwanted relay trigger.
Does it happen (1)when you press "Reset" or (2)by a power fail restart or (3) when your load a program on the the ESP-01 ?
When it happens, does it do so before the ESP-01 has connected to a network (usually this takes a few seconds) ?
Also, when it happens is the relay only triggered for a very short time (a few ms) or the full 800ms which is specified in the code ?

1 Like

In the first question, this happens when a power failure happens, or even when I unplug and plug the ESP power supply.
In the second, yes. The relay is activated before the ESP connects to the network.
Finally, the relay is activated for a very short period, it does not reach 800ms, but it is enough for my gate to stay open.

I did a test, loading an empty sketch, with only the setup and loop functions (no variables, no declarations, just the empty functions), the relay continued to be activated by the ESP when it is connected to the power, even without any code definitions

It looks like it could be this: MOSFET Self-Turn-On Phenomenon. To investigate properly you'd need a scope to see if the optocoupler led is being energized momentarily at power up. The solution maybe, in this case, to put a capacitor, say 100nF, between the mosfet gate and source. This is going to mean some fine soldering on that board. If the schematic is the same as that in the link I posted, then easiest would probably be between pin 1 on the voltage regulator and pin 3 on the optocoupler rather than on the mosfet itself.

The cause of this is that during boot some of the ESP's GPIO pins go HIGH momentarily, this is described here, unfortunately the only pins that are exposed on an ESP-01 are experiencing this. When dealing with this issue myself for a Relay board (not of the shelf, but designed PCB) i ended up adding an Attiny13a into the circuit that would add a 100ms debounce on the output pin. All other ideas to tackle this problem were more complex and required more parts (for me, that is, if you've never programmed an Attiny13 before it may be a bit more tricky)

1 Like

OK. From the description it appears, if I have understood it correctly, as if GPIO0, which is pulled high (10k) at boot time oscillates for about 25ms on power-up.

One other possible solution may be to put a RC network ( say 10K and 10uF ) in the path between the optocoupler and mosfet gate but after the gate pull down resistor. That would have a time constant of 100ms. If that works, it may than be necessary to prolong the trigger time in the code.

This must then be a very well known problem with those ESP-01 relay shields. Maybe there are more elegant solutions available. But the idea of using an Attiny13a or other microprocessor is also interesting.

Yes i had that on a breadboard as well, had some issues getting it to work, and the Attiny solution was so straightforward and worked perfectly.

It's 800ms now, that would be fine.

Well known i am not sure, but i have seen a thread come through within the last year, and this was before i started on my own board, The issue is compounded by the fact that GPIO 0, 1 & 2 can not be pulled LOW at boot for the correct boot-mode to be selected, which makes the creation of an RC circuit more complex (only GPIO 3 (RX) is ok to Pull LOW at boot) since you need both a slow charge and discharge of the capacitor.

I use them quite a lot and at less than E1,- the price is not an issue, they will run at 3.3v just fine, so actually no extra components required, once the bootloader is burned and the sketch uploaded.
the sketch i use

#define DEBOUNCE 120
#define IN_PIN 0
#define OUT_PIN 3

void setup() {
  digitalWrite(OUT_PIN, LOW);
  pinMode(OUT_PIN, OUTPUT);
  pinMode(IN_PIN, INPUT);
}

void loop() {
  digitalWrite(OUT_PIN, DebounceCheckPin());
  delay(10);
}

bool DebounceCheckPin() {
  static bool oldPinState = true;
  bool pinState = digitalRead(IN_PIN);
  if (pinState == oldPinState) return !oldPinState;
  for (uint16_t i = 0; i < DEBOUNCE; i++) {
    delay(1);
    if (pinState != digitalRead(IN_PIN)) return !oldPinState;
  }
  oldPinState = pinState;
  return !oldPinState;
}

Note my relay is active HIGH, i don't use an opto-coupler after the Attiny, but just an NPN transistor
Of course best practice is to test everything on the breadboard first, which is also easy enough to program the Attiny.

I think it is, but looking at it GPIO 0 is wired directly to pin 2 of the PC817, which means you may just need to cut that pin of the PC817 to tap in to the circuit.

1 Like

Hello All,
I have the same issue, on boot the PIN go HIGH for some milliseconds and my door is opened. I also tried to add a 470uF Capacitor as I read in this thread:

but the behavior is the same.
I also tried to do a my board connecting the relay on all GPIO (from GPIO0 to GPIO3) but the PIN is alway pulled up on boot.

As i stated before it can not be helped on an ESP-01, My solution in #9 is still valid.
The document with the investigation which actually was instigated for using an esp for the same purpose.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.