Thing to buy
- 4GB Raspberry Pi 4
- Raspberry Pi Camera Modules V2-8 Megapixel
- Adafruit PIR motion Sensor
- 120pc 15CM dupont wire female to female breadboard jumper wires
- Raspberry Pi 4 Model 4 USB-c 5.1V plug
- HDMI to Micro HDMI
- 32GB micro SD
I went with a Raspberry pi 4 which is on the expensive end for Raspberry pi, you should be able to do this with any very of Raspberry pi.
Hardware set up and test code
Raspberry pi website has a project geared for kids that works very well on how to connect the motion Sensor and Camera and a simple code to run and test that everything is working here. I’m using this code as a starting point. Using the Raspberry pi parent detector as a starting point, i follow the steps and tested it out, and i got it to work!
Making a Class
The Raspberry pi project a great starting point, but it would be better if it was a class that we could build off later. So i’ve a super simple class that contain pretty much the same code as the project. A with a few minor change. The file from the original project would overwrite it self, so i’ve added time to the filename. Second to keep the camera ribbon straight my camera is 180 degree off. So i’ve rotated the camera 180 degrees. github repo with the code is also here. https://github.com/carchi8py/Raspberry-pi-detector
from gpiozero import MotionSensor
from picamera import PiCamera
from datetime import datetime
class Detector(object):
def __init__(self):
self.pir = MotionSensor(4, threshold=0.5)
self.camera = PiCamera()
def start(self):
while True:
# The camera always start off detecting motion, so wait until it see no motion
self.pir.wait_for_no_motion()
self.pir.wait_for_motion()
print("Motion detect!")
self.start_camera()
self.pir.wait_for_no_motion()
print("No Motion")
self.camera.stop_recording()
def start_camera(self):
datename = "{0:%Y}-{0:%m}-{0:%d}:{0:%H}:{0:%M}:{0:%S}".format(datetime.now())
filename = str(datename) + "video.h264"
self.camera.resolution = (1920, 1080)
self.camera.rotation = 180
self.camera.start_recording(filename)
def main():
obj = Detector()
obj.start()
if __name__ == "__main__":
main()
Next Steps
Will be connecting this to Amazon Rekognition