I'm new to RPI and to Python 3 and I have a problem running this code on a RPI 2.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
redled = 4
greenled = 26
GPIO.setup(redled, GPIO.OUT)
GPIO.setup(greenled, GPIO.OUT)
count = 1
while count < 100000:
GPIO.output(redled, 1)
GPIO.output(greenled, 1)
time.sleep(.1)
GPIO.output(redled, 0)
GPIO.output(greenled, 0)
time.sleep(.1)
count = count + 1
GPIO.cleanup()
I get this error.
Traceback (most recent call last): File "/home/pi/testreactionrepeat.py", line 7, in GPIO.setup(redled, GPIO.OUT) RuntimeError: No access to /dev/mem. Try running as root!
I was using this code with no problems a couple of days ago and can't figure out what I'm doing wrong now. I found a similar problem and the answer was to run "sudo su" but I get a syntax error when I try that.
-
Please edit your question and add the specific error sudo su is returning.Steve Robillard– Steve Robillard2015年08月20日 23:32:07 +00:00Commented Aug 20, 2015 at 23:32
1 Answer 1
Let's assume that your program is called
program.py
Open terminal and type
sudo python program.py
This will run your python program as root giving it the necessary permissions to access/modify the GPIO pins.
-
I get this when I type sudo python reactionrepeat.py into terminal. Traceback (most recent call last): File "reactionrepeat.py", line 3, in <module> GPIO.setmode(GPIO.BCM) AttributeError: 'module' object has no attribute 'setmode'nashville_bill– nashville_bill2015年08月21日 11:07:25 +00:00Commented Aug 21, 2015 at 11:07
-
can you run "sudo apt-get update && sudo apt-get upgrade" in terminal, this will update your system with the latest software and hopefully with the latest RPi.GPIO modules and thuis fix this issue.evolutionizer– evolutionizer2015年08月21日 11:33:07 +00:00Commented Aug 21, 2015 at 11:33
-
I did the update/upgrade, and it appeared to go well. However, my code still won't run, even though it ran just fine a couple of days before I had the problem. I still get the same messages as before when I run the program and when I type in sudo python reactionrepeat.py. Could this be a hardware problem?nashville_bill– nashville_bill2015年08月21日 21:53:34 +00:00Commented Aug 21, 2015 at 21:53
-
@nashville_bill no this is definitely a software problem. Are you on the latest raspbian OS?evolutionizer– evolutionizer2015年08月22日 03:22:22 +00:00Commented Aug 22, 2015 at 3:22
-
I'm getting this experience with Ubuntu Mate 15.10 when trying to use dot3k.user43492– user434922016年03月20日 17:10:10 +00:00Commented Mar 20, 2016 at 17:10