User avatar
ludo75
Posts: 31
Joined: Sun Oct 16, 2016 1:08 pm
Location: Wonderland

gpio buttons

Sun Jan 01, 2017 5:19 pm

Code: Select all

#!/usr/bin/python

import RPi.GPIO as GPIO
import os
GPIO.setmode(GPIO.BCM)

GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_UP)
input = GPIO.input(8)
while True:
    GPIO.wait_for_edge(8, GPIO.RISING)
    os.system("pkill thing;/home/pi/thingy&")
GPIO.cleanup()
The above is part of alot more, but somewhere in the above lies the problem:
/home/pi/thingy starts thing, and the problem is that when i push "8" i get two instances of thing.
Nothing crucial like the control of nuclear facilities, but anyway i'd like to stop at one instance.
i've added "pkill thing" in /home/pi/thingy in two places a line 1 and just before i start "thing" at the end

btw thing is and audioplayer
do yall get the thing ;)

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12585
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: gpio buttons

Sun Jan 01, 2017 5:24 pm

sounds like contact bounce, your switch is producing more than one flank.

Image

User avatar
ludo75
Posts: 31
Joined: Sun Oct 16, 2016 1:08 pm
Location: Wonderland

Re: gpio buttons

Sun Jan 01, 2017 5:38 pm

what do i do about it, switch out the buttons?

User avatar
davidcoton
Posts: 4531
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: gpio buttons

Sun Jan 01, 2017 6:52 pm

There's a LONG thread about it here somewhere, I'm sure Google can find it if no-one knows the reference offhand.

Long story short, there are hardware fixes and software fixes. Quick hardware fix is to put a small capacitor (10nF or 100nF) between the GPIO and ground. If that doesn't work, find that thread.... :lol:
Signature retired

jondallimore
Posts: 229
Joined: Thu Aug 04, 2016 11:02 pm

Re: gpio buttons

Sun Jan 01, 2017 6:55 pm

put a pullup or pulldown resistor into the switch circuit to keep it high or low when switch is not pressed, or a small capacitor to debounce the switch

or - not sure of programming, but could it launch 'thingy' and then wait for the GPIO to change state again before continuing - ie, push switch GPIO goes high, thingy is launched, wait half a second or so, then continue program only when GPIO goes low?

User avatar
bensimmo
Posts: 4225
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: gpio buttons

Sun Jan 01, 2017 7:34 pm

Have a look at using GPIOZERO https://gpiozero.readthedocs.io (recent Raspian images come with it), it works well and has a lot of button stuff and bounce times.

User avatar
ludo75
Posts: 31
Joined: Sun Oct 16, 2016 1:08 pm
Location: Wonderland

Re: gpio buttons

Sun Jan 01, 2017 10:57 pm

jondallimore wrote:put a pullup or pulldown resistor into the switch circuit to keep it high or low when switch is not pressed, or a small capacitor to debounce the switch

or - not sure of programming, but could it launch 'thingy' and then wait for the GPIO to change state again before continuing - ie, push switch GPIO goes high, thingy is launched, wait half a second or so, then continue program only when GPIO goes low?
a waitstate it is at first :)

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: gpio buttons

Mon Jan 02, 2017 9:43 am

Just add

Code: Select all

, bouncetime=300
to your wait for edge command

The other thing that jumped out at me was you are waiting for a rising edge and using a pullup at the same time. So technically you are not detecting a button press, but a button release. I don't know if it matters or makes any difference at all. Just thought I'd mention it.
Alex Eames RasPi.TV, RasP.iO

Return to “General discussion”