internal PULLUP resistor not working?

I've got a problem and I hope someone can help me.... I have a Mega and this pin setup as a digital out, with a button (normally closed) to it. It should read HIGH all the time (with the button untouched). But it does not. After a while it will read LOW for many times a minute. To my understanding, this is floating behaviour by absence of a pullup resistor, but the pin is configured to have an internal pullup resistor with pinMode(6, INPUT_PULLUP). The other side of the button is connected to ground.
I stripped down all my code, just left some code around this pin to do some troubleshouting, and these are my findings:

  • It starts happening after about half an hour after the arduino is switched on (there's no other code!) First a few times a minute, later about 3 times a second.
  • It only happens when the arduino is on external power (this is the normal state for my project). If the arduino is only power by USB there are no LOW reads on that pin.
  • I changed the external power to 7.5/9/12V but that did not change a thing.
  • I used a different external power, the behaviour of the pin was the same although the pace of the LOW readings was much lower.
  • I connected the switch to a different pin, but this pin started to read the LOWs just as the old pin.
  • I disconnected the button on the end of the wire, but stilll the LOW readings appeared.
  • I disconnected the complete wire from the pin, and the LOW readings stopped!

The last two findings tell me something is interfering with the wire, and combined with non-working internal pullup resistors it is inevitable to read LOWs all the time, am I right?
I must say that this wire is the longest in my project, about 40cm, does that explain something?
The real question is ofcourse what I can do about it. Do I have to put an extra pullup resistor on the end of the wire near the button, and if so, what value should it get? And does it work well in conjuction with the internal pullup resistor?
I hope someone can shine a light on this...

  • I

Uh.. in reply to myself: the button is normally OPEN ofcourse..

I must say that this wire is the longest in my project, about 40cm, does that explain something?

It may be acting as an antenna, picking up enough EMI to overpower the internal pull-up. It doesn't really seem so long of a wire.

I'd think an external 10 kOhm resistor should provide a strong enough pull up (the internal pull up resistors in the Mega are 20-50 kOhm) but you could go with 4.7 kOhm just to be sure. In this application the only downside to using a lower value resistor is it will increase power consumption but that's only really an important consideration if you're running on batteries.

Problem solved!
I disabled the internal pullup resistor and added an external one of 4,7K. I tested it for over an hour now, but did not find any LOW readings anymore. Thanks pert!