RuntimeError:无法访问/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之前忘记将RED设置为输出.该错误信息并不能帮助解决此问题.下次,我需要记住在尝试使用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.
这篇关于RuntimeError:无法访问/dev/mem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!