Python Forum
GPIO output timing help needed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GPIO output timing help needed
#1
Greetings to the Group

I am a beginner to python so be nice to me.
I am trying to control a GPIO output from a HIGH input and have the output stay HIGH for a specific time. I have written the script and it works sort of.
The problem I'm seeing is, if the input goes LOW and then back to HIGH and back to LOW before the "sleep time" had finished, it does not reset the Output time.
I am using the "sleep time" but I don't think this is the correct way to control the Output timing. As I said I'm very new to programming and if anyone can help me here it would be greatly appreciated. I know the answer is simple but I'm not seeing it.
I have attached the code I have written, below.
import RPi.GPIO as GPIO
from time import sleep    #import time library

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)     # set i/p sig to GPIO pin 4
GPIO.setwarnings(False)
GPIO.setup(26,GPIO.OUT,initial=GPIO.LOW)  #set o/p sig to GPIO pin26, initiallize in low state
GPIO.setup(4,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)  #set internal pulldown resistor
try:
    while True:
        if  (GPIO.input(4)==1):
                GPIO.output(26,1)
                sleep(10)
        else:
            GPIO.output(26,0)
            sleep(.1)
            
finally:
    GPIO.output(26,False)
    GPIO.cleanup
Reply
#2
Greetings to the Group

I am a beginner to python so be nice to me.
I am trying to control a GPIO output from a HIGH input and have the output stay HIGH for a specific time. I have written the script and it works sort of.
The problem I'm seeing is, if the input goes LOW and then back to HIGH and back to LOW before the "sleep time" had finished, it does not reset the Output time.
I am using the "sleep time" but I don't think this is the correct way to control the Output timing. As I said I'm very new to programming and if anyone can help me here it would be greatly appreciated. I know the answer is simple but I'm not seeing it.
I have attached the code I have written, below.
import RPi.GPIO as GPIO
from time import sleep    #import time library

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)     # set i/p sig to GPIO pin 4
GPIO.setwarnings(False)
GPIO.setup(26,GPIO.OUT,initial=GPIO.LOW)  #set o/p sig to GPIO pin26, initiallize in low state
GPIO.setup(4,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)  #set internal pulldown resistor
try:
    while True:
        if  (GPIO.input(4)==1):
                GPIO.output(26,1)
                sleep(10)
        else:
            GPIO.output(26,0)
            sleep(.1)
            
finally:
    GPIO.output(26,False)
    GPIO.cleanup
        
Reply
#3
Threads merged, don't start another thread with same question.

Can you tell more exactly what you mean by this:
Quote:it does not reset the Output time

When GPIO.output(26,1) executes, output will be HIGH for next 10 seconds no matter what happens to input, because program will be in "sleep loop." Or is that not what happens?
Reply
#4
You set a pulldown:
GPIO.setup(4,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
but never reset it.
After the pin is set high, do you need to pull it low again?
if not, will the pin remain high?
Reply
#5
I think you want to use Interrupts. I'll update my post later with an very small example.

The task is, when you see the rising edge of input, the output should be for 10 seconds High?

import RPi.GPIO as GPIO
from time import sleep
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)
GPIO.setwarnings(False)
GPIO.setup(26, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def event(channel):
    GPIO.output(26, 1)
    sleep(10)
    GPIO.output(26, 0)

# now register the event detection for rising edge of pin number 4
# the function event is called, if the event happens
# to prevent bounces from the switch, you can use a software debounce
# bouncetime 200 means after first change of input, the changes for the next 200ms
# are ignored. Normally you should avoid bouncing with a capacitor 
GPIO.add_event_detect(4, GPIO.RISING, callback=event, bouncetime=200)
# https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/


# I think the event detection runs in a daemon Thread.
# The Python Interpreter ends, when no non-deamon Thread is left.
# Without the while True loop, the program should just exit directly after executing it 
# Just try it out without the loop

try:
    while True:
        sleep(5)
except KeyboardInterrupt:
    GPIO.cleanup()
Code is not tested.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
Hi folks thanks for the replys
What I am trying to do is use a high input (3.3v) to control a fan to cool a radio heat sink. I want the fan to run for about 2 min. after the input goes low.
But with the sleep time I have in the code right now, if the input goes high during this sleep time period I want it, basically to reset the time period the output is high. I hope I explained that properly.
I realize I am probably not using the time function properly but I could not find any examples on the web.
I have seen a reply here that suggest using interrupts, I have been exploring that too.
I know I am making mistakes here but if nothing else I am learning, all be it, slowly.
I hope you can steer me in the right direction, and again thanks for you help so far.

Hi again Guys
I should have mentioned that I set the internal pull down resistor so the Input(4) would not be floating. Was this wrong?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function return boolean based on GPIO pin reading caslor 2 1,130 Feb-04-2023, 12:30 PM
Last Post: caslor
  class Update input (Gpio pin raspberry pi) caslor 2 744 Jan-30-2023, 08:05 PM
Last Post: caslor
  Webhook, post_data, GPIO partial changes DigitalID 2 953 Nov-10-2022, 09:50 PM
Last Post: deanhystad
Question Timing actions with Python dangermaus33 0 980 Apr-19-2022, 10:08 PM
Last Post: dangermaus33
  Inconsistent counting / timing with threading rantwhy 1 1,719 Nov-24-2021, 04:04 AM
Last Post: deanhystad
  Synchronization/Timing Problem quest 5 2,927 Mar-31-2021, 10:26 PM
Last Post: quest
  Timing of a while loop stylingpat 4 6,666 Mar-31-2021, 10:48 AM
Last Post: stylingpat
  Seemingly unstable GPIO output while executing from RetroPie LouF 6 3,856 Feb-19-2021, 06:29 AM
Last Post: LouF
  Picture changing triggered by GPIO q_nerk 2 2,518 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  GPIO high if network IP has good ping duckredbeard 3 2,280 Oct-12-2020, 10:41 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020