members
Raspberry basics: Project 20a Raspberry PI 3 model B board, Raspberry PI camera module V2, PIR senso
of Acoptex.com in Raspberry Pi 3
Raspberry basics: Project 20a
Project name: Raspberry PI 3 model B board, Raspberry PI camera module V2, PIR sensor - Anti-theft system
Tags: Raspberry, Raspberry camera V2, Raspberry PI camera module, Raspberry PI 3, PIR sensor, burglar detector, picture capture, photo capture, take a picture, Anti-theft system, gpiozero library, picamera library, time library, signal library
Attachments: pirsensorcamera.py
In this project, you needed these parts (Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you):
1.Raspberry PI 3 model B 1 pc
2. Raspberry PI camera module V2 1 pc
3. Micro SD card with NOOBS and SD card adapter 1 pc
4. Micro USB power supply (2 A 5V or 5V 3A) 1 pc
5. TV or monitor 1 pc
6. HDMI cable 1 pc
7. USB keyboard 1 pc
8. USB mouse 1 pc
9. PIR sensor HC - SR 501 or HC - SR 505 or Mini PIR Motion Sensor Module SC0322 1 pc
10. Breadboard 1 pc
11. Jumper cables F-M, M-M
12. Momentary switch (push button) 1 pc
13. Resistor 1 pc (10 KOhm)
14. T-Cobbler Breakout and GPIO Cable 1 pc
General
We will learn how to use PIR sensor with the Raspberry Pi Camera Module V2. When the PIR sensor detects the motion it triggers an event that takes a picture. It will show you who was in front of the camera.
We’ll use the built-in picamera library, which makes it simple to control the camera.
Understanding the PIR sensor
You can read more about it here.
Understanding the Raspberry PI camera module V2
The Raspberry Pi Camera Module v2 replaced the original Camera Module in April 2016. The v2 Camera Module has a Sony IMX219 8-megapixel sensor (compared to the 5-megapixel OmniVision OV5647 sensor of the original camera).
The Camera Module can be used to take high-definition video, as well as stills photographs. It’s easy to use for beginners, but has plenty to offer advanced users if you’re looking to expand your knowledge. There are lots of examples online of people using it for time-lapse, slow-motion, and other video cleverness. You can also use the libraries we bundle with the camera to create effects.
You can read all the gory details about IMX219 and the Exmor R back-illuminated sensor architecture on Sony’s website, but suffice to say this is more than just a resolution upgrade: it’s a leap forward in image quality, colour fidelity, and low-light performance. It supports 1080p30, 720p60 and VGA90 video modes, as well as still capture. It attaches via a 15cm ribbon cable to the CSI port on the Raspberry Pi.
The camera works with all models of Raspberry Pi 1, 2, and 3. It can be accessed through the MMAL and V4L APIs, and there are numerous third-party libraries built for it, including the Picamera Python library. See the Getting Started with Picamera resource to learn how to use it.
The camera module is very popular in home security applications, and in wildlife camera traps.
There’s also an infrared version of the camera (called Pi NoIR) which gives you everything the regular Camera Module offers, with one difference: it doesn’t use an infrared filter. This gives you the ability to see in the dark with infrared lighting.
You can read more about camera module here.
Understanding the Raspberry PI 3 model B
The Raspberry Pi 3 is the third-generation Raspberry Pi. It replaced the Raspberry Pi 2 Model B in February 2016.
Specification:
- Quad Core 1.2GHz Broadcom BCM2837 64bit CPU
- 1GB RAM
- BCM43438 wireless LAN and Bluetooth Low Energy (BLE) on board
- 40-pin extended GPIO
- 4 USB 2 ports
- 4 Pole stereo output and composite video port
- Full size HDMI
- CSI camera port for connecting a Raspberry Pi camera
- DSI display port for connecting a Raspberry Pi touchscreen display
- Micro SD port for loading your operating system and storing data
- Upgraded switched Micro USB power source up to 2.5A
Signals and connections of the PIR sensor
We are going to use Mini PIR Motion Sensor Module SC0322 in this project.
Signals and connections of the Raspberry PI 3 model B
Signals and connections of the Raspberry PI camera module V2
You can see on the back of ribbon cable one side has a piece of blue plastic and other has some traces on it. Line up traces on the ribbon cable with traces on the Raspberry PI 3 camera port. There is the tab which you can lift up and pull it by its edges. By doing that it will open up so you can insert the ribbon cable end with traces into the camera port. When it's in press tab to fix the ribbon cable.
Wiring
Step by Step instruction
1. Preparations
- Do wiring.
- You need to have a Raspbian OS preinstalled in your Raspberry Pi 3.
- Connect the Raspberry PI camera module V2 to the Raspberry Pi 3 camera port (CSI camera port). Make sure that Raspberry PI 3 switched off.
- Connect Raspberry PI 3 board HDMI port and to your TV or Monitor HDMI port with HDMI cable.
- Make sure that your monitor or TV is turned on, and that you have selected the right input (e.g. HDMI 1, etc).
- Plug in your USB mouse and USB keyboard to Raspberry PI 3 USB ports.
- Connect Micro USB power supply to Raspberry PI 3 board micro USB input.
- The Raspberry PI desktop will start up.
- Go to Raspberry icon-> Preferences -> Raspberry PI configuration
- Select Interfaces and make sure that the camera is enabled. If it’s not enabled, enable it and reboot your Raspberry PI 3 board to begin. We suggest to enable all settings and you will not need to come back to these configuration tool again.
- Open Terminal window and type the command: sudo apt-get update
- Then type the command: sudo apt-get install
- Reboot your Raspberry Pi 3 to ensure that all changes take effect - use this command: sudo reboot
2. Making the program
- The Raspberry PI desktop will start up after reboot.
- Type this command in the Terminal: sudo nano pirsensorcamera.py
- Copy and paste the code from pirsensorcamera.py to opened window. Since it is a Python code, you need to be careful with the Tab characters as it is important to group the instruction as blocks in Python.
- Press Ctrl+X, Y, Enter buttons to save the file.
- Type the command in the Terminal: sudo python pirsensorcamera.py
- The camera will initialise and take a picture when the PIR motion sensor detects movement. Pictures saved on your Raspberry Pi Desktop and named incrementally (image_1.jpg, image_2.jpg and so on).
- The camera stops when the momentary switch pressed.
- Congratulations! You did it. Your anti-theft system is ready to catch some thefts. Place it in a strategic place and come back later to check any saved pictures.
Code
First you import the libraries you need. Then you create objects to refer to the momentary switch, the PIR motion sensor and the camera, and initialize the camera with camera.start_preview().
Depending on how your camera is oriented, you might also need to rotate it 180 degrees with camera.rotation =180 so that it doesn’t take the photos upside down. If your image is upside down when you test this code, go back and set the rotation to 0 or comment out this line.
Next, you initialize an i variable that starts at 0. The take_picture() function will use this variable to count and number the images, incrementing the number in the filename by one with each picture taken.
You then define the stop_camera() function that stops the camera with the camera.stop_preview() method. take_picture() function takes a photo. For this, you use the camera.capture() method, specifying the directory you want to save the image to inside the parentheses. In this case, we’re saving the images on the Raspberry Pi Desktop and naming the images image_%s.jpg, where %s is replaced with the number we incremented earlier in i. If you want to save your files to a different folder, replace this directory with the path to your chosen folder.
You then impose a 10-second delay, meaning the camera takes photos at 10-second intervals for as long as the PIR sensor detects movement. You can increase or decrease the delay time but do not make the delay time too small.
Then we define the behavior to trigger the stop_camera() function when you press the momentary switch. This function stops the camera preview and exits the program.
Finally, we tell the camera to take a picture by triggering the take_picture() function when motion detected.
Summary
We have learnt how to use PIR sensor with the Raspberry Pi Camera Module V2. You can try now to redesign your project so that, when the sensor detects motion, the Raspberry Pi takes a picture, sends you an email notification and sounds an alarm.
Libraries in use
- gpiozero
- picamera
- time
- signal
Script
- Attached on the begining of this project
Other projects of Acoptex.com










Viewed: 1296 times