Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

4.1.2 Automatic Capture Camera

Introduction

When you are out, the little squirrels in the woods might visit your windowsill. Let’s make a automatic capture camera to leave pictures of these little cuties!

Required Components

In this project, we need the following components.

../_images/4.1.4_automatic_capture_list.png

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Raphael Kit

337

Raphael Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

GPIO Extension Board

BUY

Breadboard

BUY

Jumper Wires

BUY

Resistor

BUY

Camera Module

BUY

PIR Motion Sensor Module

-

Schematic Diagram

T-Board Name

physical

wiringPi

BCM

GPIO17

Pin 11

0

17

../_images/4.1.4_automatic_capture_schematic.png

Experimental Procedures

Before this project, you need to make sure you complete 3.1.1 Photograph Module .

Step 1: Build the circuit.

../_images/4.1.4_automatic_capture_circuit.png

Step 2: Go into the Raspberry Pi Desktop. You may need a screen for a better experience, refer to: Connect your Raspberry Pi. Or access the Raspberry Pi desktop remotely, for a detailed tutorial please refer to Remote Desktop.

Step 3: Open a Terminal and get into the folder of the code.

cd ~/raphael-kit/python-pi5

Step 4: Run.

sudo python3 4.1.4_AutomaticCaptureCamera_zero.py

After the code runs, PIR will detect the surrounding environment, and if it senses the little squirrel passing by, the camera will take a photo. The photo interval is 3 seconds, and the total number of photos taken will be displayed through the print window.

There are two potentiometers on the PIR module: one is to adjust sensitivity and the other is to adjust the detection distance. To make the PIR module work better, you You need to turn both of them counterclockwise to the end.

../_images/4.1.4_PIR_TTE.png

Note

You can also open 4.1.4_AutomaticCaptureCamera_zero.py in the ~/raphael-kit/python-pi5/ path with a Python IDE, click Run button to run, and stop the code with Stop button.

Warning

If there is an error prompt RuntimeError: Cannot determine SOC peripheral base address, please refer to If gpiozero doesn’t work.

Code

Note

You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path like raphael-kit/python-pi5. After modifying the code, you can run it directly to see the effect.

#!/usr/bin/env python3
from picamera2 import Picamera2, Preview
from gpiozero import MotionSensor
import time
import os

# Retrieve the current user's login name and home directory
user = os.getlogin()
user_home = os.path.expanduser(f'~{user}')

# Initialize the camera
camera = Picamera2()
camera.start()

# Initialize the motion sensor on GPIO pin 17
pir = MotionSensor(17)

try:
    i = 1  # Initialize the image count
    while True:
        if pir.motion_detected:
            # Capture an image when motion is detected and save it with a unique number
            camera.capture_file(f'{user_home}/capture%s.jpg' % i)
            print('The number is %s' % i)  # Print the image count
            time.sleep(3)  # Wait for 3 seconds before next detection
            i += 1  # Increment the image count
        else:
            print('waiting')  # Print 'waiting' when no motion is detected
            time.sleep(0.5)  # Check for motion every 0.5 seconds

except KeyboardInterrupt:
    # Stop the camera and turn off the LED if a KeyboardInterrupt occurs
    camera.stop_preview()
    pass

Code Explanation

  1. Imports the Picamera2 and Preview classes for camera control, the MotionSensor class for motion detection, and standard libraries time and os for time handling and operating system interactions.

    #!/usr/bin/env python3
    from picamera2 import Picamera2, Preview
    from gpiozero import MotionSensor
    import time
    import os
    
  2. Retrieves the current user’s login name and home directory for saving captured images.

    # Retrieve the current user's login name and home directory
    user = os.getlogin()
    user_home = os.path.expanduser(f'~{user}')
    
  3. Initializes the camera and starts it.

    # Initialize the camera
    camera = Picamera2()
    camera.start()
    
  4. Initializes a PIR motion sensor connected to GPIO pin 17.

    # Initialize the motion sensor on GPIO pin 17
    pir = MotionSensor(17)
    
  5. Initializes a counter i to track the number of images captured. In an infinite loop, checks if motion is detected. If motion is detected, captures an image, saves it with a unique name based on the counter i, prints the image number, and waits for 3 seconds before checking for motion again. The counter i is incremented after each capture.

    try:
        i = 1  # Initialize the image count
        while True:
            if pir.motion_detected:
                # Capture an image when motion is detected and save it with a unique number
                camera.capture_file(f'{user_home}/capture%s.jpg' % i)
                print('The number is %s' % i)  # Print the image count
                time.sleep(3)  # Wait for 3 seconds before next detection
                i += 1  # Increment the image count
    
  6. If no motion is detected, prints waiting and checks for motion every 0.5 seconds.

    try:
        ...
    
        while True:
            ...
    
            else:
                print('waiting')  # Print 'waiting' when no motion is detected
                time.sleep(0.5)  # Check for motion every 0.5 seconds
    
  7. Catches a KeyboardInterrupt (like Ctrl+C) to stop the camera preview and exit the script gracefully.

    except KeyboardInterrupt:
        # Stop the camera and turn off the LED if a KeyboardInterrupt occurs
        camera.stop_preview()
        pass