Your shopping cart is empty!
Security System Using Raspberry pi and ArduCam 16MP Autofocus
- Abdulrahman
- 16 Aug 2022
- 2140
In this tutorial, it is demonstrated how to use raspberry pi and ultrasonic sensor to build a security system
Components Required
- Raspberry Pi 4 Model B 1GB
- 5VDC HC-SR04 Ultrasonic Sensor
- Breadboard 8.5x5.5cm
- Resistors 220 ohm x2
- ArduCam 16MP IMX519 Autofocus Camera Module for RPi and Jetson
Hardware Connection
Wiring
There are 4 pins in Ultrasonic Sensor that must be connected:
- VCC to +5v
- Trig to GPIO 21
- Echo to GPIO 20 & to GND ( with 2 resistors 220 ohm)
- GND to GND
Note: Use 1 resistor to connect Echo to raspberry pi and another resistor to connect it to ground
Software Requirements
After doing the hardware connection and setting up your raspberry pi. There are some Libraries need to be downloaded to use the camera.
Note: if the camera does not work after downloading, please run
sudo apt-get update
and
sudo apt-get upgrade
then
sudo reboot
Next, we download the Picamera2 Library which is is the libcamera-based replacement for Picamera which was a Python interface to the Raspberry Pi's legacy camera stack. Picamera2 also presents an easy to use Python API.
All the necessary packages can now be installed via apt and pip3, so the following should suffice. First, please run:
sudo apt install -y python3-libcamera python3-kms++
sudo apt install -y python3-prctl libatlas-base-dev ffmpeg libopenjp2-7 python3-pip
pip3 install numpy --upgrade
NOGUI=1 pip3 install git+https://github.com/raspberrypi/picamera2.git
Now, run
sudo reboot
Coding
open python software in your Raspberry pi and run the following code:
import RPi.GPIO as GPIO
import time
from picamera2 import Picamera2
from time import sleep
GPIO.setmode(GPIO.BCM)
TRIG = 21
ECHO = 20
i= 0
camera = Picamera2()
while True:
print("Distance Check")
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
GPIO.output(TRIG, False)
print("Calming Down")
time.sleep(0.2)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
print("distance:", distance, "cm")
time.sleep(2)
if 0 < distance < 40:
camera.start()
i=i+1
sleep(2)
camera.capture_file("image%s.jpg" %i)
camera.stop()
time.sleep(2)
after running the code, the camera will take a picture whenever the sensor detect an object in the range specified in the code. All pictures will be sent to the desktop
Thank You
Thanks for going through this tutorial. If you have any technical inquiries, please ask in the comment section.