emeyeraway
Posts: 160
Joined: Tue Mar 24, 2015 7:11 am

libcamera and motion detectors

Sat Feb 04, 2023 10:10 pm

I am using motion detectors HC-SR312 AM312 and HC-SR501 with camera V2 on either Raspi 3B+ or Raspi 4. The sensors do detect motion, but when there is no motion, both sensors take a photo every 60 seconds, exactly. What could cause pin 17 to go high every 60 seconds? Here is my code:

Code: Select all

import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN,GPIO.PUD_DOWN)

import datetime
import time

time.sleep(2)
ctr=1
print("Waiting for motion detection...")

while True:
    
    if GPIO.input(17):
        timestamp = str((datetime.datetime.now()))
        timestamp = timestamp[5:19]
        print("Pin 17 is high")
        os.system("libcamera-jpeg -o "+str(ctr)+".jpg --vflip --hflip")
        print("Image captured at ",timestamp)
        print("Ready")
        ctr+=1
        time.sleep(10)
    else:
        #print("Pin 17 is low")
        time.sleep(1)

    
#time.sleep(5) 
Ed Meyer

memjr
Posts: 2769
Joined: Fri Aug 21, 2020 5:59 pm

Re: libcamera and motion detectors

Sat Feb 04, 2023 10:27 pm

Edit your post and put CODE tags around your code. We can't telll if your script has bad indenting or not the way it is now.
2023-02-04_17-27.png
2023-02-04_17-27.png (24.35 KiB) Viewed 421 times

emeyeraway
Posts: 160
Joined: Tue Mar 24, 2015 7:11 am

Re: libcamera and motion detectors

Sun Feb 05, 2023 3:43 pm

Code: Select all

import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN,GPIO.PUD_DOWN)

import datetime
import time

time.sleep(2)
ctr=1
print("Waiting for motion detection...")

while True:

if GPIO.input(17):
timestamp = str((datetime.datetime.now()))
timestamp = timestamp[5:19]
print("Pin 17 is high")
os.system("libcamera-jpeg -o "+str(ctr)+".jpg --vflip --hflip")
print("Image captured at ",timestamp)
print("Ready")
ctr+=1
time.sleep(10)
else:
#print("Pin 17 is low")
time.sleep(1)


#time.sleep(5)
I did what you suggested, but it looks like the indentations in the original code aren't there. I've never done this before; please tell me if I'm doing something wrong.

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

Re: libcamera and motion detectors

Sun Feb 05, 2023 3:50 pm

Somehow you removed all indentation of the code you posted.
If I quote your top post I can actually see some indentation.

The normal way is to copy the code from your editor and paste it inside a code block when you post it on this forum.

Alternative do as I did and quote your original post:

Code: Select all

import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN,GPIO.PUD_DOWN)

import datetime
import time

time. Sleep(2)
ctr=1
print("Waiting for motion detection...")

while True:
    
    if GPIO.input(17):
        timestamp = str((datetime.datetime.now()))
        timestamp = timestamp[5:19]
        print("Pin 17 is high")
        os.system("libcamera-jpeg -o "+str(ctr)+".jpg --vflip --hflip")
        print("Image captured at ",timestamp)
        print("Ready")
        ctr+=1
        time. Sleep(10)
    else:
        #print("Pin 17 is low")
        time. Sleep(1)

    
#time.sleep(5) 

emeyeraway
Posts: 160
Joined: Tue Mar 24, 2015 7:11 am

Re: libcamera and motion detectors

Sun Feb 05, 2023 4:28 pm

Your copy of my code looks fine. It runs as I would expect it to, but the real question here is: why does pin 17 go high every 60 seconds exactly? I should add that the same thing happens when I use my Raspi 2B+ and the legacy camera software; i.e., when there is no motion near the sensor, a photo is taken exactly every 60 seconds.

memjr
Posts: 2769
Joined: Fri Aug 21, 2020 5:59 pm

Re: libcamera and motion detectors

Mon Feb 06, 2023 5:50 am

By any chance, did you have any other programs running in your pi previously that might still be running for some reason? Your code should not be doing anything every 60 seconds. Maybe you were previously playing with another program that did something every 60 seconds or so?

Unless the sensor is getting triggered every 60 seconds or something external to the *this* program is causing that pin to change every 60 seconds,

How is you sensor wired to the pi? Long wires? Thin/thick wires? Please describe your setup, what sensor? How is it connected? Diagram?

