Page 1 of 1

Python GPIO

Posted: Fri Jan 18, 2013 6:11 pm
by thatbakerguy
I'm trying to write my first python program so i started with the example in the Education Manual. The code is just supposed to blink an LED when you hit enter here's the code

Code: Select all

 import RPi.GPIO as GPIO

#set up pin 11 to output
GPIO.setup(11, GPIO.OUT)

state = False

while 1:
    GPIO.output(11, state)
    command = input("Press return to switch the led on/off or Q to quit")
    if command.strip().upper().startswitch("Q"):
        break
    state = not state 
when i try to run it from the command line with

Code: Select all

 sudo python led.py
i get this error.

Code: Select all

Traceback (most recent call last):
  File "ledd.py", line 4, in <module>
    GPIO.setup(11, GPIO.OUT)
RPi.GPIO.ModeNotSetException: Please set mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

I tried replacing .setup with setmode and other things but i cant get it to work any suggestions?

Re: Python GPIO

Posted: Fri Jan 18, 2013 6:16 pm
by MattHawkinsUK
Stick the following on the line after your import line :

GPIO.setmode(GPIO.BOARD)

This tells it you are using Pin 11 on the GPIO header.

Re: Python GPIO

Posted: Fri Jan 18, 2013 6:38 pm
by thatbakerguy
Okay i did that and it helped a little. now when i run it i get this error

Code: Select all

led.py:7: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(11, GPIO.OUT)
Press return to switch the led on/off or 'Q' to quit
and then when i hit enter i get

Code: Select all

Traceback (most recent call last):
  File "led.py", line 14, in <module>
    if command.strip().upper().startswitch('Q'):
AttributeError: 'str' object has no attribute 'startswitch'

Re: Python GPIO

Posted: Fri Jan 18, 2013 7:00 pm
by bgreat
The first warning can be ignored. To eliminate the warning on subsequent execution of your program, add the following at the end of your script:

Code: Select all

GPIO.cleanup()
If you read your error message carefully and compare it to the code you were trying to type in, you will see that you have a typo. You typed "startswitch" and meant "startswith". Correct this and it should work for you.

Enjoy!
Bill

Re: Python GPIO

Posted: Fri Jan 18, 2013 7:04 pm
by bgreat
Also, the reason for your needing to add the GPIO.setmode() call at the beginning of your application is due to an update to the GPIO module to support both board connect pin numbering and Broadcom GPIO numbering schemes. When this was added it became necessary to add the initialization call to indicate the numbering scheme in use by the application.

GPIO.setmode(GPIO.BCM) --> Broadcom GPIO numbering
GPIO.setmode(GPIO.BOARD) -- board pin numbering

Enjoy!
Bill

Re: Python GPIO

Posted: Fri Jan 18, 2013 7:06 pm
by croston
Education guide errata available here:
http://www.raspberrypi.org/phpBB3/viewt ... ta#p249673

Re: Python GPIO

Posted: Fri Jan 18, 2013 9:07 pm
by thatbakerguy
awesome thanks guys, got it working.