shortcircuited
Posts: 5
Joined: Sat Apr 17, 2021 9:06 pm

PIR sensor and Pi Camera

Sat Apr 17, 2021 9:20 pm

Hello Everyone,

I am new to the forum and excited to be here.

I am using a PIR motion sensor and a Pi Camera with my Raspberry Pi 3 B+. The circuit works but not exactly as I would like and wonder if the issue is with the PIR sensor. The objective is that every time the PIR sensor senses motion, the Pi camera should immediately snap a picture and save it in a folder. It works but if I swing my hand in front of the PIR, say, once every seconds, the sensor+Pi camera don't respond as quickly. There is a strange lag. This is the code from a book I have:

Code: Select all

from gpiozero import Button, MotionSensor
from picamera import PiCamera
from time import sleep
from signal import pause

#create objects that refer to a button,
#a motion, sensor, and the PiCamera
button = Button(2)
pir = MotionSensor(4)
camera = PiCamera()

#start the camera
camera.rotation = 180
camera.start_preview()
#create image names]
 i = 0

#stop the camera when the pushbutton is pressed
def stop_camera():
	camera.stop_preview()
 
 #exit the program
exit()

#take a photo when motion is detected
def take_photo():
 	global i
 	i = i + 1
 	camera.capture('/home/pi/Desktop/image_%s.jpg' % i)
 	print('A photo has been taken')
	sleep(10)
#assign a function that runs when the button is pressed
button.when_pressed = stop_camera
#assign a function that runs when motion is detected
 pir.when_motion = take_photo
pause()
I have tweaked the sensitivity and time of the PIR sensor but the system does not respond as intended (i.e. it does not take a picture every time I wave my hand in front of the PIR). I read that the signal on the PIR sensor stays high for a minimum of 5 seconds....that would explain things.

However, when I try the code below (which does not involved a Pi camera taking a pic and storing into a folder), the PIR response is much quicker:

Code: Select all

from gpiozero import MotionSensor

pir = MotionSensor(4)

while True:
	pir.wait_for_motion()
	print("You moved")
	pir.wait_for_no_motion()
Does anyone know what is going on?

Thank you,
Shortcircuited

User avatar
neilgl
Posts: 3050
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: PIR sensor and Pi Camera

Sun Apr 18, 2021 4:43 pm

You have a sleep(10) after the photo is taken?

shortcircuited
Posts: 5
Joined: Sat Apr 17, 2021 9:06 pm

Re: PIR sensor and Pi Camera

Mon Apr 19, 2021 5:28 pm

Hi,

I changed that to 1 or even 0.5 but still experience the lag unfortunately...

User avatar
neilgl
Posts: 3050
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: PIR sensor and Pi Camera

Mon Apr 19, 2021 6:10 pm

Something odd there as using your code on my pi3 (with sleep() commented out), it works very quickly, taking an image instantly when GPIO4 goes high.

shortcircuited
Posts: 5
Joined: Sat Apr 17, 2021 9:06 pm

Re: PIR sensor and Pi Camera

Mon Apr 19, 2021 11:39 pm

Thank you. That is encouraging. I will comment sleep out too and see what happens. Will let your know.

I think I read that the PIR sensor stay HIGH for a few seconds (maybe a minimum of 5s) and I thought that could be the issue: it needs to go back to low before going back to high and tell the Pi camera to snap a picture. I was placing different objects in front of the PIR sensor very quickly, like every second, and would like the system to take picture of the various objects...

gordon77
Posts: 5594
Joined: Sun Aug 05, 2012 3:12 pm

Re: PIR sensor and Pi Camera

Tue Apr 20, 2021 5:11 am

I expect the pir has adjustments on it. One for sensitivity and one for time. Have you tried adjusting them?

https://thepihut.com/products/pir-motio ... 6fEALw_wcB

shortcircuited
Posts: 5
Joined: Sat Apr 17, 2021 9:06 pm

Re: PIR sensor and Pi Camera

Tue Apr 20, 2021 6:59 pm

Thanks gordon77. I did turn one of the screws to see if it would make a difference. I will try again.

Thank you!

gordon77
Posts: 5594
Joined: Sun Aug 05, 2012 3:12 pm

Re: PIR sensor and Pi Camera

Tue Apr 20, 2021 7:45 pm

They can be awkward to get as required, I find it best to start adjustments with the Sensitivity fully Clockwise and Delay fully anti-clockwise.

shortcircuited
Posts: 5
Joined: Sat Apr 17, 2021 9:06 pm

Re: PIR sensor and Pi Camera

Wed Apr 21, 2021 1:29 am

Hi Gordon77,

A factor that may play a role but I have omitted is the following actions incode happening within the same .py file:

1) The PIR sensor detects motion
2) The Pi Camera takes a picture when motion is detected. The picture gets stored in a folder
3) Some machine learning code (model) for image classification grabs the picture and classifies it (take 30seconds).

Maybe that entire process interferes with the speed at which the PIR sensor reacts? I will first try PIR sensor+Pi camera alone without the ML code...

thanks!

User avatar
neilgl
Posts: 3050
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: PIR sensor and Pi Camera

Wed Apr 21, 2021 9:03 am

To test it without the pir connected, install raspi-gpio if you don’t already have it using

Code: Select all

sudo apt install -y raspi-gpio 
Run your script, then in a separate terminal set e.g. GPIO4 low/high like this:

Code: Select all

raspi-gpio set 4 op
raspi-gpio set 4 dl
raspi-gpio set 4 dh
raspi-gpio set 4 dl
Your script will then take a photo etc.
That eliminates any time on / duration issues from the PIR

Return to “Beginners”