Hardware Setup
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
- 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
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.
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.
If you use OV9281
, IMX290
, IMX378
or non-Raspberry Pi official IMX219
and IMX477
cameras, you need to configure the config.txt file separately:
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 Model | Setting Statement |
---|---|
OV9281 | dtoverlay=ov9281 |
IMX290/IMX327 | dtoverlay=imx290, clock-frequency=37125000 |
IMX477 | dtoverlay=imx477 |
IMX378 | dtoverlay=imx378 |
IMX219 | dtoverlay=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:
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.
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, create the following python script on the Raspberry Pi:
nano test.py
Paste the following code into the file:
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.
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.
Enable I2C Interface
-
To enable the I2C interface, run the following command on the Raspberry Pi:
sudo raspi-config nonint do_i2c 0
-
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: -- -- -- -- -- -- -- -- -
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. -
Set the System Time:
Synchronize the RTC time:
sudo hwclock -w
This will set the hardware clock to the current system time.
-
Enable automatic bus configuration:
sudo sed -i '/^exit 0/i echo ds3231 0x68 > /sys/class/i2c-adapter/i2c-1/new_device' /etc/rc.local
-
Disable the fake-hwclock:
sudo systemctl disable fake-hwclock
sudo systemctl stop fake-hwclockThis 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
-
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.