2
\$\begingroup\$

I want to register button presses via a Python program I wrote with a Raspberry Pi 3. My buttons are connected both connected to ground and GPIO pins 22 and 27. However, when the buttons are pressed, it isn't giving a constant True but it changes really fast, so I get something like 'True, False, True, True, False, False, False, True, True' when it should be all true.

When the button isn't pressed however, it gives a constant false. I have checked all wires and tried it with and without resistors (10k and 4.7k), but they give the same result.

Is there any way to get this problem fixed? I also tried it with different switches, but they all give the same result. They are wires up with standard jumper wires.

\$\endgroup\$
  • \$\begingroup\$ Show a schematic of how exactly you have wired them up, and how (pull-ups?) you have configured the pins. \$\endgroup\$ – CL. Aug 7 '17 at 12:03
  • \$\begingroup\$ are you using 'pressed' as a verb, and we're talking about the bouncing 10mS of Olin's answer, or as a noun, the state of being held down, and we're talking about the seconds that should be true once the bouncing has stopped? \$\endgroup\$ – Neil_UK Aug 7 '17 at 12:54
  • \$\begingroup\$ Intermittent and unreliable contact bounce is normal for any poor switch. Adding C shunt across switch such that RC> polling interval , debounces the switch voltage by charge and hold. Decay time RC can be controlled by 1M=R. Assuming switch is <100 Ohms carbon and R pullup to Vdd(max) is 1M, C can be 10nF. for 10mS =T which ought to exceed bounce time. \$\endgroup\$ – Tony Stewart Sunnyskyguy EE75 Aug 8 '17 at 18:48
3
\$\begingroup\$

This is what's called bouncing. You should expect any mechanical switch to do that for up to a few 10s of ms on make and break.

To deal with this, you perform de-bouncing in software. Usually that means you don't consider a new button state valid until you've seen that state continually for 50 ms or so.

If the bouncing persists while you're holding the button down, then the button is faulty. Replace it.

\$\endgroup\$
  • \$\begingroup\$ Whoever downvoted this, what exactly do you think is wrong? \$\endgroup\$ – Olin Lathrop Aug 7 '17 at 12:44
  • \$\begingroup\$ Thanks for your reply! It isn't bouncing, because even when I test every 1/10th of a second, the problem persists. When there isn't a wire connected, it is a constant 0, which it should. When there is a wire connected to the GPIO pin, it's changing between 1 and 0, so you get something like this: 0000001111111000001111110000000000000111111 When that wire is connected to ground (the button is pressed) it gives a constant 0, which I find a little bit weird. \$\endgroup\$ – ItsKasper Aug 8 '17 at 17:58
  • \$\begingroup\$ @its: Something isn't adding up. It sounds like this input is high impedance, and with just a wire connected it is picking up noise. However, that is inconsistent with what you said in the question. Show a schematic and explain exactly what happens when the button is pressed and when its not. The part about the wire doesn't make sense. \$\endgroup\$ – Olin Lathrop Aug 8 '17 at 18:04
  • \$\begingroup\$ I really appreciate your effort! I got it fixed by adding a pulldown (correct me if I'm wrong) resistor to ground and instead of connecting the button to ground, I connected it to 3.3v. Now it works without problems (yet). Thanks for your quick responses and have a good day! \$\endgroup\$ – ItsKasper Aug 8 '17 at 18:16
0
\$\begingroup\$

Most tutorials say that you have to have only two wires connected to the button: one from the GPIO pin to the button, and one from the button to ground. This didn't work for me like it should. I still have the 'GPIO wire' connected to the button, but alongside there's a 10k resister going to ground. The other sound of the button I connected to 3.3v. Make sure that you don't use anything over 3.3v, because the GPIO pins aren't capable of handling anything above 3.3v!

\$\endgroup\$

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.