Delay before Internal Pullup becomes effective

Strontium
Posts: 20
Joined: Sat Dec 26, 2015 11:33 pm

Delay before Internal Pullup becomes effective

Postby Strontium » Tue Jul 24, 2018 9:58 am

Hi,

I am working on an Arduino sketch and I see a weird problem.

This is my code:

Code: Select all

pinMode(21, INPUT_PULLUP);
X = digitalRead(21); 
Y = digitalRead(21); 
There is nothing connected to pin 21, it is an unpopulated header, and so should be being pulled up by the internal pull up, and should have no load on it.

X is being assigned 0.
Y is being assigned 1.

X & Y, should, in theory, be both 1.

If i change the code to:

Code: Select all

pinMode(21, INPUT_PULLUP);
delay(1);
X = digitalRead(21); 
Y = digitalRead(21); 
Then X & Y are both 1, as expected.

My question is, does anyone else see this? I have tried it on two boards with the same result. I have tried it on other pins, with the same result. It seems strange to have to insert a dummy read, or wait 1 millisecond before the pin state is what it should be.

Steven

ESP_Sprite
Posts: 8926
Joined: Thu Nov 26, 2015 4:08 am

Re: Delay before Internal Pullup becomes effective

Postby ESP_Sprite » Tue Jul 24, 2018 2:45 pm

Well, electrically this can be explained. A GPIO pin has a certain capacitance, and a pull-up has a certain resistance. If the GPIO level is low, it takes a while (say, around the RC time) for the pull-up resistance to charge the pin capacitance. It's not much, a quick back-of-the-envelope gives me around an uS, but seeing the CPU runs at 240MHz, it's very possible that you sample the pin before this has happened. Suggest you indeed put a small delay (a few uS should already work) between the setting of the pull-up and the read and it'll probably work; a mS is more than adequate.

(By the way, neat trick: if you put your finger very close *near* (but not touching) the offending pin, chances are the second digitalRead will also return zero. This is because the combination of pin-air-finger forms a capacitor to ground, which exists in parallel with the existing parasitic capacitance; the total of capacitance of the pin increases. This causes the pull-up to need more time to charge it, meaning that it will take longer for a digitalRead to return 1. This is how some capacitive touch solutions work.)

Strontium
Posts: 20
Joined: Sat Dec 26, 2015 11:33 pm

Re: Delay before Internal Pullup becomes effective

Postby Strontium » Wed Jul 25, 2018 2:06 am

Yes, we suspected as much. Just wanted to make sure there wasn't some other explanation.

Thanks for your reply.

Steven

Who is online

Users browsing this forum: No registered users and 50 guests