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: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: 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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
ben_nuttall
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 231
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
Contact: Website

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.
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero and creator of piwheels

Return to “General discussion”