运行时错误:无法访问/dev/mem [英] RuntimeError: No access to /dev/mem
问题描述
我一直在尝试使用 Python GPIO PWM 来控制一组连接到我的 RPi 的 LED.当我运行 Python 脚本时,出现以下错误:
I have been trying to use the Python GPIO PWM to control a set of LEDs connected to my RPi. When I run the Python script, I get the following error:
Traceback (most recent call last):
File "cycle.py", line 12, in <module>
r = GPIO.PWM(f, RED)
RuntimeError: No access to /dev/mem. Try running as root!
我尝试以 root 身份运行脚本(使用 sudo 和实际以 root 身份登录).所有其他 GPIO 功能都正常工作,我尝试通过 apt 进行更新和卸载/重新安装 python-rpi.gpio.这是我一直在运行的代码.
I have tried running the script as root (both with sudo and with actually logging in as root). All of the other GPIO functions work correctly and I have tried doing an update and uninstalling/reinstalling python-rpi.gpio through apt. Here is the code I have been running.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
RED = 11
f = 100
r = GPIO.PWM(RED, f) <== Where it crashes
r.start(0)
try:
while 1:
for dc in range(0, 101, 5):
r.ChangeDutyCycle(dc)
time.sleep(0.1)
for dc in range(100, -1, 5):
r.ChangeDutyCycle(dc)
time.sleep(0.1)
except:
pass
r.stop()
GPIO.cleanup()
它基于此处的示例,但仍然可能存在错误.我已经为此苦苦挣扎了很长时间,因此将不胜感激所提供的任何帮助.谢谢!
It is based off of the example found here, but there could still be bugs. I have been struggling with this for quite a bit now so any help provided would be greatly appreciated. Thanks!
推荐答案
上面代码的问题是我在尝试使用它之前忘记将 RED 设置为 at 输出.该错误消息无助于解决此问题.下次,我需要记住在尝试使用它们之前将 PWM 引脚设置为输出.
The problem is with the code above is that I forgot to set RED to at output before trying to use it. The error message did not help resolve this problem. Next time, I need to remember to setup PWM pins as outputs before trying to use them.
这篇关于运行时错误:无法访问/dev/mem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!