简体   繁体   English

无法添加边缘检测 - Raspberry Pi 3 GPIO

[英]Failed to add edge detection - Raspberry Pi 3 GPIO

I am using a Raspberry Pi 3 model B.我使用的是 Raspberry Pi 3 B 型。

I tried different approaches using both gpiozero and RPi.GPIO .我使用gpiozeroRPi.GPIO尝试了不同的方法。 The problem occurs regardless of the library used.无论使用何种库,都会出现此问题。

Here is an example of code that fails with gpiozero .这是gpiozero失败的代码gpiozero

from gpiozero import Button
from signal import pause

def handle():
    print("Pressed!")

button = None
while not button:
    try:
        button = Button(4, pull_up=True)
        button.when_pressed = handle
    except RuntimeError as e:
        print(e)
        pass

pause()

The line button = Button(4, pull_up=True) always raises a RuntimeError and the output of the program (running python3 ) is:button = Button(4, pull_up=True)总是引发RuntimeError并且程序(运行python3 )的输出是:

Failed to add edge detection
Failed to add edge detection
Failed to add edge detection
Failed to add edge detection
# ... it goes on for ages

I already tried to reinstall RPi.GPIO and gpiozero but it did not help.我已经尝试重新安装RPi.GPIOgpiozero但它没有帮助。

Here is the full traceback of the exception这是异常的完整追溯

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/input_devices.py", line 84, in __init__
    self.pin.when_changed = self._fire_events
  File "/usr/lib/python3/dist-packages/gpiozero/pins/__init__.py", line 240, in <lambda>
    lambda self, value: self._set_when_changed(value),
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 233, in _set_when_changed
    bouncetime=self._bounce)
RuntimeError: Failed to add edge detection

I also get the same problem on raspberry pi3 B with Arch_arm operating system.我在使用Arch_arm操作系统的raspberry pi3 B上也遇到了同样的问题。 It seems that this problem have nothing to do with your python code.似乎这个问题与您的python代码无关。


In Raspberrypi/linux system, You can only use the GPIO with root authority by default.在树莓派/linux系统中,默认只能使用root权限的GPIO。

The /dev/gpiomem instead of /dev/mem , can let user use GPIO with rootless. /dev/gpiomem而不是/dev/mem ,可以让用户使用 GPIO 与 rootless。 Of course you should make some changes.当然,你应该做一些改变。

  1. add new group name gpio && add your user account name to the group添加新组名gpio && 将您的用户帐户名添加到组中

    sudo group -r gpio sudo usermod -a -G gpio pi
  2. add udev rules to /etc/udev/rules.d/将 udev 规则添加到/etc/udev/rules.d/

     ls -l /etc/udev/rules.d -rw-r--r-- 1 root root 580 Aug 5 15:02 raspberrypi.rules

    You can add these rules below to the tail of the file raspberrypi.rules with sudo.您可以使用 sudo 将下面的这些规则添加到文件raspberrypi.rules的尾部。

     SUBSYSTEM=="bcm2835-gpiomem", KERNEL=="gpiomem", GROUP="gpio", MODE="0660" SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'" SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"

Happy coding.快乐编码。

running it with sudo should worksudo运行它应该可以工作

For example例如

sudo python test-gpio.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM