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.
simulate this circuit – Schematic created using CircuitLab
Figure 1. A typical GPIO switch sensing arrangement with external pull-up.
How it works:
A few more points:
Now to your questions:
This should now be clear from the answer above.
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'.
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.
Only you can answer that as we don't know what's connected. Generally there is no problem.
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.