0

I read the Arduino official docs on digitalWrite and am confused about what is happening from an electrical perspective when digitalWrite is invoked on INPUT_PULLUP pins.

Say my code says this:

pinMode(4, INPUT_PULLUP);
digitalWrite(4, LOW);

The docs say:

If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor. See the Digital Pins tutorial for more information.

Does this mean pin 4 is floating or is it at ground? Are those two lines of code equivalent to pinMode(4, OUTPUT); digitalWrite(LOW); ?

3
  • 1
    look at section 14.2 of ww1.microchip.com/downloads/en/DeviceDoc/…
    – jsotola
    Mar 26 at 3:27
  • Thanks, I a noob in EE and arduino so I think I need some help understanding what this doc means. I'm trying to get at what kind of behavior to expect. Are you also saying this is chip dependent?
    – rfii
    Mar 26 at 3:42
  • 1
    definitely chip dependent, but probably very much the similar
    – jsotola
    Mar 26 at 3:52
1

It's pretty explicitly stated there in your quote: if you write a LOW when in INPUT mode it disables the pullup resistor. That's all it does.

pinMode(4, INPUT);
digitalWrite(4, HIGH);

is exactly the same as:

pinMode(4, INPUT_PULLUP);

And

pinMode(4, INPUT_PULLUP);
digitalWrite(4, LOW);

is exactly the same as

pinMode(4, INPUT);

Of course this exploits a peculiarity of the AVR's IO block, but any decent core will emulate that functionality for compatibility with older code.

2
  • Thanks, can you address the part of the question about if pinMode(4, INPUT); leaves the pin floating or if it is at GND?
    – rfii
    Mar 26 at 15:34
  • 1
    A pin set to INPUT with no pullup and not connected to anything is always floating. It can never output anything or be set to any level unless it is explicitly set to OUTPUT.
    – Majenko
    Mar 26 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.