Electronic – ESP8266, GPIO0 or GPIO2 as input

esp8266gpio

I have a ESP8266-01 and I want to use it to check if my door is open or not using GPIO0 or GPIO2 as input.

As GPIO0 and GPIO2 both needs to be high or floating at boot, I cannot directly connect my switch to either of it. The solution is that I connect the switch between GPIO0 and GPIO2 so that at boot-up both are high, and once the chip is booted, set GPIO0 as output and low. Now, if the switch is open, GPIO2 will read high and if it is close it'll read low. This is what I understand from the discussions and posts at several places.

But, I'm new and confused! 😛

Can I connect GPIO0 and GPIO2 directly without anything else? No pull-ups, no resistors in between?

Is it okay to directly connect GPIO0 and GPIO2 with a wire at boot-up? And also when GPIO0 is output-low and GPIO2 is input?

As I understand, in output mode, GPIO is connected the with VCC or ground with very low resistance. What about input? How is it implemented? If I connect GPIO0's output to GPIO2's input, is there any chance of short-circuit?

When setting that up after boot, is there any specific sequence I should be setting GPIO0 and GPIO2 as output or input respectively? Does it matter?

From what I understand, the chip has internal pull-ups. But discussions around the web gives a very confusing idea about the need of external pull-up resistors. Are pull-up resistors needed to implement this?

Best Answer

Here's the standard means of doing what you require with a little explanation.

schematic

simulate this circuit – Schematic created using CircuitLab

Figure 1. A typical GPIO switch sensing arrangement with external pull-up.

How it works:

  • If SW1 is open then R1 pulls the input to V+ which will be read as a logical '1' by the micro.
  • If SW1 is closed then the input will be pulled to GND and will be read as a logical zero.
  • C1 adds a little "debounce" to slow the rise and fall of the input voltage in the event of chatter or bounce on contact open or closure. You can work out the approximate delay simply by \$ \tau = R1 \cdot C1 = 10k \cdot 100n = 1 \ \mathrm {ms} \$. \$ \tau \$ (the Greek letter tau) is the time-constant.

A few more points:

  • Because R1 is connected to the same V+ as the micro it will be impossible for the GPIO voltage to exceed the supply voltage (which could damage the chip). The voltage on each will rise in unison.
  • Because this switching arrangement is so common and pull-up resistors are required, it is usual for many of these micros to feature internal pull-ups. If you enable yours in your program then R1 can be omitted.

Now to your questions:

Can I connect GPIO0 and GPIO2 directly without anything else? No pull-ups, no resistors in between?

This should now be clear from the answer above.

Is it okay to directly connect GPIO0 and GPIO2 with a wire at boot-up? And also when GPIO0 is output-low and GPIO2 is input?

That's fine. It would be a problem if you had two outputs connected together and one was trying to give a 'high' while the other was giving a 'low'.

As I understand, in output mode, GPIO is connected the with VCC or ground with very low resistance. What about input? How is it implemented? If I connect GPIO0's output to GPIO2's input, is there any chance of short-circuit?

When the GPIO is configured as an input it is essentially a transistor monitoring the input voltage. Since it is not sourcing or sinking any current it can't be damaged by pulling the input high or low. On the other hand if you short an output to ground and try to set the output high then you will draw a high current from the output and possibly damage the device.

When setting that up after boot, is there any specific sequence I should be setting GPIO0 and GPIO2 as output or input respectively? Does it matter?

Only you can answer that as we don't know what's connected. Generally there is no problem.

From what I understand, the chip has internal pull-ups. But discussions around the web gives a very confusing idea about the need of external pull-up resistors. Are pull-up resistors needed to implement this?

The pull-up, either external or internal, is required if there is a possibility that the input could "float" to an undefined level. If no pull-up was used in Figure 1 then opening SW1 would leave the input floating and susceptible to stray voltages and electrical interference giving unreliable readings of the switch status.