2

I have looked at all the posts about this error, but none have cured it for me. I am using a ChronoDot RTC on an old Pi B+ with the SQW Wave Output going to Pin 8 which I use to update an LCD display every second with the time and my NTP GPS Server stats. It is a nice accurate way to do it. Every second is shown. This worked fine in Python 2.7 for 6 years, but the SSD finally died, so I installed the latest RaspiOS Lite on a new SSD, modified the script for Python 3 but I cannot get the interrupt/event detect to work. The relevant code is here:

import RPi.GPIO as GPIO
SQW_PIN = 8
GPIO.setmode(GPIO.BCM)
GPIO.setup(SQW_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(SQW_PIN, GPIO.FALLING)
GPIO.add_event_callback(SQW_PIN, update_time)

and also tried this:

import RPi.GPIO as GPIO
SQW_PIN = 8
GPIO.setmode(GPIO.BCM)
GPIO.setup(SQW_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(SQW_PIN, GPIO.FALLING, callback=update_time)

but they both give the same error:

GPIO.add_event_detect(SQW_PIN, GPIO.FALLING, callback=update_time)
RuntimeError: Failed to add edge detection

Why is this not running in Python 3 when it did in Python 2?

2
  • In the code above you have not set the pin number (just a variable) If this is partial code then you have to give us the exact failing code and the exact, full error messages (note that the error code cannot be the result of the first bit of code)
    – Dirk
    Jul 22, 2021 at 18:42
  • I do have the pin variable set in the original code, just trying to save space. I have added it now. As for the error code, I have not included the actual lines with line numbers to save space too
    – themetman
    Jul 22, 2021 at 18:47

1 Answer 1

1

This is caused by the Raspberry Pi's SPI0 interface reserving two pins for Chip Select (CS0 and CS1 on the Pi's pinout) instead of only one, which means that GPIO pins 7 (CS1) and 8 (CS0) are normally unreadable/inaccessible.

This can be fixed by adding the following directive to /boot/config.txt on the Pi:

dtoverlay=spi0-1cs,cs0_pin=7

The 1cs means that the Pi will now only reserve one pin for Chip Select.

Make sure to change cs0_pin=<pin number> to a pin that you are not using (if you need to access pin 7, change it to 8, etc.)

More info about dtoverlay: https://github.com/raspberrypi/firmware/blob/83dafbc92c0e63f76ca2ecdd42462d56489d1c77/boot/overlays/README#L4040

Your Answer

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

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