Search code examples
pythonraspberry-pigpio

Raspberry Pi blinking LED light errors


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.


Solution

  • 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).