I am trying to run a program using GPIO from a normal user. I checked that /dev/gpiomem
has the correct permission (root:gpio 0660
) and that my user is member of group gpio
.
The following works correctly:
import RPi.GPIO as GPIO
from time import sleep
try:
pin = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.IN)
while True:
pin_value = GPIO.input(pin)
print("Pin {} has value {}".format(pin, pin_value))
sleep(1)
finally:
GPIO.cleanup()
When I press a button attached to pin 21 it does indeed show 1, and 0 otherwise. I run this as a standard user, so I guess it uses /dev/gpiomem
(and indeed it failed before I fixed its permissions).
However, if I try to set an event on such pin
import RPi.GPIO as GPIO
from time import sleep
try:
pin = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.IN)
GPIO.add_event_detect(pin, GPIO.RISING)
GPIO.add_event_callback(pin, lambda _: print("Pin {} was pressed".format(pin)))
while True:
pass
finally:
GPIO.cleanup()
it raises the following exception:
Traceback (most recent call last):
File "tst.py", line 9, in <module>
GPIO.add_event_detect(pin, GPIO.RISING)
RuntimeError: Failed to add edge detection
Everything works fine using root, but I want to run this program as a standard user. Kernel is 4.14.37 and RPi.GPIO is 0.6.3. I am using a RPi 2.
It seems that adding an event on a pin still requires being root. Am I right? Am I doing anything wrong? How else could I detect a pin rising as a standard user?
/sys/gpio
interface to poll rising/falling events...