svmystique
Posts: 23
Joined: Sun Feb 03, 2013 12:47 am

GPIO programming question

Sat Feb 16, 2013 12:51 am

I'm new to the Raspberry Pi and trying to get a handle on GPIO programming. I'm using examples in Raspberry Pi User Guide by Halfacree and Upton. I've set up the libraries, Pi and breadboard as indicated in the Guide. When I test by wiring the LED directly to the 3.3 V on the GPIO connector the LED lights.

This is the program I'm trying to execute:

import RPi.GPIO as GPIO
import time

while True:
Print “on”
GPIO.output(11,True)
time.sleep(2)
Print “off”
GPIO.output(11,False)
time.sleep(2)

I inserted some print commands to be sure that the program was indeed stepping through the instructions. It was, but the LED did not blink. No error messages were generated.
Thanks

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: GPIO programming question

Sat Feb 16, 2013 2:08 am

You need to call the GPIO initialization and if you are going to use a GPIO pin as an output, you must first set the output mode for the GPIO.

Code: Select all

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time

# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)

# Set pin 11 as an output
GPIO.setup(11, GPIO.OUT)

while True:
    print “on”
    GPIO.output(11,True)
    time.sleep(2)
    print “off”
    GPIO.output(11,False)
    time.sleep(2)
Be aware that depending on how you have the LED (with resistor) wired, that a low (False) level may be "on" and a high (True) level may be off. With the LED anode connected to 3.3V and the cathode connected to the GPIO via a resistor, low is on and high is off. With the LED cathode connected to ground and the anode connected to the GPIO via a resistor, high is on and low is off.

Also, use the "Code" tag button when posting code to preserve the formatting.

Enjoy!
Bill

svmystique
Posts: 23
Joined: Sun Feb 03, 2013 12:47 am

Re: GPIO programming question

Sat Feb 16, 2013 3:40 am

Thanks, Bill, will give it a try.

Return to “Beginners”