drifterf
Posts: 70
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

turn on led for period of time with a button

Sat Dec 08, 2018 9:12 am

Hi, im working on a small project and have made progress but i need the following

i want to activate a led (at the moment, when its working i will be using the gpio for a relay module)

i want to press a physical button on the breadboard and have the led stay on for a period of time i can set and then auto turn off.

could anyone help with what im after. Many thanks

Andyroo
Posts: 2833
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: turn on led for period of time with a button

Sat Dec 08, 2018 6:46 pm

Very basic code to turn a LED on is:

Code: Select all

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)
This assumes the LED is connected to pin #18 - (6th one down outer row of pins) and a resistor connected to ground and the LED...

This will keep the LED on for 1 second - to increase the duration just change the time.sleep(1) to the number of seconds you want - e.g.

Code: Select all

time.sleep(10)
gives you 10 seconds of light.

To detect the button press have a read through of https://raspberrypihq.com/use-a-push-bu ... y-pi-gpio/

Let us know how you get on and if you get stuck merging the two codes together :)
Need Pi spray - these things are breeding in my house...

User avatar
bensimmo
Posts: 3953
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: turn on led for period of time with a button

Sat Dec 08, 2018 7:01 pm

It sounds like you are brand new to all of this.
Work you way through https://projects.raspberrypi.org/en/pro ... -computing an Pi document.
You'll see you answer and then you continue from there.

PhatFil
Posts: 1187
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: turn on led for period of time with a button

Sat Dec 08, 2018 7:41 pm

"Blink" is the "hello World" of development board programming ;) a quick search for "Python Pi Blink" should provide very fruitful.

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: turn on led for period of time with a button

Sun Dec 09, 2018 9:35 am

i would use GPIOZERO

Code: Select all

from gpiozero import LED, Button
from time import sleep
from signal import pause
led = LED(17) # Led pin
button = Button(2)  # button pin

button.wait_for_press()
led.on()
sleep(3) # wait for 3 sec
led.off()
pause()

drifterf
Posts: 70
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: turn on led for period of time with a button

Mon Dec 10, 2018 8:42 am

Hi, thank you all for the replys. i will have a read through of the links, much appreciated !

drifterf
Posts: 70
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: turn on led for period of time with a button

Mon Dec 10, 2018 9:59 am

Andyroo wrote:
Sat Dec 08, 2018 6:46 pm
Very basic code to turn a LED on is:

Code: Select all

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)
This assumes the LED is connected to pin #18 - (6th one down outer row of pins) and a resistor connected to ground and the LED...

This will keep the LED on for 1 second - to increase the duration just change the time.sleep(1) to the number of seconds you want - e.g.

Code: Select all

time.sleep(10)
gives you 10 seconds of light.

To detect the button press have a read through of https://raspberrypihq.com/use-a-push-bu ... y-pi-gpio/

Let us know how you get on and if you get stuck merging the two codes together :)

could you help me in embedding the 2 scripts together please

scotty101
Posts: 3564
Joined: Fri Jun 08, 2012 6:03 pm

Re: turn on led for period of time with a button

Mon Dec 10, 2018 12:31 pm

drifterf wrote:
Mon Dec 10, 2018 9:59 am
could you help me in embedding the 2 scripts together please
Have you run though the tutorial that someone shared above?
https://projects.raspberrypi.org/en/pro ... -computing

Once you've taken that tutorial and tried to complete your project yourself, come back if you are stuck.
Copying and pasting someone else's code from the internet (without understanding it) won't help you learn.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

drifterf
Posts: 70
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: turn on led for period of time with a button

Tue Dec 11, 2018 6:52 pm

Hi, i really appreciate the tutorial pages you have given me. Just to let you know i have got it sorted and have got what i watned to work to work :)

I do need the script to continue working when it has done it cycle. It seems to do the script once and then i have to reload to continue it, i need it to do it constantly.

DirkS
Posts: 9804
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: turn on led for period of time with a button

Tue Dec 11, 2018 7:28 pm

The best solution depends on your script and the module(s) you use.
If you use gpiozero you would put a pause() at the end (needs an extra module) while for other modules you would probably use a loop of some sort.

Can you post your code?

drifterf
Posts: 70
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: turn on led for period of time with a button

Tue Dec 11, 2018 8:11 pm

from gpiozero import LED, Button, Buzzer
from time import sleep

led = LED(17)
button = Button(2)
buzzer = Buzzer(18)

button.wait_for_press()
led.on()
buzzer.on()
sleep(3)
led.off()
buzzer.off()



i just want to loop this script please.

also is there a reason why when i launch the code in idle it take about 10 seconds after showing me "RESTART: / etc" before it actually starts to do anything in my script. its as if there is a delay lol.

DirkS
Posts: 9804
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: turn on led for period of time with a button

Tue Dec 11, 2018 8:35 pm

I think this should do it

Code: Select all

from gpiozero import LED, Button, Buzzer
from time import sleep

led = LED(17)
button = Button(2)
buzzer = Buzzer(18)

while True:
    button.wait_for_press()
    led.on()
    buzzer.on()
    sleep(3)
    led.off()
    buzzer.off()

drifterf
Posts: 70
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: turn on led for period of time with a button

Wed Dec 12, 2018 6:23 am

thank you this worked, i did try the "while true" but couldnt get it to work, must of typed something wrong, many thanks !!
DirkS wrote:
Tue Dec 11, 2018 8:35 pm
I think this should do it

Code: Select all

from gpiozero import LED, Button, Buzzer
from time import sleep

led = LED(17)
button = Button(2)
buzzer = Buzzer(18)

while True:
    button.wait_for_press()
    led.on()
    buzzer.on()
    sleep(3)
    led.off()
    buzzer.off()

Return to “Python”