如何修复 Raspberry Pi GPIO "No access to /dev/mem. Try running as root!"
问题:
你想使用 RPi.GPIO 切换 Raspberry Pi GPIO 引脚,但你看到类似这样的错误消息
gpio-error.txt
Traceback (most recent call last):
File "gpio.py", line 8, in <module>
GPIO.setup(pin1, GPIO.OUT)
RuntimeError: No access to /dev/mem. Try running as root!解决方案
将你的用户添加到 gpio 组:
add-user-to-gpio-group.sh
sudo usermod -a -G gpio $USER然后注销并重新登录(如果不奏效则重启)。
此错误的原因是 /dev/gpiomem 只能由属于 gpio 组的用户访问。默认情况下,只有 pi 用户属于 gpio 组 - 因此,如果你以 pi 或 root 以外的任何用户身份运行脚本,你将看到该错误消息。
Check out similar posts by category:
Electronics, Raspberry Pi
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow