User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Motion sensor to call script for continuous lighting

Mon May 18, 2020 2:15 am

Hey All.
I'm using a motion sensor to turn on an LED. My hurdle right now is to have the led stay on for an interval, if more movement is detected I want to reset a timer to zero. I almost have this figured out by using 2 scripts, the first one detects motion and calls the second which turns the LED high for an interval.

What happens now is the sensor detects movement, triggers the led script which executes to completion and the led goes dim, even though I'm constantly moving, a second or so passes and the LED lights up again and the process repeats.

How can I write this so when movement is detected the interval is reset and the LED stays HIGH without interruption?

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import time

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_PIR = 18

print "PIR Module Test (CTRL-C to exit)"

GPIO.setup(GPIO_PIR,GPIO.IN)      # Echo

Current_State  = 0
Previous_State = 0
try:

  print "Waiting for PIR to settle ..."

  # Loop until PIR output is 0
  while GPIO.input(GPIO_PIR)==1:
    Current_State  = 0

  print "  Ready"

  # Loop until users quits with CTRL-C
  while True :

    # Read PIR state
    Current_State = GPIO.input(GPIO_PIR)

    if Current_State==1 and Previous_State==0:
      # PIR is triggered
      print "  Motion detected!"
      execfile('led_on.py')
     # Record previous state
      Previous_State=1
    elif Current_State==0 and Previous_State==1:
      # PIR has returned to ready state
      print "  Ready"
      Previous_State=0

    # Wait for 10 milliseconds
    time.sleep(0.01)

except KeyboardInterrupt:
  print "  Quit"
  # Reset GPIO settings
  GPIO.cleanup()
Here is the LED code

Code: Select all

import RPi.GPIO as GPIO
import time

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_LED = 17
GPIO.setup(GPIO_LED, GPIO.OUT) 
GPIO.setwarnings(False)

GPIO.output(GPIO_LED, True) 
time.sleep(5)
GPIO.output(GPIO_LED, False) 


pcmanbob
Posts: 9522
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Motion sensor to call script for continuous lighting

Mon May 18, 2020 8:47 am

Simple answer YES.

What you need to do is start a timer and then while the timer is running you keep checking for the PIR input to go high again , then if it does you reset the timer, that way while there is movement your timer will be constantly reset.

example code based on your code.

Code: Select all

#!/usr/bin/python3
import RPi.GPIO as GPIO
import time

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_PIR = 18
GPIO_LED = 17
print "PIR Module Test (CTRL-C to exit)"

GPIO.setup(GPIO_PIR,GPIO.IN)      # Echo
GPIO.setup(GPIO_LED, GPIO.OUT) 

delay = 60

try:

    print ("Waiting for PIR to settle ...")

    # Loop until PIR output is 0
    while GPIO.input(GPIO_PIR)==1:
        time.sleep(10)


    print ("Ready")

    # Loop until users quits with CTRL-C
    while True :

    # Read PIR state
    while GPIO.input(GPIO_PIR) == 0:
        time.sleep(0.1)
        
    start = time.time()
    # PIR is triggered
    print ("Motion detected!")
    GPIO.output(GPIO_LED, True) 
    
    # start timer  with reset if pir retriggered    
    end = start + delay
    now = time.time()
    while end > now:  
        if GPIO.input(GPIO_PIR) == 1:
            start = time.time()
            end = start + delay
        now = time.time()
        time.sleep(0.5)
            
    print ("Time up")
    GPIO.output(GPIO_LED, False) 
    
    
    
except KeyboardInterrupt:
  print ("  Quit")
  # Reset GPIO settings
  GPIO.cleanup()
untested code , adjusted to run on python3
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: Motion sensor to call script for continuous lighting

Mon May 18, 2020 9:30 pm

Ah yes, this is similar to how it's done in Arduino, C+; my native language so to speak.

So I spent a couple of hours playing with the indenting on your suggested code and just couldn't get it to work. I suspect your suggested code will work if I can get the indents right.

I looked at some tutorial but I'm just not getting it. What indentation will make this work?

pcmanbob
Posts: 9522
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Motion sensor to call script for continuous lighting

Tue May 19, 2020 9:17 am

Adjusted indentation and changed time.sleep to catch triggers better.

tested and working

Code: Select all

#!/usr/bin/python3
import RPi.GPIO as GPIO
import time

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_PIR = 18
GPIO_LED = 17
print ("PIR Module Test (CTRL-C to exit)")

GPIO.setup(GPIO_PIR,GPIO.IN)      # Echo
GPIO.setup(GPIO_LED, GPIO.OUT) 

delay = 10

try:

    print ("Waiting for PIR to settle ...")

    # Loop until PIR output is 0
    while GPIO.input(GPIO_PIR)==1:
        time.sleep(10)


    print ("Ready")

    # Loop until users quits with CTRL-C
    while True :

        # Read PIR state
        while GPIO.input(GPIO_PIR) == 0:
            time.sleep(0.1)
            
        start = time.time()
        # PIR is triggered
        print ("Motion detected!")
        GPIO.output(GPIO_LED, True) 
        
        # start timer  with reset if pir retriggered    
        end = start + delay
        now = time.time()
        while end > now:  
            if GPIO.input(GPIO_PIR) == 1:
                print ("Motion detected again!")
                start = time.time()
                end = start + delay
            now = time.time()
            time.sleep(0.1)
                
        print ("Time up")
        GPIO.output(GPIO_LED, False) 
    
    
    
except KeyboardInterrupt:
  print ("  Quit")
  # Reset GPIO settings
  GPIO.cleanup()
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: Motion sensor to call script for continuous lighting

Tue May 19, 2020 10:53 pm

Hmm. I'm still not quite getting it to run properly.

When I fire up the script the LED goes HIGH and stays HIGH. From the console I see the "Ready" prompt and when I move I get "Motion Detected" which quickly changes to "Motion detected again!" and the message scrolls forever and the script seems to lock up. I haven't seen the "Time Up" message.

Thanks for your help so far!

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: Motion sensor to call script for continuous lighting

Wed May 20, 2020 12:21 am

UPDATE
I just swapped out the PIR with a different one and it's working better. But still needs a bit more tweaking

The code runs but like the original the light goes LOW before it detects more movement and goes HIGH again.

pcmanbob
Posts: 9522
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Motion sensor to call script for continuous lighting

Wed May 20, 2020 7:24 am

OK so first I set the delay at only 10 seconds for testing , try increasing it to several minutes, ( 180 seconds).

Did you set the PIR up correctly ?

my code works correctly that I know because I tested it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”