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()
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()