4.1.4 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/3.1.18components.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/1.1.18_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/3.1.18fritzing.png

Step 2: To connect the camera module and complete the configuration, please refer to: Camera Module.

Step 3: 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 Access for Raspberry Pi.

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

cd ~/raphael-kit/python/

Step 5: Run.

sudo python3 4.1.4_AutomaticCaptureCamera.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/PIR_TTE.png

Note

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

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. After modifying the code, you can run it directly to see the effect.

#!/usr/bin/env python3

from picamera import PiCamera
import RPi.GPIO as GPIO
import time
import os
user = os.getlogin()
user_home = os.path.expanduser(f'~{user}')


camera = PiCamera()

pirPin = 17    # the pir connect to pin17

def setup():
   GPIO.setmode(GPIO.BCM)
   GPIO.setup(pirPin, GPIO.IN)
   camera.start_preview(alpha=200)

def main():
   i = 1
   while True:
      pirVal = GPIO.input(pirPin)
      if pirVal==GPIO.HIGH:
            camera.capture(f'{user_home}/capture%s.jpg' % i)
            print('The number is %s' % i)
            time.sleep(3)
            i = i + 1

def destroy():
   GPIO.cleanup()
   camera.stop_preview()

if __name__ == '__main__':
   setup()
   try:
      main()
   except KeyboardInterrupt:
      destroy()

Code Explanation

pirVal = GPIO.input(pirPin)
if pirVal==GPIO.HIGH:
      camera.capture(f'{user_home}/capture%s.jpg' % i)
      print('The number is %s' % i)
      time.sleep(3)
      i = i + 1

Every time a little squirrel is detected by the PIR module, the Raspberry Pi will take a photo and tell you through the print window how many pictures have been taken. The interval between each photo is 3s.

Phenomenon Picture

../_images/4.1.4spycamera.JPG