Skip to main content

Hardware Setup

note

Please ignore possible occurences of the Raspberry Pi Zero 1 W in the instructions. The instructions were originally written for the Raspberry Pi Zero W but were updated to work with the Raspberry Pi Zero 2 W as it is recommended for it's better performance.

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
sudo chmod +x install_requirements.sh
sudo ./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

Edit the /boot/firmware/config.txt file:

sudo sh -c 'echo "start_x=1" >> /boot/firmware/config.txt'
sudo sed -i 's/camera_auto_detect=1/camera_auto_detect=0/' /boot/firmware/config.txt
sudo reboot

This will enable the camera interface and disable the camera auto-detection feature. The Raspberry Pi will then reboot.

info

If you use OV9281, IMX290, IMX378 or non-Raspberry Pi official IMX219 and IMX477 cameras, you need to configure the config.txt file separately:

/boot/firmware/config.txt
sudo sh -c 'echo "start_x=1" >> /boot/firmware/config.txt'
sudo nano /boot/firmware/config.txt

Find camera_auto_detect=1 and modify it to camera_auto_detect=0. At the end of the file, add the following setting statements according to the camera model:

Camera ModelSetting Statement
OV9281dtoverlay=ov9281
IMX290/IMX327dtoverlay=imx290, clock-frequency=37125000
IMX477dtoverlay=imx477
IMX378dtoverlay=imx378
IMX219dtoverlay=imx219

Test the Camera

The WaveShare RPi Camera (F) has a OV5647 sensor which should be supported by libcamera and Raspicam (Raspbian Buster).
Check if the camera is detected by the Raspberry Pi with the following command:

vcgencmd get_camera

This should return something like:

Example Output:
pi@securitycam:~ $ vcgencmd get_camera
supported=1 detected=1, libcamera interfaces=0

Now test the camera with the following command:

python3 -c "import cv2; cap=cv2.VideoCapture(0); ret,frame=cap.read(); cv2.imwrite('opencv_test.jpg', frame) if ret else print('Error: Could not read frame'); cap.release()"

This should take a picture and save it as opencv_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, create the following python script on the Raspberry Pi:

nano test.py

Paste the following code into the file:

test.py
import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

Digital_Pin = 22
GPIO.setup(Digital_Pin, GPIO.IN)

try:
while True:
if GPIO.input(Digital_Pin):
print("Magnet Detected!")
else:
print("No Magnet")
sleep(1)
except KeyboardInterrupt:
print("\nScript closed!")

finally:
GPIO.cleanup()

Save the file and exit the editor. Run the script with the following command:

python3 test.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.

Real Time Clock Module

DS3231 connection

The pinout on the DS3231 module is shown below.

Image 1Image 2
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

Image 2
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

Enable I2C Interface

  1. To enable the I2C interface, run the following command on the Raspberry Pi:

    sudo raspi-config nonint do_i2c 0
  2. Test the DS3231 RTC module:

    You can use the following command to see if your Raspberry Pi detects the DS3231 RTC:

    sudo i2cdetect -y 1

    You should see a number (usually 68 or 0x68) in the grid that corresponds to 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. Configure the I2C-Bus for the x68 address:

    echo ds3231 0x68 | sudo tee /sys/class/i2c-adapter/i2c-1/new_device

    The command sudo hwclock should now return the time from the RTC.

  4. Set the System Time:

    Synchronize the RTC time:

    sudo hwclock -w

    This will set the hardware clock to the current system time.

  5. Enable automatic bus configuration:

    sudo sed -i '/^exit 0/i echo ds3231 0x68 > /sys/class/i2c-adapter/i2c-1/new_device' /etc/rc.local
  6. Disable the fake-hwclock:

    sudo systemctl disable fake-hwclock
    sudo systemctl stop fake-hwclock

    This will prevent the fake-hwclock from overwriting the hardware clock at boot. If you have internet and want to sync the RTC time with the time servers, you can manually run this command again:

    sudo hwclock -w
  7. Reboot the Raspberry Pi:

    sudo reboot

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.