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.