dezmob
dezmob

Reputation: 13

RPi.GPIO add_event_detect are sometimes ignored

I'm working on a project that is using GPIO(BCM) 17, 27, 22, 5 and 6.

Events on pin 17 are detected pretty well, but GPIO 27 is really reluctant to trigger events. they seems to get detected with a bit of delay...

I'm using a RPI4 with buster lite.

This is a video showing the issue

https://youtu.be/bLXQkA1bzKA

there is a photo of the wiring

This is the snippet that I'm using in the video in python3

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def my_callback(channel):
        print("event!")
GPIO.add_event_detect(27, GPIO.FALLING, callback=my_callback, bouncetime=300)

while True:
    pass

Is there any better methods to detect button press? are there GPIO that must be avoided in the RaspberryPi platform ? Or maybe I'm doing something completely wrong...?

Upvotes: 1

Views: 1607

Answers (4)

fguillen
fguillen

Reputation: 38878

I had the same issue. It was fixed by replacing GPIO by gpiozero.

Upvotes: -1

Joe Rock
Joe Rock

Reputation: 1

Look you guys. bouncetime does not work the way you want. As soon as the event is triggered weather it be a button press or noise the bouncetime starts. So by setting a high bouncetime you may be waiting longer than the button press.

You need a short hardware bouncetime like 1 or 5 and then do a double software debounce. Something like....

    time.sleep(.060)
    if not GPIO.input(pin):
            time.sleep(.060)
            if not GPIO.input(pin):
                        A button was really pushed.

All in .125 seconds. 5 hardware bouncetime + 2 .06 second = .125 Which is about the time the button was in contact.

This makes sure the button is still off which is pressed in a pull up button.

Also note. I like to remove the event detect at the beginning of the handler. Then adding a 2 second sleep. Then turn the event detect back on. This keep from doube presses.

In your case

           GPIO.remove_event_detect(27)

I hope I help someone in the future as this was a hard lesson to learn. You think buttons are simple.

Upvotes: 0

Sandra
Sandra

Reputation: 26

I already had a Pi with some strange IO behaviour. It was from the early batch of the Pi4. Keep in mind that Raspberry Pi are really cheaply made and even the slightest noise in the PSU or just bad luck in the silicon lottery can make the Pi an unreliable device. Just to be sure, swap the PSU (even if it's an official one) and if this doesn't solve the problem just ask for an RMA to your provider.

Upvotes: 0

NWiogrhkt
NWiogrhkt

Reputation: 111

You have set the bounce time to 300 ms, which is quite long. Try 30 ms, then your program will react much more reliably.

The event is called only if there is a change in the signal after the bounce time (in your case 300 ms).


Also please try:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def my_callback(channel):
    print("event!")

GPIO.add_event_detect(27, GPIO.FALLING, callback=my_callback, bouncetime=300)

while True:
    time.sleep(0.01)

Some strange behavior is caused by the load of a loop without content.

Upvotes: 0

Related Questions