rmulukutla
Posts: 1
Joined: Sun Dec 20, 2015 1:36 am

New to Raspberry Pi. Help with programming GPIO pins

Sun Dec 20, 2015 8:37 pm

I just brought a Raspberry Pi Model B and I am new to Raspberry Pi and Python. I am having difficulty turning on and off an LED using a Python program below. I have anode (+) of the LED connected to GIPO pin 7 and Cathode (-) of the LED connected to a 330 ohm resistor and which in turn is connected to GIPO pin 6. I ran the python program from LXTerminal using command sudo idle. The LED does not turn on and Off. I am sure I am doing something wrong. I appreciate any help on this,


import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
for x in range(0,3):
GPIO.output(7,True)
time.sleep(1)
GPIO.output(7,False)
time.sleep(1)
GPIO.cleanup()

User avatar
DougieLawson
Posts: 40194
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: New to Raspberry Pi. Help with programming GPIO pins

Sun Dec 20, 2015 8:55 pm

Start using GPIOzero. See http://raspi.tv/2015/gpio-zero-test-dri ... f-security for instructions.
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
ben_nuttall
Posts: 235
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridgeshire, UK
Contact: Website Twitter

Re: New to Raspberry Pi. Help with programming GPIO pins

Sat Jan 02, 2016 4:16 pm

First of all, you'd better test the LED works without worrying that your code is wrong. Try connecting it to GND and 3V3 to see if it comes on. 3V3 is basically a GPIO pin that's always on. If you can't get it to come on that way, your LED is probably broken.

Assuming your LED does work, perhaps you're not connected to the GPIO pin you think you are?

The code you're using contains "GPIO.setmode(GPIO.BOARD)" which tells the program you're using the physical pin number layout (i.e. 1 to 40 in order) rather than the recommended BCM mode. I'd change your code to say "GPIO.setmode(GPIO.BOARD)" and choose a GPIO pin. See a pinout diagram here: http://pinout.xyz/ and use the BCM GPIO numbers.

Also, as Dougie says, GPIO Zero is a much simpler solution.
Former RPF staff. Author of gpiozero and creator of piwheels.

Return to “General discussion”