jucasan
Posts: 6
Joined: Tue Jul 26, 2016 6:39 pm

simple LED on/off

Wed Apr 12, 2017 4:20 am

Hello forum,

Just trying to make my 1st project with the LED and I copy this code

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
print "LED on"
GPIO.output(18,GPIO.HIGH)
time.sleep(1)
print "LED off"
GPIO.output(18,GPIO.LOW)

and I though that increasing the time.sleep(1) would make the LED stay lid longer but what it does is to make it blink whatever value is in time.speed(1). If I want to keep the LED on for whatever amount of seconds, how can I change the code?
Thanks

User avatar
joan
Posts: 14959
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: simple LED on/off

Wed Apr 12, 2017 8:39 am

How is the LED wired?

PiGraham
Posts: 3971
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: simple LED on/off

Wed Apr 12, 2017 9:00 am

jucasan wrote:Hello forum,

Just trying to make my 1st project with the LED and I copy this code

Code: Select all

import RPi.GPIO as GPIO
import time
on_time = 1.0
off_time = 2.0
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
while(1):
    print "LED on"
    time.sleep(on_time)
    GPIO.output(18,GPIO.HIGH)
    print "LED off"
    time.sleep(off_time)
    GPIO.output(18,GPIO.LOW)
and I though that increasing the time.sleep(1) would make the LED stay lid longer but what it does is to make it blink whatever value is in time.speed(1). If I want to keep the LED on for whatever amount of seconds, how can I change the code?
Thanks
See code for control of on and off time.
Note that this assumes LED is gonnected from gpio via resistor to GND. If it is connected from gpio via resistor to 3.3v (LED the other way around) then HIGH will be off and LOW will be on.
I added a loop to make it easier to see what the LED is doing.

jucasan
Posts: 6
Joined: Tue Jul 26, 2016 6:39 pm

Re: simple LED on/off

Thu Apr 13, 2017 1:00 am

I don't really understand what is going on.

This is the example I'm doing

https://thepihut.com/blogs/raspberry-pi ... -gpio-pins

I tried pi graham code and the gpio doesn't stay high (just blinks)
There is another way to do this?

edit: just connected the + to 3.3v. Is that supposed to blink? I'm starting to think there is something wrong with my pi.

PiGraham
Posts: 3971
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: simple LED on/off

Thu Apr 13, 2017 8:02 am

jucasan wrote:I don't really understand what is going on.

This is the example I'm doing

https://thepihut.com/blogs/raspberry-pi ... -gpio-pins

I tried pi graham code and the gpio doesn't stay high (just blinks)
There is another way to do this?

edit: just connected the + to 3.3v. Is that supposed to blink? I'm starting to think there is something wrong with my pi.
Of course it blinks. What do you want it to do? Your own code was turning on to one second then off again. That's one blink. Change the sleep times for longer on or off times.
Remove the while loop if you just one the single pulse.
How is the LED wired? That matters.

Return to “Beginners”