projectcornholio
Posts: 5
Joined: Fri Dec 30, 2016 4:59 am

Unable to use GPIO04/Pin7 as Interrupt?

Wed Feb 22, 2017 8:23 pm

Hey guys,

I've been working hard on learning how to use and interface with the pi! It's been great so far but I've been unable to get past this issue on my own. I've done online research but can't find anything... :?

So I'm working on a larger project but in an effort to troubleshoot the issue, I've written a little code to demonstrate the issue. I keep getting the error:

GPIO.add_event_detect(4, GPIO.FALLING, callback=buttonpressed, bouncetime=300)
RuntimeError: Failed to add edge detection

I realise that GPIO04/Pin7 is the GCLK so that could be the issue but I need a way around it because my project is currently using all other available pins (except pins 27 and 28). Below is my code that returns this error.

Please help! Any response is appreciated!

All I'm needing is for the button to interrupt the main thread, do its own thing and then resume the main thread.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def buttonpressed(channel):
    print("\n")
    print('Button Was Pressed')

GPIO.add_event_detect(4, GPIO.FALLING, callback=buttonpressed, bouncetime=300)

while 1:
    print('waiting...')
    time.sleep(1)
    

mattmiller
Posts: 2245
Joined: Thu Feb 05, 2015 11:25 pm

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 7:13 am

The problem is that your using a pull-down resistor AND looking for a falling edge

Since the input is low - then it can't fall any further :)

projectcornholio
Posts: 5
Joined: Fri Dec 30, 2016 4:59 am

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 2:40 pm

Thanks for the response and good catch! That was a stupid typo on my end. Sadly, this didn't fix the issue though. :x

I have re-written the code to be more generic and pasted it below. I do hope that I'm making some mistake that I continuously overlook. I get the same error as before:

GPIO.add_event_detect(7, GPIO.RISING, callback=my_callback)
RuntimeError: Failed to add edge detection

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

## Pin 7 is going to 3.3 PWR through the button

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)



def my_callback(channel):
    print("\n")
    print('Button Was Pressed')

GPIO.add_event_detect(7, GPIO.RISING, callback=my_callback)


try:
    while 1:
        print('waiting...')
        time.sleep(1)

except KeyboardInterrupt:  
    GPIO.cleanup()

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 2:45 pm

Works for me.

Code: Select all

$ python q.py
waiting...
waiting...


Button Was Pressed


Button Was Pressed
waiting...
waiting...


Button Was Pressed
waiting...
waiting...
^C$ 

projectcornholio
Posts: 5
Joined: Fri Dec 30, 2016 4:59 am

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 2:56 pm

Well... that's confusing. So maybe it's board related? Do you guys think it's hardware? I can try testing pin7 to make sure it still goes high and low properly. Maybe give the board an update and keep my fingers crossed?

mattmiller
Posts: 2245
Joined: Thu Feb 05, 2015 11:25 pm

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 3:01 pm

Its quite easy (unless hardwired while power is off) to accidentally connect to pin2 instead of pin1 :( (Done it twice myself)

I now ALWAYS enable pull-ups and connect the other end of switches to gnd

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 3:04 pm

Maybe give the board an update
The hardware of an RPi board cannot be updated. It contains no 'flash' or any other form of non-volatile persistent memory. Other than a very small number of bits - literally bits - of One Time Programmable configuration (OTP) your RPi cannot be modified from the way it left the factory.

pcmanbob
Posts: 9612
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 4:13 pm

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

## Pin 7 is going to 3.3 PWR through the button

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
Check you have your switch connected to the right GPIO pin
it should be connected to pin 7 not GPIO 7 as you are using board mode
Image
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 4:55 pm

Check your version of RPi.GPIO is up to date. Mine is 0.6.3 (GPIO.VERSION). It seems likely you are using an ancient version of the module.

mattmiller
Posts: 2245
Joined: Thu Feb 05, 2015 11:25 pm

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 5:12 pm

It seems likely you are using an ancient version of the module
What's made you say that?

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 5:50 pm

mattmiller wrote:
It seems likely you are using an ancient version of the module
What's made you say that?
The error message.

GPIO.add_event_detect(7, GPIO.RISING, callback=my_callback)
RuntimeError: Failed to add edge detection

I don't know the condition used to raise the error but it sounds like a misconfiguration. Or perhaps the GPIO is shot, but I think it unlikely that Linux or RPi.GPIO would test for that possibility during GPIO initialisation.

mattmiller
Posts: 2245
Joined: Thu Feb 05, 2015 11:25 pm

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 8:54 pm

The error message.
I completely missed that they were getting an error msg
Twice :(
Silly me!

projectcornholio
Posts: 5
Joined: Fri Dec 30, 2016 4:59 am

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 10:13 pm

Thanks for the help guys. I just updated my Rpi.GPIO and it says its already up to date :/ See image attached.

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Feb 23, 2017 10:23 pm

projectcornholio wrote:Thanks for the help guys. I just updated my Rpi.GPIO and it says its already up to date :/ See image attached.
What is given by print(GPIO.VERSION) ?

morgannickraspi
Posts: 2
Joined: Thu Jul 20, 2017 2:54 pm

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Jul 20, 2017 2:58 pm

Panic ye not - there's probably nothing wrong with your PI. I had the exact same issue and it can be fixed by updating your PI to the latest version:

Code: Select all

sudo apt-get update
sudo apt-get dist-upgrade
Have fun!
Last edited by morgannickraspi on Fri Jul 21, 2017 5:11 am, edited 1 time in total.

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

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Jul 20, 2017 4:31 pm

DO NOT RUN rpi-update it is not a way to update the.
It is used for testing new stuff and fixes/things that might break it however.
Last edited by bensimmo on Thu Jul 20, 2017 4:35 pm, edited 1 time in total.

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

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Jul 20, 2017 4:33 pm

One thing I will say is check you do not have 1-Wire enabled, as that will 'load up' on gpio4 (Pin7)

Check the simple code works by putting it on another pin for testing.

User avatar
DougieLawson
Posts: 39303
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Jul 20, 2017 9:01 pm

morgannickraspi wrote:Panic ye not - there's probably nothing wrong with your PI. I had the exact same issue and it can be fixed by updating your PI to the latest version:

Code: Select all

sudo apt-get update
sudo apt-get dist-upgrade
sudo rpi-update
Have fun!
There is no reason for general users to EVER run rpi-update. Please don't suggest it, it should be used ONLY when advised by an expert. Anyone who does run it MUST know how to rebuild their system when something bad goes wrong.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

morgannickraspi
Posts: 2
Joined: Thu Jul 20, 2017 2:54 pm

Re: Unable to use GPIO04/Pin7 as Interrupt?

Thu Jul 20, 2017 9:48 pm

Apologies all if I've given bum info. Thanks for the rip-update heads up.

The update and dist-upgrade will fix "RuntimeError: Failed to add edge detection" problem though.

[quote="DougieLawson"][quote="morgannickraspi"]Panic ye not - there's probably nothing wrong with your PI. I had the exact same issue and it can be fixed by updating your PI to the latest version:

Code: Select all

sudo apt-get update
sudo apt-get dist-upgrade
Have fun!

Return to “Troubleshooting”