3

I'm trying to get started with GPIO, but I just can't get this LED to flash on and off, for some reason.

I'm using Raspbian on an old model B Pi with a 26 pin breakout board and a breadboard. The wiring is fine, the LED turns on but nothing happens when I run the code, it just stays on solid. I have a photo of the wired breadboard as well as the code. The LED IS lit, a bit hard to see as the pic was taken with a flash so you can see detail better. To be clear, the led does light up when the Pi is on.

Pi and wiring Close up Code running in LXTerminal The following is the code from the tutorial that I am using:

import RPi.GPIO as GPIO
import time
pinNum = 8
GPIO.setmode(GPIO.BCM) #numbering scheme that corresponds to breakout board and pin layout
GPIO.setup(pinNum,GPIO.OUT) #replace pinNum with whatever pin you used, this sets up that pin as an output
#set LED to flash forever
while True:
    GPIO.output(pinNum,GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output(pinNum,GPIO.LOW)
    time.sleep(0.5)     
15
  • 1
    You can add the photo by editing your question, placing your cursor where yo want the photo to appear and clicking the mountain icon and following the prompts. To rule out the obvious did you reverse the LED (they have polarity - the longer leg is positive)? Also, are you sure the ribbon cable is properly aligned? The usually have a stripe indicating pin 1 this needs to attach to the pins closest to the sd card. – Steve Robillard Jun 8 '17 at 3:19
  • Thanks Steve, I added the photo:) Also, yes I checked polarity on the led, short end (negative) goes to ground, also have the ribbon correct, red stripe at the edge of the board. – Daver80 Jun 8 '17 at 4:09
  • Any chance of getting a better pic it is hard to tell what pins you used on the breakout board (even when I zoom in). Also, have you tried connecting the white wire to 3.3 Volts to rule out a bad LED? – Steve Robillard Jun 8 '17 at 4:49
  • The led is fine, it lights up when the pi is powered on. I have connected the white wire to several different pins including the 3.3 and they all light up the led. The problem is not getting the led to turn on, but to make it flash (blink) when I run the python code in a terminal. When I run the code the led is supposed to flash on and off and instead it just stays on solid like it always is while the pi is turned on. – Daver80 Jun 8 '17 at 4:55
  • If I have time tomorrow I will set everything up with the led lit and try to get a clearer image but the white wire went to bcm pin 8. Here is the example I'm working from: Example: Just gotta make that little bastard flash on and off! – Daver80 Jun 8 '17 at 5:09
3

I just tried your circuit and code, and it should work, However, looking closely at the photo and counting holes in the breadbaord I think the problem is that the white wire is actually connected to the last pin of the breakout board. That would be pin 7 not pin 8.

You can fix the problem in hardware or in software:

To correct the problem from hardware simply move the white wire one pin to the right (the next to last pin on the breakout board).

To fix it via software change this line:

pinNum = 8

to this:

pinNum = 7

Note: you only need to make one of the above changes not both.

1
  • Aha, good catch Steve, I noticed that myself after taking the pic. As I mentioned I had tried moving that wire to some other pins to see if they worked also (many did) and must have forgotten to move the wire back to bcm 8 before taking the pic. My bad, that muddied the waters considerably. To be clear it was in 8 for my first test and I have re-tried everything just now to confirm. Still solid on all the time:( I even tried using every pin 1 - 26... getting desperate... Thanks very much for the replies though! – Daver80 Jun 8 '17 at 21:55
0

Steve showed me how to save my code to a python script and the led blinks now. Not sure why this doesn't work from running the code in the terminal, but I suppose this is officially solved. Just so strange why the tutorial would say to run the code right inside the terminal if it can't work... weird. Anyways THANK YOU STEVE!

4
  • It can work from the shell. I have a copy working, but like I said when you have a multiline script it is easier to edit and run it if you don't need to reenter it. – Steve Robillard Jun 8 '17 at 23:05
  • I may have found the problem you had in the shell. Assuming the screenshot above is what you did you needed to hit return a couple of times after the last sleep call. – Steve Robillard Jun 8 '17 at 23:17
  • Wow, that was the deal, I had to hit return one extra time. Whoops! Thanks again bud. Also, any idea why the led keeps flashing after I close the terminal? Is there a way to end the program or do I just have to reboot the pi? – Daver80 Jun 9 '17 at 0:27
  • ctrl-c will usually end it – Steve Robillard Jun 9 '17 at 0:28
0

I recommend using the gpiozero library. It is designed to make physical computing much easier. The documentation is great and has lots of good beginner examples.

Using gpiozero, your example becomes:

from gpiozero import LED
from signal import pause

led = LED(8)
led.blink(0.5, 0.5)

pause()   # keeps the program running so the led will blink use ctrl-c to end
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.