Hardware Setup
This project was originally developed on the Raspberry Pi Zero W but has since moved to the Raspberry Pi Zero 2 W for better performance. The original Pi Zero W is no longer officially supported.
Prerequisites
- Raspberry Pi Zero 2 W (or Raspberry Pi Zero 2 WH)
- Headers for the Raspberry Pi Zero 2 W if you did not choose a Raspberry Pi Zero 2 WH (if you do not feel comfortable soldering the headers you can use solderless headers such as these here)
- Waveshare RPi Camera (F) or another compatible camera module
- Flexcable adapter for the camera module
- KY-025 module (Magnetic reed switch)
- DS3231 Real Time Clock Module
- a bunch of Dupont Jumper Wires
- Breadboard (optional)
- Bluetooth-enabled device (e.g., a smartphone) to pair with the Raspberry Pi
It is assumed that the Raspberry Pi Zero 2 W is already set up and running (Optional - set up VNC if you have a hard time working in a headless setup). If you need help setting up the Raspberry Pi Zero 2 W, please refer to the official Raspberry Pi Documentation.
Installations
Clone the Repository to the Raspberry Pi first:
sudo git clone https://github.com/infinitel8p/Security-Cam.git /opt/security-cam
sudo chown -R pi:pi /opt/security-cam
sudo chmod -R u+rwX /opt/security-cam
Now update the Raspberry Pi:
sudo apt update && sudo apt upgrade -y
After the installation is complete install all required Python, Node.js and system packages:
cd /opt/security-cam
./install_requirements.sh
Camera
Attach the camera to the Raspberry Pi. You will need a flex cable adapter for the Pi Zero for the camera module since the Raspberry Pi Zero has a smaller smaller connector than the standard camera module. The camera should be attached to the Raspberry Pi as shown below.
If connected properly you should see a faint red glow from the camera's infrared LEDs when the raspberry is turned on.
Camera Setup
On Raspberry Pi OS Trixie, the camera should work out of the box via libcamera - no /boot/firmware/config.txt changes are needed.
If your camera is not auto-detected, you need to manually add a device tree overlay to /boot/firmware/config.txt. Find your camera's sensor model in the table below and run the command, replacing <your_overlay> with the matching setting statement:
| Camera Model | Setting Statement |
|---|---|
| OV5647 | dtoverlay=ov5647 |
| OV9281 | dtoverlay=ov9281 |
| IMX290/IMX327 | dtoverlay=imx290, clock-frequency=37125000 |
| IMX477 | dtoverlay=imx477 |
| IMX378 | dtoverlay=imx378 |
| IMX219 | dtoverlay=imx219 |
For example, if you are using the Waveshare RPi Camera (F) with the OV5647 sensor:
sudo sh -c 'echo "dtoverlay=ov5647" >> /boot/firmware/config.txt'
sudo reboot
Test the Camera
Check if the camera is detected by the Raspberry Pi with the following command:
rpicam-hello --list-cameras
This should return something like:
pi@securitycam:~ $ rpicam-hello --list-cameras
Available cameras
-----------------
0 : ov5647 [2592x1944 10-bit GBRG] (/base/soc/i2c0mux/i2c@1/ov5647@36)
Modes: 'SGBRG10_CSI2P' : 640x480 [58.92 fps - (16, 0)/2560x1920 crop]
1296x972 [46.34 fps - (0, 0)/2592x1944 crop]
1920x1080 [32.81 fps - (348, 434)/1928x1080 crop]
2592x1944 [15.63 fps - (0, 0)/2592x1944 crop]
Now test the camera by capturing a still image:
rpicam-still -o test.jpg
This should take a picture and save it as test.jpg in the current directory. You can now either view the image on the Raspberry Pi or transfer it to your computer to view it there.
Magnet Reed Switch
If you have a Raspberry Pi Zero 2 W and you have not yet soldered the GPIO header, do it now (You should probably remove the camera flex cable from the raspberry first).
The magnet reed switch will be connected to the Raspberry Pi as shown below.
This is the connection layout using a breadboard. The magnet reed can also just be connected directly to the Raspberry Pi skipping the breadboard.
KY-025 connection
The pinout on the KY-025 module with the already connected jumper wires is shown below.
| KY-025 Pin | Wire |
|---|---|
| GND | Black Jumper Wire |
| DO | Blue Jumper Wire |
| + (VCC) | Red Jumper Wire |
The KY-025 module has a fourth pin, marked with AO. This is the analog output pin and is not used in this project since the Raspberry Pi does not have an analog input pin and a converter such as the ADS1115 would be required to use it.
Raspberry Pi connection
The pinout on the Raspberry Pi with the already connected jumper wires is shown below.
| Raspberry Pi Pin | Wire |
|---|---|
| [pin 14] GND | Black Jumper Wire |
| [pin 15] GPIO22 | Blue Jumper Wire |
| [pin 17] 3V3 | Red Jumper Wire |
Test the Magnet Reed Switch
If connected properly in the previous step, the red LED on the KY-025 module should be lit up when the Raspberry Pi is turned on. To test the magnet reed switch, run the included test script:
python3 /opt/security-cam/setup/test_reed_switch.py
Now test the switch. When you come near the sensor with a magnet the script should print Magnet Detected! if the magnet reed switch is triggered and No Magnet if it is not. Press Ctrl+C to stop the script.
Real Time Clock Module
DS3231 connection
The pinout on the DS3231 module is shown below.
| DS3231 Pin | Wire |
|---|---|
| 3.3V | Red Wire |
| SDA | Orange Wire |
| NC | - |
| SCK | Yellow Wire |
| GND | Green Wire |
The DS3231 module has a fifth pin, marked with NC. This "NC" (Not Connected) pin is not used or connected to anything. (obviously)
Raspberry Pi connection
| Raspberry Pi Pin | Wire |
|---|---|
| [pin 1] 3V3 | Red Wire |
| [pin 3] SDA | Orange Wire |
| [pin 5] SCL | Yellow Wire |
| [pin 6 or 9] GND | Green Wire |
For the connections on the Raspberry you can refer the diagram shown here again
This is the connection layout using a breadboard. The clock can also just be connected directly to the Raspberry Pi skipping the breadboard as shown below.
RTC Setup
-
Enable the I2C interface:
sudo raspi-config nonint do_i2c 0 -
Verify the DS3231 RTC module is detected:
sudo i2cdetect -y 1You should see
68in the grid, which is the module's address:Example Output:pi@securitycam:~ $ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- -
Add the RTC device tree overlay:
This tells the kernel to automatically handle the DS3231 RTC on boot:
sudo sh -c 'echo "dtoverlay=i2c-rtc,ds3231" >> /boot/firmware/config.txt' -
Disable fake-hwclock (if present):
If
fake-hwclockis installed, disable it so it doesn't conflict with the real hardware clock. It's safe to run this even if it's not installed - the commands will simply report that the services don't exist.sudo systemctl disable fake-hwclock fake-hwclock-load fake-hwclock-save 2>/dev/null
sudo systemctl stop fake-hwclock fake-hwclock-load fake-hwclock-save 2>/dev/null -
Reboot the Raspberry Pi:
sudo reboot -
Verify the RTC is working:
After reboot, read the hardware clock time:
sudo hwclock -rIf you have internet and want to sync the RTC time with the time servers, run:
sudo hwclock -w
With the RTC module connected and configured, the Raspberry Pi will now use the RTC module to keep track of the time even when it is turned off.
Continue to wifi.mdx to set up the Raspberry Pi as an access point and connect to it.