If your python code is in a file named test.py
in /home/user
try to run:
$ sudo python /home/user/test.py
(adjust accordingly to your environment).
Pregunta
I just got a raspberry pi and have been enjoying it. I am trying to get started with some simple tutorials and am having a problem. The tutorial is this :
http://www.rpiblog.com/2012/09/using-gpio-of-raspberry-pi-to-blink-led.html . But, when I try to run the code I get the error
Traceback (most recent call last): File "/home/pi/Desktop/BlinkgLed.py", line 13, in GPIO.setup(11, GPIO.OUT) RuntimeError: No access to /dev/mem. Try running as root!
The code I am running is
import RPi.GPIO as GPIO
import time
# blinking function
def blink(pin):
GPIO.output(pin,GPIO.HIGH)
time.sleep(1)
GPIO.output(pin,GPIO.LOW)
time.sleep(1)
return
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
# set up GPIO output channel
GPIO.setup(11, GPIO.OUT)
# blink GPIO17 50 times
for i in range(0,50):
blink(11)
GPIO.cleanup()
I tried doing this
sudo python
import RPi.GPIO as GPIO
import time
# blinking function
def blink(pin):
GPIO.output(pin,GPIO.HIGH)
time.sleep(1)
GPIO.output(pin,GPIO.LOW)
time.sleep(1)
return
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
# set up GPIO output channel
GPIO.setup(11, GPIO.OUT)
# blink GPIO17 50 times
for i in range(0,50):
blink(11)
GPIO.cleanup()
And now I get the error
Invalid Syntax
I also tried this
sudo idle3
and I got this
Client is not authorized to connect to ServerTraceback (most recent call last): File "/usr/bin/idle3", line 5, in main() File "/usr/lib/python3.2/idlelib/PyShell.py", line 1405, in main root = Tk(className="Idle") File "/usr/lib/python3.2/tkinter/init.py", line 1701, in init self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) _tkinter.TclError: couldn't connect to display ":1.0”
Any thoughts on how to solve any of these?
BTW I installed GPIO by this tutorial :
https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/
and was able to make led’s tp turn on and off by code. So I don’t think thats the problem.
Solución
If your python code is in a file named test.py
in /home/user
try to run:
$ sudo python /home/user/test.py
(adjust accordingly to your environment).
Otros consejos
You need to open a terminal window and type in...
sudo idle [Then press return]
After that you can go back to the Python window and write the code you need.
Hope that helps.
The following bash script is about blinking GPIO2 with regard to the user defined time by taking three input variables for amount of hours, minutes and seconds respectively.
#!/bin/bash
cd /sys/class/gpio
echo 2 > export
cd gpio2
echo out > direction
echo 0 > value
echo -n "enter hour : "
read hour
echo -n "enter minute : "
read min
echo 1 > value
for ((i=0;i<$min;i++))
do
sleep 1m
done
for ((i=0;i<$hour;i++))
do
sleep 1h
done
cd /sys/class/gpio
cd gpio2
echo 0 > value