0

I am coding a datalogger using a Raspberry Pi and am starting to test my code but now keep getting an error saying 'RuntimeError: Failed to add edge detection'. Here is my code:

import RPi.GPIO as GPIO
import datetime
from datetime import timedelta
import csv
from csv import writer

# endTime = datetime.datetime.now() + datetime.timedelta(seconds=60)



def my_callback(channel):
     data_writer([GPIO.input(40), datetime.datetime.now()])      # writes the detected
     # edge event to a CSV file with columns of rise/fall and the date & time of detection


if __name__ == '__main__':

    GPIO.setmode(GPIO.BOARD)       # set up GPIO numbering (BOARD = physical pins on Pi)
    GPIO.setup(40, GPIO.IN)         # set physical pin 40 as input


# create CSV file to write edge detection data to called Datalogger, with data on new line
    with open('/media/pi/KINGSTON/datalogger.csv', 'w', newline='') as Datalogger:
        data_writer = writer(Datalogger)

        data_writer.writerow(["Edge type", "Date & Time"])       # create headers in CSV file


        GPIO.add_event_detect(40, GPIO.BOTH, callback=my_callback)     # edge detection function
        # detects both rise and fall in signal on pin 40, then runs my callback


        while True:
# main program loop here - loops detection of edges then runs my callback
          # if datetime.datetime.now() >= endTime:
               #break
          time.sleep(0.01)
              # exit()

Does anyone know how to fix this? I've seen that I might need to run this as root but am not sure how to do so.
Many thanks.

  • Your code runs on my Pi3B+ (had to add 'import time') although I haven't got anything connected to pin 40. Are you running the latest Raspbian Stretch fully updated? – CoderMike Oct 29 '18 at 14:45
  • Is SPI disabled under Interfaces in Raspberry Pi Configuration as that is also on pin 40 pinout.xyz/pinout/pin40_gpio21 ? – CoderMike Oct 29 '18 at 14:54
  • I have version 4.14.71-v7 installed. How would I run this script as root user? (I'm new to python). I haven't disabled SPI I will see if that works thanks. – user93154 Oct 29 '18 at 14:57
  • SPI has always been disabled by the looks of it. Do I need to enable remote access of GPIO? – user93154 Oct 29 '18 at 15:02
  • Should not need to run as root, no remote access of GPIO not required. – CoderMike Oct 29 '18 at 15:05
0

The following seems to work reliably on my Pi3B with a button connected to pin 40 with a resistor to 3.3V. If I use Thonny this can re-introduce the 'failed to add edge detection error', running from command line seems to work fine.

I've added GPIO.PUD_DOWN to pull pin 40 low as in my case it should normally be low unless pressed.

I've added bouncetime=10 to the add_event_detect - don't fully understand the need for this however some posts suggest it helps solve a threading issue.

import RPi.GPIO as GPIO
import datetime
from datetime import timedelta
import csv
from csv import writer
import time

# endTime = datetime.datetime.now() + datetime.timedelta(seconds=60)

def my_callback(channel):
     data_writer.writerow([GPIO.input(40), datetime.datetime.now()])      # writes the detected
     # edge event to a CSV file with columns of rise/fall and the date & time of detection


if __name__ == '__main__':

    GPIO.setmode(GPIO.BOARD)       # set up GPIO numbering (BOARD = physical pins on Pi)
    GPIO.setup(40, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)         # set physical pin 40 as input


# create CSV file to write edge detection data to called Datalogger, with data on new line
    with open('/home/pi/datalogger.csv', 'w', newline='') as Datalogger:
        data_writer = writer(Datalogger)

        data_writer.writerow(["Edge type", "Date & Time"])       # create headers in CSV file


        GPIO.add_event_detect(40, GPIO.BOTH, callback=my_callback, bouncetime=10)     # edge detection function
        # detects both rise and fall in signal on pin 40, then runs my callback


        while True:
# main program loop here - loops detection of edges then runs my callback
          # if datetime.datetime.now() >= endTime:
               #break
          time.sleep(0.01)
              # exit()
| improve this answer | |
  • Thanks a lot! I'll give mine a try tomorrow and see how it goes. I think the bouncetime is due to the connections of the battery once pressed. It can detect slight connections and disconnections when you press the button making it think its been pressed multiple times whereas it's been pressed once with just slightly inaccurate contacts within the button. So the bouncetime is a type of delay I think. – user93154 Oct 29 '18 at 17:10
  • Hi @CoderMike, I'm having trouble running it again. Can you explain how and where you're running the code from? Thanks. – user93154 Oct 30 '18 at 10:49
  • Run from a terminal window 'python3 test.py' – CoderMike Oct 30 '18 at 15:10
  • That doesn't work for me. It says directory or file does not exist. In the terminal I've also done 'cd <path to my python script>' then 'python3 DataloggerV2.py'. This runs the program but still gives the runtime error: failed to add event. Is there something else I'm missing? – user93154 Oct 30 '18 at 15:19
  • Did you do a fresh reboot, then run the python program? What is actually connected to pin 40? – CoderMike Oct 30 '18 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy