Arduino: PinMode as INPUT

Created on 27 Jan 2018  Â·  12Comments  Â·  Source: esp8266/Arduino

...

Most helpful comment

@Tensa225 I may be mistaken, but I really think your message above does not make much sense.

Sorry if I'm stating the obvious, but OUTPUT vs INPUT has nothing to do with the direction of the current (sourcing or sinking), it has to do with which component drives the voltage level.

  • If you want the ESP to drive the voltage level, use pinMode(pin, OUTPUT). If you then call digitalWrite(pin, HIGH), it will source current if the other end is trying to pull that voltage low (e.g. resistor to GND). If you call digitalWrite(pin, LOW) instead, it will sink current if the other end is trying to pull that voltage high (e.g. resistor to VCC).
  • If you want the attached component to drive the voltage level, and the ESP to get that information, use pinMode(pin, INPUT), then let the other end impose its voltage, and call digitalRead(pin)

OUTPUT is called _low impedance_, because there is almost no resistance between the pin and either VCC (if digitalWrite(pin, HIGH)) or GND (if digitalWrite(pin, LOW)).
INPUT is called _high impedance_, because the ESP has no influence on the signal (like if it was disconnected or was connected through a very high value resistor). If nothing is connected to an input pin, the value returned by digitalRead() is generally undefined (it is _floating_ because nothing is driving the pin at the other end).

Now additionally to that, in input mode, a setting can be used to connect a resistor inside the chip from the pin to VCC (INPUT_PULLUP available for GPIO0-15) or GND (INPUT_PULLDOWN_16, only available for GPIO16, hence the name). This of course lowers the impedance, but the resistance is high enough so that the pin voltage can still be driven by the component at the other end (which really has a LOW impedance).

Finally, many ESP8266 modules include external pull-up or pull down resistors that make sure the chip boots correctly (e.g. from Flash), and you cannot do anything to disable them, apart from unsoldering them of course.

Analog pins (the ESP has only one), as you say, are INPUT pins that have the particularity to be read not only as a HIGH or LOW level by digitalRead(), but also as the actual value of the applied voltage using analogRead().

If you have noticed a change between versions (please specify which ones exactly) of the cores, please post a drawing of your schematic and a minimal sketch demonstrating the issue, as well as observed results with each tested version so we can reproduce the issue.

All 12 comments

on the ESP, LOW is HIGH and HIGH is LOW
if you drive a pin LOW you effectively set it as 3.3V
digitalWrite(pin, HIGH) effectively outputs 0V
it's a chinese design i guess :)

problem is, if it's changed in the core, everyone's code/projects will be effected. not good...

if you drive a pin LOW you effectively set it as 3.3V

That's not the case, as far as I can tell. How did you arrive to this conclusion?

@tonton81 If that was true, none of the thousands and thousands of sensors people use every day with the ESP would work. That is to say, not even remotely true.

sorry im confused, i had the adafruit huzzah breakout and when i set a gpio high the output was low, i havnt played with the gpios lately though im not too fond of their shared finicky “states” :)

@tonton81 How did you determine that the output was low? Did you check it with multimeter? Looking at the pictures at https://www.adafruit.com/product/2471 there is no inverting logic anywhere on the board, and the ESP itself doesn't use inverted logic like that.

it was wired to another mcu’s input i was using esp as output teensy as slave, perhaps i did something wrong, anyways was a few months ago :P

I'm don't know the characteristics of the ESP by memory, but the following is my understanding:

@Tensa225 said:

OUTPUT should be high impedance, INPUT low impedance. Are they?

I think you have it backwards, or you are confusing the meaning of high impedance. OUTPUT internally connects either VCC or GND to the pin, and is therefore low impedance. INPUT just reads what is connected to the pin, and needs to allow the entire range from GND to VCC, so it should be high impedance.

When you set a pin to INPUT, and you don't have anything connected to it, the reading could be anything (it floats). Not all pins behave the same though, some have pullups/pulldowns. If a pin doesn't have a pullup/pulldown, then you should probably add one yourself, depending on your application.

Having said the above, this is not the right place to discuss this. This is an issue tracker for the core, and the GPIO behavior doesn't currently have issues related to your description.
Closing.

@Tensa225 I may be mistaken, but I really think your message above does not make much sense.

Sorry if I'm stating the obvious, but OUTPUT vs INPUT has nothing to do with the direction of the current (sourcing or sinking), it has to do with which component drives the voltage level.

  • If you want the ESP to drive the voltage level, use pinMode(pin, OUTPUT). If you then call digitalWrite(pin, HIGH), it will source current if the other end is trying to pull that voltage low (e.g. resistor to GND). If you call digitalWrite(pin, LOW) instead, it will sink current if the other end is trying to pull that voltage high (e.g. resistor to VCC).
  • If you want the attached component to drive the voltage level, and the ESP to get that information, use pinMode(pin, INPUT), then let the other end impose its voltage, and call digitalRead(pin)

OUTPUT is called _low impedance_, because there is almost no resistance between the pin and either VCC (if digitalWrite(pin, HIGH)) or GND (if digitalWrite(pin, LOW)).
INPUT is called _high impedance_, because the ESP has no influence on the signal (like if it was disconnected or was connected through a very high value resistor). If nothing is connected to an input pin, the value returned by digitalRead() is generally undefined (it is _floating_ because nothing is driving the pin at the other end).

Now additionally to that, in input mode, a setting can be used to connect a resistor inside the chip from the pin to VCC (INPUT_PULLUP available for GPIO0-15) or GND (INPUT_PULLDOWN_16, only available for GPIO16, hence the name). This of course lowers the impedance, but the resistance is high enough so that the pin voltage can still be driven by the component at the other end (which really has a LOW impedance).

Finally, many ESP8266 modules include external pull-up or pull down resistors that make sure the chip boots correctly (e.g. from Flash), and you cannot do anything to disable them, apart from unsoldering them of course.

Analog pins (the ESP has only one), as you say, are INPUT pins that have the particularity to be read not only as a HIGH or LOW level by digitalRead(), but also as the actual value of the applied voltage using analogRead().

If you have noticed a change between versions (please specify which ones exactly) of the cores, please post a drawing of your schematic and a minimal sketch demonstrating the issue, as well as observed results with each tested version so we can reproduce the issue.

At this point I do have to ask, if this is really the right place to try and teach someone electronics basics?
@Tensa225 has completely misunderstood how GPIO works in a microcontroller, which is not an issue with the code, and keeping this thread alive here doesn't make much sense.

@WereCatf At this point I do have to admit, you are right :-). Sorry couldn't resist to explain...

Was this page helpful?
0 / 5 - 0 ratings