RuntimeError: No access to /dev/mem

依然范特西╮ 提交于 2019-12-31 04:50:09

问题


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!

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!


回答1:


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.



来源:https://stackoverflow.com/questions/18389013/runtimeerror-no-access-to-dev-mem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!