Internal pull up resistors and long distances to switch

I'm using internal the internal pull-ups on a Mega to read the state of a button and am getting inconsistent reads. When the button is pressed, it consistently reads as LOW, with no variance. However, when it's not being pressed, I get sporadic results. Sporadic enough that I can't seem to work through the errors with code.

There is close to 10' of wire from the button back to ground, and I suspect this long distance is the culprit. What might a workaround be? If I were to use an external pull-up resistor, what might be a good value to guarantee consistent results?

Start with 10K, but first try a 0.1uf cap from the input pin to ground. Keep the internal pullup on. Don't go lower than 1K. The internal pullup are pretty weak so external resistors can help a lot as can the cap.

I've read that the internal resistors are between 20k and 50k. Wouldn't I want to exceed that?

At 10’, that’s a lot of wire to pick up 50 or 60 Hz AC. I would suggest trying a twisted pair of wires (the other wire to ground and/or chassis). The 0.1 uF suggest by groundFungus might do it. The RC time constant delay is 0.1 second but it will clean up the AC too.

No, you need a stronger pullup, so a lower value of pullup resistor. The stronger the pullup, the more noise that it takes to cause a false indication. But one wants to keep the value of the pullup a reasonable value so as not to consume more current than necessary.

Pullup tutorial.

groundFungus:
Pullup tutorial.

Nice link!!!

But there is one line at the link that would probably throw people, which is the line that says “Keep in mind, if the resistor wasn’t there, your button would connect VCC to ground, which is very bad and is also known as a short.”

My take on it would be … if the resistor wasn’t there, then Vcc woud be connected to nothing, and the button would only be connected to the input pin when pressed. Resistor not there means open-circuit between the Vcc node and the rest of the circuit. But once again, that’s the life story of many articles, where the author doesn’t read line-by-line everything that they wrote — just to make sure it’s not going to throw anybody. But, aside from that… good link about pull-up resistors and their usage.

You can try something as shown in Dirty Ac power activating uno button - #7 by dlloyd - General Electronics - Arduino Forum

“Keep in mind, if the resistor wasn’t there, your button would connect VCC to ground, which is very bad and is also known as a short.”

Perhaps it should read something like “… if there was no resistor in the circuit…” or something similar. Its really difficult to write perfectly clear instructions, let alone targeted to an audience with little or no electronic background.

JohnRob:
Perhaps it should read something like "..... if there was no resistor in the circuit......." or something similar. Its really difficult to write perfectly clear instructions, let along targeted to an audience with little or no electronic background.

Totally agree JR. If there happen to be no resistor in the circuit, then that would leave a gap in the wire. Open circuit condition, which would leave Vcc disconnected (not connected to the input pin, and not connected to the button etc).

Bapstack:
I've read that the internal resistors are between 20k and 50k. Wouldn't I want to exceed that?

I have seen for my some cases (particularly interrupt process), the internal pull-up (20k - 5k) value does not work at all? An external pull-up of 2.2k worked well with UNO.

Would there be a problem with using the same pull-up resistor for 2 pins, if each are connected to different buttons? Not that it would save me much time or space. Just curious.

No. If one switch is closed it will pull both inputs to ground.

There are a few ways to solve this one. Here are a couple

  1. better shielding from emf with good ground to Arduino. Lower the impedance with smaller resistor. Use thicker wires for less impedance. Use a small filter cap to ground.

  2. Use higher voltage. Like 12 or 24v and use voltage divider at Arduino to drop to 5.

wolframore:
Lower the impedance with smaller resistor. Use thicker wires for less impedance.

No, thicker wires makes no sense, the wire resistance will be a few ohms at most, and even if it were 100 ohms it wouldn't stop the circuit working. We are talking about a signal line, not power.

2k2 pullup will be fine for most situations, adding 10nF or so to ground at the Arduino end will knock
out any RF interference completely.