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.
My code examples are always for Python >=3.6.0
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
  Picture changing triggered by GPIO q_nerk 2 159 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  GPIO high if network IP has good ping duckredbeard 3 261 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  raspberry pi tank gpio help jatgm1 1 434 May-06-2020, 09:00 PM
Last Post: Larz60+
  Assigning Data from one column to another with different associated timing interval alexafshari 1 435 Apr-30-2020, 03:59 PM
Last Post: pyzyx3qwerty
  Where should I place GPIO.cleanup() shallanq 2 495 Apr-11-2020, 05:02 AM
Last Post: shallanq
  Orange PI Win steering LEDs connect to GPIO djmcg 0 377 Dec-27-2019, 08:26 AM
Last Post: djmcg
  GPIO time in HIGH LOW boris_za 1 522 Dec-07-2019, 01:48 PM
Last Post: Larz60+
  Frequency and timing of psycopg2 commits acecase 0 442 Nov-01-2019, 05:50 PM
Last Post: acecase
  Perpetual timing Mark17 3 799 Oct-24-2019, 03:46 PM
Last Post: Gribouillis
  Timing input Mark17 2 648 Oct-23-2019, 08:25 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

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