Exactly every 60 seconds is too exact to be a bug all by itself. Why not every 47 seconds? It feels like there is something else going on here.

emeyeraway
Posts: 160
Joined: Tue Mar 24, 2015 7:11 am

Re: libcamera and motion detectors

Mon Feb 06, 2023 11:01 pm

I'm using a HC-SR312 AM312 sensor. I switched to a new one to see if the sensor was the problem, but it behaved the same way. I'm using a Raspi 4. I started over with a clean SD card, and have only two other Python programs on it, neither of which involves 60 seconds in any way that I can see.

I took the PI in question away, and used a different PI to provide power and ground to the sensor and connected its output lead to a voltmeter. The sensor behaved properly: it detected motion and produced no highs in the absence of motion.

I then went back to the original PI and ran the code with pin 17 (the one I had the sensor output connected to) connected to the voltmeter. When I ran the code there were no highs to the voltmeter.

So it appears that neither the sensor nor the code is producing a high at pin 17. Yet when I assemble the original PI and sensor together and run the code, it takes a photo every 60 seconds!

The sensor is connected to the PI with three 12" hook-up wires. The case for the PI has heat sinks and a fan in it. The only pins in use are power and ground for the fan and for the sensor, and pin 17 for sensor output; a total of 5. I tried using a different pin for the sensor output, but that didn't change anything. I tried using 5v and 3.3v to power the sensor; no difference in behavior.

I then connected the original sensor to the original PI 4, but connected the sensor output pin to a voltmeter instead of GPIO 17. When I ran the code and watched the voltmeter, the sensor behaved as expected; i.e., the only time the voltmeter showed 3.3v was when there was motion at the sensor. Encouraged, I turned everything off, reconnected the sensor output pin to GPIO17 and ran the code. No more 60-second highs! Photos were taken only when there was motion at the sensor.

However, after taking the setup outside (temp ~35F) where I plan to use it, it started taking photos almost continuously. I brought it back in, set it up and it started taking photos exactly every 60 seconds again. I removed the output pin from pin 17 and connected it to a voltmeter as in the previous paragraph, but this time the voltmeter went high every 60 seconds. Finally I put the sensor output back on pin 17 and to my great surprise it started working properly.

It is very frustrating dealing with this intermittent, unexplainable situation. Anyone reading through all this must be convinced that I'm nuts, and I'm beginning to think that could explain everything...

memjr
Posts: 2769
Joined: Fri Aug 21, 2020 5:59 pm

Re: libcamera and motion detectors

Tue Feb 07, 2023 5:23 am

Do you happen to have your cell phone near you? Cell phones can interfere with these and I would not be surprised if pings or reply to pings ever 60 seconds. Easy enough to test. Put all cell phones around your working spot in airplane mode and test again. Turn them back on and testmagain.

emeyeraway
Posts: 160
Joined: Tue Mar 24, 2015 7:11 am

Re: libcamera and motion detectors

Thu Feb 09, 2023 8:09 am

I tried turning off my iPhone. I also tried turning off my internet and using my laptop as a hotspot. Neither helped. I did discover that disconnecting and reconnecting to pin 17 led to proper behavior for around 10 minutes, but things always degenerated back to taking photos every 60 seconds, exactly.
Finally I tried running the code from the terminal rather than GUI and that solved the problem. I realize now that I should have done that in the first place. I had always run Python code from GUI. That's why I'm still a beginner at age 85.
But I enjoyed the process of trying to figure out what was going on. What caused the "exactly 60 seconds" phenomenon remains a mystery.

emeyeraway
Posts: 160
Joined: Tue Mar 24, 2015 7:11 am

Re: libcamera and motion detectors

Wed Feb 15, 2023 8:45 pm

I spoke too soon. It turns out that even running from the terminal leads to the "60-second" phenomenon. As of this writing I have no clue what causes the sensor to send a high to pin 17 exactly every 60 seconds.

memjr
Posts: 2769
Joined: Fri Aug 21, 2020 5:59 pm

Re: libcamera and motion detectors

Wed Feb 15, 2023 10:47 pm

I'm out of ideas. Some of these have a repeater mode, but from what I understand it is not for taking pictures are certain intervals, but repeated dectection after it no longer things there's anything moving in front of it.

Try looking into that, but I do not believe this is your case.

Return to “Beginners”