Code Issues. Using GPIO's

8 posts
by Murphyboy » Fri May 19, 2017 4:25 pm

I am struggling creating the code to control some LED's.

What am I trying to do?

Well, I want to be able to control a single LED, Using three push buttons which controls the time period the LED remains illuminated for then switches off.

For example: one button does 10 seconds, the next does 20 second and the third does 30 seconds (For example)

I have managed to control the LED to flash at a certain time period and have managed to illuminate the LED using the push button, but I just cant piece these together. Or find the information I need.

After a few hours of trying I just don't quite know what I'm doing.

Would anyone be able to help me with this script or point me in the rough direction


Posts: 3
Joined: Fri May 19, 2017 4:01 pm
by joan » Fri May 19, 2017 6:04 pm
Please post your code.
User avatar
Posts: 12645
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by Murphyboy » Fri May 19, 2017 6:27 pm
import RPi.GPIO as GPIO
import time
GPIO.setup(24, GPIO.OUT)

While True:
if GPIO.input (24) :
GPIO.output (23, True)
GPIO.output (23, False)

time.sleep (0.1)

But I cant work out how to make it so the LED only stay on for a set time period rather than just being a light switch effect. If that makes sense?
Posts: 3
Joined: Fri May 19, 2017 4:01 pm
by B.Goode » Fri May 19, 2017 6:45 pm
The syntax (meaning) of a Python script depends on indentation.

To maintain indentation and thus the meaning of your script it needs to be posted using the mark-up provided by the grey [ Code ] button at the top of the message edit screen.

It already looks strange that you define 24 as an output but immediately read its value as an input.

You don't seem to make any attempt to use Sleep to introduce a 10 sec delay?
Posts: 3490
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK
by Murphyboy » Fri May 19, 2017 7:39 pm
Hmmm, I think I have got myself in a bit of a pickle.

Thank you for attempting to help, I will revaluate what I have done. As I seem to of mixed up a few numbers here. And ill repost in reply the correct format.

Very new to the software/programming world. Usually more hardware based but trying to broaden my horizons.


Posts: 3
Joined: Fri May 19, 2017 4:01 pm
by alphanumeric » Fri May 19, 2017 8:23 pm
Posts: 990
Joined: Tue Jan 19, 2016 2:17 pm
by pcmanbob » Fri May 19, 2017 8:37 pm

so here an example of how you could do it. assuming switches on gpio 24 and 26 and LED on gpio 23

Code: Select all
while true:

   if GPIO.input(24) == True:
      GPIO.output(23, True)
      GPIO.output(23, False)
   if GPIO.input(26) == True:
      GPIO.output(23, True)
      GPIO.output(23, False)

so the if statements look to see if button is pressed, if it is then it turns the LED on then you create your desired on period by adding a time.sleep before turning the LED off.
so you see by changing the sleep time between turning the LED on and off you control how long the LED is on for.

the last time.sleep(0.1) is just to slow your program a fraction because if no button is pressed the loop will execute very fast and uses a lot of cpu time which warms up your Pi.

hope this helps
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Posts: 954
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK
by alphanumeric » Fri May 19, 2017 8:49 pm
I use a GPIO pin to shut down my Pi when I press a button connected to it, this is my code.
Code: Select all
import RPi.GPIO as GPIO

GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_OFF) 

def switchpress(channel): 
    os.system("sudo shutdown now -P")

GPIO.add_event_detect(16, GPIO.FALLING, callback = switchpress, bouncetime = 2000)

I have my own pull up resister installed which is why I used GPIO.PUD_OFF. To use the internal pull up resister it is GPIO.PUD_UP
Posts: 990
Joined: Tue Jan 19, 2016 2:17 pm