3

I'm currently having trouble with my project. I'm using a Raspberry Pi 3B+ with an InnoMaker Hifi Amp Hat and some push buttons.

import time
import signal
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

debounceTime = 300

btn1 = 24
btn2 = 25
btn3 = 13
btn4 = 12
btn5 = 16

def cb_btn1(channel):
    print('Button 1 pressed')

def cb_btn2(channel):
    print('Button 2 pressed')

def cb_btn3(channel):
    print('Button 3 pressed')

def cb_btn4(channel):
    print('Button 4 pressed')

def cb_btn5(channel):
    print('Button 5 pressed')

class GracefulExit:
    kill_now = False
    def __init__(self):
        signal.signal(signal.SIGINT, self.exit_gracefully)
        signal.signal(signal.SIGTERM, self.exit_gracefully)

    def exit_gracefully(self,signum, frame):
        self.kill_now = True

GPIO.setup(btn1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(btn2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(btn3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(btn4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(btn5, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.add_event_detect(btn1, GPIO.FALLING, callback=cb_btn1, bouncetime=debounceTime)
GPIO.add_event_detect(btn2, GPIO.FALLING, callback=cb_btn2, bouncetime=debounceTime)
GPIO.add_event_detect(btn3, GPIO.FALLING, callback=cb_btn3, bouncetime=debounceTime)
GPIO.add_event_detect(btn4, GPIO.FALLING, callback=cb_btn4, bouncetime=debounceTime)
GPIO.add_event_detect(btn5, GPIO.FALLING, callback=cb_btn5, bouncetime=debounceTime)

def main():
    app_killer = GracefulExit()
    while not app_killer.kill_now:
        try:
            time.sleep(0.5)
        except BaseException:
            GPIO.cleanup()
            print "Encountered an axeption."
            break
    print "End of the program."
    GPIO.cleanup()

if __name__ == '__main__':
    main()

I've tried with external pull-up/down and also adding a 0.1uF cap between 3.3V / GND and the GPIO.

I'm not using any pins that the Hat is supposed to use (GPIOs 18 to 21) according to the manual

My setup us currently on a breadboard, maybe I'm getting EMI with the jumpers wires...

I'm using a good quality power supply so I hope the problem isn't coming from here... For reference it's a Mean Well GSM40A12-P1J

I've discovered that touching the metal Ethernet/USB port with the metal tip of an USB flash drive or the tips of my multimeter probe trigger almost all my GPIO each times.

Is there something that I'm missing to get reliable input?

Schematics Pi header Breadboard pictures


Edit: Ok I'm slowly putting the pieces together. In order to make pigpiod running on a non full raspbian image in addition to sudo apt install pigpiodI had to run sudo apt install python3-pigpio

14
  • I wouldn't expect that problem with the set up as described. Could we have a clear photo showing the connections to the Pi?
    – joan
    Oct 16, 2019 at 17:46
  • I've removed the GPIO protection resistors since they aren't mandatory and make the setup clearer on the pictures
    – Sébastien
    Oct 16, 2019 at 18:10
  • Sorry I'm not familiar with pigpiod, I've installed it via sudo apt install pigpiodand running via sudo pigpiod. Trying to run the script tells me ImportError: No module named pigpio. Is there any additional package to install? I can't see on PyPl any python module called pigpio only apigpio but I'm assuming that this is totally different?
    – Sébastien
    Oct 17, 2019 at 2:13
  • I'm running Raspbian Lite, if I ps -auxI can see root 2400 6.3 0.1 9976 1632 ? SLsl 04:07 0:12 pigpiod but then when I try python monitor.pyI have Traceback (most recent call last): File "monitor.py", line 12, in <module> import pigpio ImportError: No module named pigpio If I try sudo pigpiodagain I have 2019-10-17 05:49:58 initInitialise: Can't lock /var/run/pigpio.pid Can't initialise pigpio library
    – Sébastien
    Oct 17, 2019 at 3:51
  • You can say which GPIO to monitor. In this case the switches so run as ./monitor.py 12 13 16 24 25
    – joan
    Oct 17, 2019 at 8:42

1 Answer 1

1

To mitigate against unwanted events I suggest you use code similar to the following. Basically read the level of the GPIO in the callback to verify it is not a short term transient.

#!/usr/bin/env python

import time
import signal
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

buttons=[24, 25, 13, 12, 16]

def cb_buttons(channel):
   if GPIO.input(channel) == 0:
      btn = buttons.index(channel) + 1
      print("Button {} pressed".format(btn))

class GracefulExit:
    kill_now = False
    def __init__(self):
        signal.signal(signal.SIGINT, self.exit_gracefully)
        signal.signal(signal.SIGTERM, self.exit_gracefully)

    def exit_gracefully(self,signum, frame):
        self.kill_now = True

for btn in buttons:
   GPIO.setup(btn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
   GPIO.add_event_detect(btn, GPIO.FALLING, callback=cb_buttons)

def main():
    app_killer = GracefulExit()
    while not app_killer.kill_now:
        try:
            time.sleep(0.5)
        except BaseException:
            GPIO.cleanup()
            print "Encountered an axeption."
            break
    print "End of the program."
    GPIO.cleanup()

if __name__ == '__main__':
    main()
2
  • I'm letting the script running, I can't manage to get the event to trigger accidentally now when I'm inserting an USB thumb drive, that's already a win there
    – Sébastien
    Oct 18, 2019 at 14:20
  • It's being almost 24h and not a single false triggering with your improvement!
    – Sébastien
    Oct 19, 2019 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.