Skip to main content

Hardware Setup

note

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

info

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.

270868898-25d4370a-b620-4d31-9c9b-fb232d06bef7

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.

info

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 ModelSetting Statement
OV5647dtoverlay=ov5647
OV9281dtoverlay=ov9281
IMX290/IMX327dtoverlay=imx290, clock-frequency=37125000
IMX477dtoverlay=imx477
IMX378dtoverlay=imx378
IMX219dtoverlay=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:

Example Output:
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.

20230927_072021

note

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.

image

KY-025 PinWire
GNDBlack Jumper Wire
DOBlue Jumper Wire
+ (VCC)Red Jumper Wire
note

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.

image

image

Raspberry Pi PinWire
[pin 14] GNDBlack Jumper Wire
[pin 15] GPIO22Blue Jumper Wire
[pin 17] 3V3Red 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 module frontDS3231 module back
DS3231 PinWire
3.3VRed Wire
SDAOrange Wire
NC-
SCKYellow Wire
GNDGreen Wire
note

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

20230927_172741

Raspberry Pi PinWire
[pin 1] 3V3Red Wire
[pin 3] SDAOrange Wire
[pin 5] SCLYellow Wire
[pin 6 or 9] GNDGreen Wire
note

For the connections on the Raspberry you can refer the diagram shown here again

RTC wiring diagram
tip

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.

61JfhAHWeKL AC_SX679

RTC Setup

  1. Enable the I2C interface:

    sudo raspi-config nonint do_i2c 0
  2. Verify the DS3231 RTC module is detected:

    sudo i2cdetect -y 1

    You should see 68 in 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: -- -- -- -- -- -- -- --
  3. 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'
  4. Disable fake-hwclock (if present):

    If fake-hwclock is 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
  5. Reboot the Raspberry Pi:

    sudo reboot
  6. Verify the RTC is working:

    After reboot, read the hardware clock time:

    sudo hwclock -r

    If 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.