Security System Using Raspberry pi and ArduCam 16MP Autofocus

In this tutorial, it is demonstrated how to use raspberry pi and ultrasonic sensor to build a security system

Components Required

Hardware Connection

whatsapp image 2022 08 15 at 3.35.31 pm


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+

2022 08 30 110052 1280x800 scrot

Now, run sudo reboot


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
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")
    GPIO.output(TRIG, True)
    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")

    if 0 < distance < 40:
        camera.capture_file("image%s.jpg" %i)


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.  

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

5 Best Plug-ins for Cura
Common Linux Terminal Commands
WIFI Robot Maneuver With MDDS10
I2C Communication between micro:bit and Arduino
Quadruped Robot with SC08A Servo Controller
Tutorials of Cytron Technologies Scroll to Top