1

On my Raspberry Pi B+ I have a standard program to blink a LED. I run it using the terminal command python blinky.py, and when I do, the LED lights up, but does not blink, as it should. I then cancel the running command using ctrl+c, but the LED remains on.

Thoughts? Python code is below.

import RPi.GPIO as GPIO
import time
# blinking function
def blink(pin):
        GPIO.output(pin,GPIO.HIGH)
        time.sleep(100)
        GPIO.output(pin,GPIO.LOW)
        time.sleep(100)
        return
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
# set up GPIO output channel
GPIO.setup(11, GPIO.OUT)
# blink GPIO17 50 times
for i in range(0,550):
        blink(11)
GPIO.cleanup()

Edit: The circuit I used connected pin 9 as ground to a resistor to reduce the voltage to a palatable amount to the LED, then through the LED to GPIO pin 11.

1
  • Could you please edit your question to include the circuit that you're trying to use?
    – NomadMaker
    Commented Aug 13, 2018 at 23:17

1 Answer 1

2

There are a couple of things wrong with this program. I suggest that you work with basic text i/o (print statements) rather than pins until you're comfortable with python. Working with pins is a fairly advanced thing and complicates understanding about how the program works.

One thing that is wrong is that time.sleep() uses a parameter in seconds. So you told the program to sleep for over 3 minutes for the full LED blink.

The reason the Pi stays lit is that you told the pin to turn on, and then when you interrupt the program, it doesn't clean itself up by turning that pin to false (or low) again.

Ideally you should catch the signal and do the cleanup manually at that point.

When I wrote my initial, naive, text version of this by copying your code and replacing all pin-based things with printing either "On" or "Off", the program had no output. I don't know enough python to understand what I did incorrectly.

The program that I did get working was enough to demonstrate the time.sleep() problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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