RuntimeError: No access to /dev/mem

2019-02-28 16:03发布

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条回答
叛逆
2楼-- · 2019-02-28 16:17

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.

查看更多
登录 后发表回答