Introduction: Raspberry Pi Motion Sensitive Camera

Using a Raspberry Pi, a Raspberry Pi camera module, a PIR motion sensor, a USB WiFi adapter, a handful of parts, and a couple of Python programs, you can construct a camera that will automatically snap photos or record short videos when something moves in front of the camera and will automatically upload the photos/videos to Dropbox. This instructable shows how to build a Raspberry Pi Motion Sensitive Camera.

My inspiration for making this was to help satisfy my curiosity about the behavior of my cats when I'm not home and to identify the local wildlife culprits that devour the plants in my garden. There are commercial products that can do this but I thought it would be fun to build one that works exactly the way I desire.

The Raspberry Pi executes a Python program that starts when the Raspberry Pi is booted and waits for motion to be detected by the PIR sensor. When motion is detected, the Raspberry Pi snaps a photo or records a short video and then uploads it to a DropBox account. The great thing about uploading to Dropbox is that the photos and videos are available to be viewed by the owner of the Dropbox account on any device (laptop, desktop, tablet, smart phone) from anywhere where internet access is available. When the upload is complete, the program sleeps for a few seconds and then waits again for motion to be detected.

There is a second constantly running Python program that monitors a switch on the side of the camera. This program illuminates an LED in the switch so you know when the camera is operational. When the program detects the switch being pressed, it blinks the LED in the switch and then halts the software in Raspberry Pi. The LED in the switch turns off to indicate that it is safe to unplug the power to the camera.

There is an additional LED mounted on one of the side panels that is illuminated when the Raspberry Pi is connected to power.

A more detailed explanation of how the software works is provided in step 20.

The diagram shows the wiring of the various components to the Raspberry Pi.

The case for the camera was made with 1/8" thick birch plywood cut using a Epilog Helix laser cutter.

This instructable assumes you have basic experience with setting up and running a Raspberry Pi, that you have experience soldering, and that you have access to and experience with a laser cutter.

I made this at TechShop.

Step 1: Parts

The following parts are needed to construct the Raspberry Pi Motion Sensitive Camera:

Raspberry Pi Parts:

  • 1 Raspberry Pi model B 512MB RAM (Available on Amazon or Adafruit ID 998) (photo 1)
  • 1 16GB SDHC class 4 card (photo 2)
  • 1 HK Mini USB WiFi 150Mbps wireless adapter 150M LAN 802.11n/g/b with antenna (available from Amazon) or other supported WiFi adapter for Raspberry Pi (photo 3)
  • 2 3/4" #4-40 Machine screws and nuts (photo 4)
  • 2 1/4" Round nylon spacers for #4 or #6 screws (photo 4)

PIR Sensor Parts:

  • 1 PIR Sensor revision B (Parallax Product ID 555-28027) (photo 5)
  • 2 M2 12mm Machine screws (photo 6)
  • 4 M2 Nuts (photo 6)
  • 2 M2 Washers (photo 6)
  • 3 20cm Female to female jumper wires (Amazon B00AYCON8Y) (photo 25)

Raspberry Pi Camera Parts:

  • 1 Raspberry Pi camera board (available on Amazon or Adafruit ID 1367) (photo 7)
  • 4 M2 10mm Machine screws (photo 8)
  • 4 M2 Washers (photo 8)
  • 4 M2 Nuts (photo 8)
  • 12 #4 Flat nylon washers (Amazon Supply part number B000FN1560) (photo 8)

Shutdown Switch Parts:

  • 1 16mm Illuminated push button red momentary switch (Adafruit ID 1439) (photo 9)
  • Heat shrink tubing (1/16" diameter) (photo 10)
  • 4 12" Jumper wires (one end male, one end female) (Sparkfun part number PRT-09385) (photo 11)
  • 1 470 Ohm 1/4 watt resistor (photo 12)

Power Indicator Parts:

  • 1 Panel mount red LED (Jameco Part no. 141111) (photo 13)
  • 2 12" Jumper wires (one end male, one end female) (Sparkfun part number PRT-09385) (photo 14)
  • Heat shrink tubing (1/16" diameter) (photo 10)
  • 1 470 Ohm 1/4 watt resistor (photo 12)

Case Parts:

  • 2 96mm White plastic wire pull handles (Liberty part number P604AEH-W-C available at Home Depot) (photo 15)
  • 4 #8 x 1/2" Sheet metal screws for use with the handles (do not use the screws that come with the handle - they are too long) (photo 16)
  • 24 #4-40 1/2" Machine screws and nuts for holding the case together (photo 17)
  • 1 Sheet of 24" x 18" 1/8" (3mm) birch plywood (not pictured)

Bottom Panel Parts:

  • 1 1/4-20 Round screw plate (Servocity part number 545468) (photo 18)
  • 4 #6-32 7/16" Pan head machine screws (Servocity part number 90272A147) (photo 19)
  • 4 #6-32 Nuts (Servocity part number 90480A007) (photo 19)

Panel USB Cables:

  • 1 Panel mount USB cable - type B female to micro-B male (Adafruit ID 937) (photo 20)
  • 1 Panel Mount USB Cable - type A male to type A female (Adafruit ID 908) (photo 21)

Power Supply:

  • 1 USB battery pack for Raspberry Pi - 4400mAh - 5V @ 1A (Adafruit ID 1565) (photo 22)
  • 1 USB power adapter 5 volts 2 amps (photo 23)
  • 1 6 foot USB cable with a type B male connector and type A male connector (also called USB printer cables) (photo 24)

Miscellaneous

  • Tripod (photo 26)

Tools (not pictured):

  • Soldering iron and solder
  • Heat source for heat shrink tubing (heat gun, matches, lighter, or candle)
  • Wire cutters
  • Wire strippers
  • Small Philips head screw driver
  • Hemostat
  • Needle nose pliers
  • Painter's tape(to help hold screws in place on the front panel)
  • 120 grade sandpaper

Equipment for installing Raspberry Pi software (not pictured):

  • HDMI capable computer monitor
  • USB keyboard
  • USB mouse
  • Ethernet cable

Step 2: Cut the Case Parts Using a Laser Cutter

The first step is to cut the parts for the case out of a sheet of 1/8" (3mm) birch plywood. A 45 watt Epilog Helix laser cutter at TechShop was used to cut out and engrave the parts. All of the case parts can be cut out from one 24" x 18" sheet of the plywood.

The design is shown in the first diagram. The black lines will be cut by the laser cutter; the blue lines will be etched using a low power cut; green text will be engraved; red lines will not be cut - they are included to show the outline of the parts. I used the color mapping capability of the Laser cutter to specify the settings for cutting and engraving. The setting used are shown in the right hand side of the "Color Mapping" screen capture.

All of the finished parts together are shown in photos 1 and 2.

The individual parts are:

  • Front panel with closeups of the engraving (photos 3 - 10)
  • Back panel (photo 11)
  • Bottom panel (photo 12)
  • Left panel (photo 13)
  • Right panel (photo 14)
  • Top panel (photo 15)

Photos 16 and 17 show a closeup of the cutouts that will be used for the screws and nuts that hold the case together. How these are used are described in step 13.

The burn marks left by the laser can be removed with a gentle sanding using 120 grit sandpaper.

The laser cutting design files are in the ZIP file attached to this step. The file contains CorelDraw (.cdr) and Encapsulated PostScript (.eps) files for the design and the file for loading the color mapping data for the laser cutter. See page 113 of https://www.epiloglaser.com/downloads/pdf/mini_he... for details on how to use color mapping.

Step 3: Prepare the Shutdown Switch

Using the following parts: push button switch (photo 1), jumper wires (photo 2), 470 ohm resistor (photo 3), and heat shrink tubing (photo 4), prepare the switch:

  1. Cut four lengths of heat shrink tubing (photo 5).
  2. Cut off the male ends of the jumper wires (photo 6).
  3. Strip the ends off the wires (photo 7).
  4. Slide the heat shrink tubing onto the wires (photo 8).
  5. Using photo 9 as a guide to the terminals on the switch, solder the resistor to the positive (+) terminal (photo 10).
  6. Attach and solder the red wire to the resistor (photos 11 and 12). Cutoff the excess wire on the resistor.
  7. Attach and solder the black wire to the negative (-) terminal (photos 13 and 14) using photo 9 as a guide.
  8. Attach and solder the blue wires to the remaining terminals (photo 15).
  9. Slide the heat shrink tubing over the resistor and over the exposed wires soldered to the terminals (photo 16). Use a heat gun, lighter, candle, or match to shrink the tubing.

The completed switch is shown in photo 17.

Step 4: Prepare the LED Power Indicator

Start the assembly of the LED power indicator with the panel mount LED (photo 1) and one 470 ohm resistor (photo 2).

  1. Using wire cutters, shorten the length of the resistor lead (photo 3).
  2. Clamp the resistor to the longer LED wire using a hemostat (photo 4 and 5).
  3. Solder the wires together and remove the hemostat (photo 6)
  4. Cut two lengths of heat shrink tubing (photo 7)
  5. Cut the male ends off of two jumper wires (photo 8)
  6. Slide the heat shrink tubing on the jumper wires and strip the ends (photo 9)
  7. Attach and solder the red jumper wire to the resistor (photos 10 and 11). Cutoff the excess wire on the resistor (photo 12).
  8. Attach and solder the black jumper wire to the other wire on the LED (photos 13 and 14).
  9. Slide the heat shrinking tubing over the exposed wires (photo 15). Use a heat gun, lighter, match, or candle to shrink the tubing.

The completed LED power indicator is shown in photo 16.

Step 5: Assemble the Left Panel

Assemble the left panel as follows:

  1. Screw one of the handles (photo 2) to the left panel (photo 1) using two #8 x 1/2" sheet metal screws (photo 3) as shown in photos 4 and 5.
  2. Attach the type B female to micro-B male panel USB cable (photo 6) to the panel as shown in photos 7 and 8.
  3. Attach the shutdown switch (photo 9) to the panel as shown in photos 10 and 11.
  4. Attach the LED power indicator (photo 12) to the panel as shown in photos 13 and 14.

The left panel is now complete.

Step 6: Assemble the Right Panel

Assemble the right panel as follows:

  1. Screw one of the handles (photo 2) to the right panel (photo 1) using two #8 x 1/2" sheet metal screws (photo 3) as shown in photos 4 and 5.
  2. Attach the type A male to type A female panel USB cable (photo 6) to the panel as shown in photos 7 and 8.

The right panel is now complete.

Step 7: Assemble the Bottom Panel

Assemble the bottom panel as follows:

  • Screw the round screw plate (photo 2) to the bottom panel (photo 1) using four #6-32 7/16" pan head machine screws and nuts (photo 3) as shown in photos 4 to 6. Tighten the screws firmly.

The completed bottom panel is shown in photo 7.

Step 8: Create the Dropbox Platform App

In this step, you will create a Dropbox platform app. Creating the app will allow the Raspberry Pi Motion Sensitive Camera to be able to upload photos and videos to your Dropbox account.

Create a Dropbox account if you don't have one already. Go to http://www.dropbox.com and click on the "Sign up" button. If you already have a Dropbox account, sign in to Dropbox.

  1. After you have signed in, go to http://www.dropbox.com/developers. Click on "App Console" as shown in screen capture 1. The "Your apps" page will display.
  2. Click the "Create app" button (screen capture 2).
  3. Select the "Dropbox API app" option (screen capture 3).
  4. Select the "Files and datastores" option (screen capture 4).
  5. Select the "Yes - My app only needs access to the files it creates" option (screen capture 5).
  6. Use "RPi-MS-Camera" as the name for application and enter it into the text box (screen capture 6).
  7. Click on the "Create app" button (screen capture 7).
  8. You have now authorized your application to access Dropbox. Print the screen. You will need to enter the App key and the App secret (screen 8) on your Raspberry Pi in the next step.

Step 9: Prepare the Raspberry Pi Software

The first step in preparing the Raspberry Pi is to install and configure the Raspbian software:

  1. Connect the Raspberry Pi camera to the Raspberry Pi using the instructions that come with the camera module.
  2. Download and load Raspbian (http://www.rasbian.com/RaspbianImages) onto a SD card. Instructions for how to do this can be found here (https://learn.adafruit.com/adafruit-raspberry-pi-lesson-1-preparing-and-sd-card-for-your-raspberry-pi/overview). I used the September 2014 version of Raspbian: I would recommend using this version or a later one for this project: don't use an earlier version.
  3. Insert the SD card into the slot on the Raspberry Pi.
  4. Connect the Raspberry Pi to a display, keyboard, mouse, LAN with access to the internet, and a 5 volt USB power adapter as shown in the "Raspberry Pi Setup" diagram.
  5. Boot the Raspberry Pi and perform the first time configuration according to the instructions here (https://learn.adafruit.com/adafruits-raspberry-pi-lesson-2-first-time-configuration). When configuring the Raspbian, do not configure the graphical user interface to start automatically. Make sure to enable the camera and it's a good idea to change the password for the Raspberry Pi at this time.

The second step is to download the Python programs that control the shutdown switch and that snap pictures or take photos when motion is detected. Create the directory where the Python programs will reside and where the photos and videos will be stored using these commands:

cd /home/pi
mkdir python_programs
cd python_programs
mkdir camera_output

Issue the following two commands to get the Python programs:

wget "https://s3-us-west-1.amazonaws.com/talk2bruce/instructables/rpi-ms-camera/rpi-halt-btn.py"
wget "https://s3-us-west-1.amazonaws.com/talk2bruce/instructables/rpi-ms-camera/rpi-ms-camera.py"

The third step is to install the Dropbox uploader and configure it use the App key and the App secret from step 8 "Create the Dropbox Platform App". Install the uploader with the following commands:

cd /home/pi
git clone "https://github.com/andreafabrizi/Dropbox-Uploader.git"

if this command fails, you will need to install git with the command below and then re-issue the git clone command above:

sudo apt-get install git-core

After installing the Dropbox-Uploader from git, change to the Python programs directory with the following command:

cd /home/pi/python_programs

Issue the command below to configure the Dropbox uploader:

sudo python rpi-ms-camera.py -firsttime

You should ignore the five things that are listed on the screen as things to do - you did those in the Step 8 of this instructable. What you do need to do is to enter the app key and app secret from step 8 and select "a" for the permission type (see the first arrow on Screenshot 1). After you hit enter on the permission type, you will be instructed to enter a web address into a browser (see second arrow on Screenshot 1). Carefully write down the web address and go to a computer with a web browser and enter it. After you enter it, the web browser will display a page asking you to allow the app to access it's folder on Dropbox (screenshot 2). Click the "allow" button. The next web page displayed will let you know that the app was successfully connected to Dropbox (screenshot 3). Hit "Enter" as instructed on the Raspberry Pi screen. You will get confirmation that the setup completed on the Raspberry Pi as shown in screenshot 4. If it fails, it's most likely due to mistyping of the app key, app secret, or the web address. Try again by typing in the command above and re-entering the information.

Next, test the connection to Dropbox by issuing the command:

sudo python rpi-ms-camera.py -test

This command will send a file containing the IP address of your Raspberry Pi to Dropbox. The output of the command is shown in screenshot 5. You should find the file in the application in the App folder in your Dropbox account.

The fourth step is to configure the Raspberry Pi for use with your wireless network. Edit the file "wpa_supplicant.conf" with the command:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Add the following lines to the bottom of the file substituting the name of your wifi network and your wifi password as shown and save the file. If you are unfamiliar with how to use the nano editor, howtogeek has a nice Beginner's Guide to Nano.

network={
  ssid="my wifi network name"
  psk="my wifi password"
}

The file should look like the what is shown in screenshot 6. More details on how to configure the Raspberry Pi for different kinds of wifi networks can be found on the Sparkfun website.

The fifth step is to configure the system to start the Python program that monitors and illuminates the shutdown button during the system boot process. Edit the file "rc.local" with the command:

sudo nano /etc/rc.local

Add the line command below to the bottom of the file before the line that says "exit 0" and save the file.

python /home/pi/python_programs/rpi-halt-btn.py&

The file should look like the one in screenshot 7.

The sixth step is to configure the system to run the Python camera program when the WiFi on the Raspberry Pi is up and running. When the Python camera program is running, it waits for motion to be detected and then takes photos or short videos and uploads them to Dropbox. The command to start the program is placed in the "etc/network/interfaces" file. Edit the file with the command:

sudo nano /etc/network/interfaces

If you want to have the camera take photos, add the following command to the bottom of the file:

post-up python /home/pi/python_programs/rpi-ms-camera.py -p

If you want to have the camera record videos, add the following command to the bottom of the file:

post-up python /home/pi/python_programs/rpi-ms-camera.py -v

Screenshot 8 shows what the file should look like when configured to take photos.

Configuration of the Raspberry is now complete.

The last step is to shutdown the Raspberry Pi with the command:

sudo halt

After the Raspberry Pi shuts down, disconnect all the cables but leave the SD Card in the socket on the Raspberry Pi.

Step 10: Mount the PIR Sensor on the Front Panel

Using the two M2 machine screws, two M2 washers and four M2 nuts (photo 2) mount the PIR sensor (photo 3) to the front panel (photo 1) as follows:

  1. Place the two washers over the holes on outside (non-engraved) of the panel and insert the the machine screws (photo 4).
  2. While holding the screws in place (painter's tape can help hold the screws in position), flip over the panel and screw on two of the nuts (photo 5).
  3. Slide the PIR sensor onto the screws and secure it in place using two nuts (photo 6). Make sure the connector pins are facing down over the "PIR Sensor" engraving.

The mounted PIR sensor is shown in photo 7.

Step 11: Mount the Raspberry Pi on the Front Panel

Using two 3/4" #4-40 Machine screws, nuts, and nylon spacers (photo 1), mount the Raspberry Pi on the front panel as follows:

  1. Insert the screws into the outside (non-engraved) side the panel (photo 2).
  2. While holding the screws in place (painter's tape can help hold the screws in position), flip over the panel (photo 3).
  3. Slide the nylon spacers on to the screws (photo 4).
  4. Gently slide the Raspberry Pi on to the screws (photo 5).
  5. Secure the Raspberry Pi using the nuts (photo 6). Do not over tighten the nuts: they should be "monkey tight", not "gorilla tight".

Step 12: Mount the Camera on the Front Panel

Using four M2 10mm machine screws, four M2 washers, twelve #4 flat nylon washers, and four M2 nuts (photo 2) mount the Raspberry Pi camera (photo 1) as follows:

  1. Place a washer over each of the holes on the outside of the panel (non-engraved side) and insert the four screws (photo 3).
  2. While holding the screws in place (painter's tape can be used to hold the screws in position), flip over the panel and slide two nylon spacers over each of the screws (photo 4).
  3. Place the camera over the screws, put a nylon spacer over the screw and gently screw the nut in place (photos 5 and 6). Do not over tighten the nuts. The mounted camera is shown in photos 7.

The completed front panel with the PIR sensor, camera, and Raspberry Pi is shown in photos 8 and 9.

Step 13: Assemble the Case

The case for Raspberry Pi Motion Sensitive Camera is held together with #4-40 1/2" Machine screws and nuts: gluing is not required. The way to do this is to thread the nuts on the screws (photo 2), fit the panels together (photo 1), place the screw into the T-slot (photo 3) and gently tighten with a screw driver while holding the nut with your fingers. When the whole case is held together this way, it's very sturdy.

Now, assemble the case as follows:

  1. Start with the top panel (photo 4) and attach it to the left panel by the shutdown switch (photo 5).
  2. Attach the left panel to the bottom panel (photo 6).
  3. Attach the other end of the bottom panel to right panel (photo 7).
  4. Attach the other end of the right panel to top panel (photo 8). The side panels should now all be attached and secured using the screws and T-slots.
  5. Position the side panels as shown in photo 9 and gently push the top panel into place (photo 10). Make sure that the engraving on the inside of the front panel matches the placement of the side connectors (see photo 16).
  6. Secure in place with the screws and T-slots as shown in photos 11 to 14.

The case should be securely held together and look like photos 15 and 16. The back panel will be attached to the case in step 18.

Step 14: Connect the Internal USB Cables

Now that the case is mostly assembled (photo 1), it's time to start connecting the components.

    Connect the type A male USB connector one of the type A female USB connectors on the Raspberry Pi (photo 2). This USB connector will be used to connect the USB WiFi adapter to the Raspberry Pi.

    Connect the micro B male USB connector to the female micro B USB connector on the Raspberry Pi (photo 3). This USB connector will be used to provide power to the Raspberry Pi.

    Step 15: Connect the GPIO Wires

    In this step, the LED power indicator, the PIR sensor, and the shutdown switch will be connected to the GPIO pins on the Raspberry Pi (photo 1).

    1. Using the "Wiring the Raspberry Pi Motion Sensitive Camera - Step 1" diagram and photos 2 and 3 as guides, connect the PIR sensor jumper wires to GPIO pin 23, +3.3V power, and ground. A Python program running on the Raspberry Pi will monitor the GPIO pin 23 and when motion is detected it will snap a photo or record a video clip.
    2. Using the "Wiring the Raspberry Pi Motion Sensitive Camera - Step 2" diagram and photo 4 as a guide, connect the LED power indicator to +5V power and ground. When the Raspberry Pi has power connected, the LED will illuminate.
    3. Using the "Wiring the Raspberry Pi Motion Sensitive Camera - Step 3" diagram and photo 5 as a guide, connect the blue wires from the shutdown switch to GPIO pin 25 and ground (it does not matter which wire you connect to which pin). A Python program on the Raspberry Pi will monitor the switch and when the button is pressed, the program will issue a command to shutdown the Raspberry Pi.
    4. Using the "Wiring the Raspberry Pi Motion Sensitive Camera - Step 4" diagram and photo 6 as a guide, connect the power wires from the shutdown switch to GPIO pin 4 power and ground. A Python program on the Raspberry Pi will illuminate the switch when the Raspberry Pi is starting and will turn off the LED with the Raspberry Pi has shutdown.

    The Raspberry Pi Motion Sensitive Camera with all the internal wiring completed is shown in photo 7.

    Step 16: Assemble the WiFi Antenna

    Attach the antenna to the USB WiFi adapter as shown in the photos.

    Step 17: Set Up and Test the Camera

    Set up and test the Raspberry Pi Motion Sensitive Camera as follows:

    1. Attach the camera to tripod (photos 1 and 2).
    2. Plug the USB WiFi adapter into the USB connector on the camera (photo 3).
    3. Attach the 6 foot USB cable to the camera (photo 4).
    4. Connect the other end of the USB cable to the USB Power Adapter (photo 5) and plug the power adapter into an AC power outlet.
    5. The LED power indicator should be illuminated (photo 6).
    6. After a minute or two, the shutdown switch should illuminate indicating that the Raspberry Pi is up and running (photo 7).
    7. The LED on the USB WiFi adapter should also be illuminated (photo 8).
    8. The PIR sensor will illuminate for several seconds as it calibrates itself (photo 9).
    9. If you have a Windows computer set up with Dropbox, you will see a pop up message indicating that the camera has uploaded a text file to your Dropbox account (screenshot 10). This file is uploaded every time the camera is started; the file contains the IP address that was assigned to Raspberry Pi.

    Test the camera:

    1. Wave your hand in front of the PIR sensor. The sensor should illuminate (photo 9).
    2. Within a minute or so, a pop message should appear on your computer indicating that a photo (or video depending on how you configured the camera) has been uploaded (screenshot 11).
    3. Go to the "RPi-MS-Camera" in the "Apps" directory in your Dropbox folder and should see both the text file and the photo that was taken by the camera (screenshot 12).
    4. Repeat the above three several times to ensure everything is working properly.

    Setup and testing is now complete!

    Step 18: Final Assembly

    The last step in constructing the Raspberry Pi Motion Sensitive Camera is to attach the back panel.

    1. Place the panel on back of the camera case and gently push it into place (photo 1).
    2. Using #4-40 1/2" machine screws and nuts, thread the nuts on the screws and place the screw into the T-slot (photo 2) and gently tighten with a screw driver while holding the nut with your fingers.

    Step 19: Voila!

    The Raspberry Pi Motion Sensitive Camera is now complete!

    Photos 1 to 7 show the camera in operation.

    If the camera is going to be used in a place where it's inconvenient or not possible to get power from an AC outlet, a 5 volt 1 amp USB battery pack can be used (photos 8 and 9). According to Adafruit, the power pack in the photos will power a 'headless' Raspberry Pi (no keyboard, mouse, display attached) with a mini WiFi adapter plugged into the USB port for about 7 hours.

    Step 20: How the Python Programs Work

    There are two customer Python programs used in this instructable to create the Raspberry Pi Motion Sensitive Camera.

    rpi-ms-camera.py

    The first program "rpi-ms-camera.py" is started by the command:

    post-up python /home/pi/python_programs/rpi-ms-camera.py -p

    in the system file "/etc/network/interfaces". The "post-up" command is run when the network interfaces are up and running.

    Listings 1 to 6 show the source code for the rpi-ms-camera.py program:

    Lines 4-26 (listing 1) import needed libraries and define constants that will be used within the program.

    • Lines 4-6 import the libraries that are required to issue system commands, control the camera module, get the time, and allow the program to read and write the GPIO pins.
    • Line 10 defines which GPIO pin is used to detect motion detected by the PIR sensor.
    • Lines 13 and 14 are constants used for output messages that indicate what mode the camera is operating.
    • Line 17 defines what directory will be used to store the most recent photo or video captured by the camera.
    • Line 20 defines the location of the program that is used to upload files to Dropbox.
    • Line 23 sets the number of seconds of video that will be recorded when motion is detected. This can be changed to suit your needs. The more seconds of video recorded, the longer it will take to upload to Dropbox.
    • Line 26 sets the number of seconds the program will wait after snapping a photo or recording a video. This delay is used to prevent taking a lot of photos/videos in a row of the same event. This can be changed if more frequent or less frequent photos/videos are to be taken.

    Lines 28-31 (listing 2) define the "generate_file_name" function. This function is used to generate a unique file name based on the date and time. This file name will be used for the photos or videos. The format of the name generated is "yyyy-mm-dd-hh-MM-ss-tz" where yyyy is the year, mm is the month, dd is the day, hh is the hour, MM is the minute, ss is the seconds, and tz is the time zone.

    Lines 33-46 (listing 2) define the "motion_detected" function. This function is called when the PIR sensor has detected motion. This function snaps a photo or records video, uploads the photo or video to Dropbox, and deletes the file from the Raspberry Pi.

    • Line 36 creates the file name to be used for the photo or video.
    • Line 37 prints a message indicating that motion has been detected.
    • Lines 38-40 are executed if the camera mode has been set to snap photos.
      • Line 39 adds the ".jpg" extension to the name of the file.
      • Line 40 calls the "snap_photo" function to take the photo.
    • Lines 41-43 are executed if the camera mode has been set to record video.
      • Line 42 adds the ".h264" extension to the name of the file.
      • Line 43 calls the "record_video" function to record the video.
    • Line 44 calls the "upload_to_dropbox" function to upload the photo or video to Dropbox.
    • Line 45 deletes the photo or video. This is done to prevent the Raspberry Pi SD card from filling up with photos or videos.
    • Line 46 prints a message indicating that the file has been deleted.

    Lines 48-53 (listing 3) define the "snap_photo" function. This function is used to take a photo.

    • Line 51 sets the resolution for the photo.
    • Line 52 snaps the photo and stores it in the specified file.
    • Line 53 prints a message indicating that a photo was taken.

    Lines 55-64 (listing 3) define the "record_video" function. This function is used to record a video.

    • Line 59 sets the resolution for the video.
    • Line 60 starts recording the video and stores it in the specified file.
    • Line 61 prints a message indicating that video recording has started.
    • Line 62 makes the program wait until for the specified number of seconds.
    • Line 63 stops the recording of the video.
    • Line 64 prints a message indicating that the video has stopped recording.

    Lines 66-74 (listing 3) defines the "upload_to_dropbox" function. This function will upload the specified file to Dropbox.

    • Line 70 extracts just the file name from the full file name that was passed in to the function. This will be the name of the file stored in Dropbox.
    • Line 71 prints a message about what file will be uploaded.
    • Line 72 defines the full command that will be used to up load the file. The command consists of the name and location of the upload program, the "upload" option for the command, the local file name, and the name that should be used for the file on Dropbox.
    • Line 74 uses the system function "subprocess.call" to execute the command.

    Lines 76-85 (listing 4) define the "upload_ip_address" function. This function is used to create a file with the IP address of the Raspberry Pi and upload it to Dropbox. The purpose of uploading a file with the IP address is to make it easy to get the IP address of the Raspberry Pi for debugging purposes. With the IP address, it is possible to use "ssh" to open a terminal session to the Raspberry Pi and issue Linux commands.

    • Line 80 prints a message indicating that the IP address will be uploaded to Dropbox.
    • Line 81 creates the name of the file that will contain the IP address.
    • Line 82 constructs the command to get the IP address from the system. The command is "hostname -I >" followed by the file name.
    • Line 83 uses the system function "subprocess.call" to execute the command.
    • Line 84 calls "upload_to_dropbox" to upload the file to Dropbox.
    • Line 85 deletes the file from the Raspberry Pi.

    Lines 87-91 (listing 4) define the "first_time_config" function. This function is called during the process to set up the Raspberry Pi when the command "python rpi-ms-camera.py -firsttime" is issued. This command is used in Step 9 of this instructable.

    • Line 91 uses the system function "subprocess.call" to issue the command that uploads files to Dropbox. When this command issued for the first time, it asks the user to input the information required to connect to Dropbox.

    Lines 93-99 (listing 4) define the "test_dropbox" function. This function is called during the process to set up the Raspberry when the command "python rpi-ms-camera.py -test" is issued. This command creates a test file containing the IP address of the Raspberry Pi and uploads it to Dropbox. If it successfully sends the file, then the Raspberry Pi has been properly set up for use with Dropbox. It is used in step 9 of this instructable.

    • Line 97 prints a message indicating that the Dropbox connection is about to be tested.
    • Line 98 calls the "upload_ip_address" function to create and upload a file containing the IP address of the Raspberry Pi.
    • Line 99 prints a message indicating the upload has completed and asks the user to check the Dropbox app folder to verify the file has successfully uploaded.

    Lines 100-133 (listing 5) are the beginning of the main program.

    • Lines 107-110 are comments that show all of the options for the program.
    • Line 113 checks to see whether any options have been specified. If options have been specified the program continues on line 114. If no options have been specified, the program goes to line 127.
    • Lines 114-115 check to see if the option for taking photos has been specified and if so sets the camera mode to photos.
    • Lines 116-117 check to see if the option for recording video has been specified and if so sets the camera mode to videos.
    • Lines 118-120 check to see if the first time option was specified and if so calls the "first_time_config" function and then exits the program.
    • Lines 121-123 check to see if the test option was specified and if so calls the "test_dropbox" function and then exits the program.
    • Lines 124-126 are reached if some other option was specified. Since this an unrecognized option, an error message is printed, and the program exits.
    • Lines 127-133 are executed when no options have been specified. Lines 128-132 print what all the valid options are and then line 133 exits the program.

    Lines 135-164 (listing 6) perform initialization for the main program.

    • Lines 135-136 print messages indicating the Raspberry Pi Motion Sensitive Camera is starting and which mode (photo or video) will be used.
    • Lines 138-141 sets up the camera module. Line 139 initializes the camera. Lines 140-141 indicate to the camera to flip the image so it will be right side up from the user's perspective. Flipping the image is necessary because of the way the camera is mounted to the front panel.
    • Lines 143-145 sets up the GPIO pin for the PIR motion sensor. There are two numbering schemes for the GPIO pins: line 144 tells the GPIO software what scheme this program will use. Line 145 sets the pin for the PIR sensor to be an input pin.
    • Line 149 calls the "upload_ip_address" function to upload a file to Dropbox containing the IP address assigned to this Raspberry Pi. This is used for debugging but also to let you know that the camera is up and running.

    Lines 152-163 (listing 6) is the main loop for the program.

    • Line 152 defines a loop that will repeat indefinitely until the the Raspberry Pi is shut down.
    • Line 154 prints a message indicating that the camera will be waiting to detect motion.
    • Line 155 waits for a signal from the PIR sensor indicating that motion has been detected.
    • Line 156 calls the "motion_detected" function to snap a photo or record a video and upload it to Dropbox.
    • Line 157 prints a message indicating that the camera will sleep for a number of seconds. The purpose of this delay is prevent too many photos/videos from being taken for the same motion event.
    • Line 158 sleeps for the specified number of seconds.
    • After sleeping has ended, the program goes back to line 154.

    Lines 159-163 (listing 6) are used for debugging purposes. These lines will be executed if the program was run from the command line and Ctrl/C is pressed to stop it.

    • Line 160 prints a message indicating the user has stopped the program
    • Line 161 stops the camera module.
    • Line 162 stops the monitoring of the GPIO pins.
    • Line 163 stops the loop causing the program to exit.

    rpi-halt-btn.py

    The second program "rpi-halt-btn.py" is started during the boot process by the command

    python /home/pi/python_programs/rpi-halt-btn.py&

    in the system file "/etc/rc.local". The "&" at the end of the command makes the program run as a separate process that keeps running until the program exits.

    Listing 7 shows the source code for the rpi-halt-btn.py program:

    Lines 2-13 initialize the program.

    • Lines 2-3 import the libraries that are required to control the devices connected to the GPIO pins, issue system commands, allows the program to sleep.
    • Lines 6-7 define the pin that will monitor the shutdown switch and the pin that will control the illumination of the LED in the switch.
    • Line 8 tells the GPIO software what numbering scheme the program will use for the GPIO pins.
    • Line 9 sets the GPIO pin for controlling the LED in the switch to be an output pin.
    • Line 10 sets the GPIO pin for monitoring the switch for presses to be an input pin.
    • Line 11 illuminates the LED in the switch by setting the value of the pin to true.
    • Line 13 prints a message indicating that the switch is now being monitored.

    Line 18 causes the program to wait for the switch to be pressed.

    Lines 19-21 are used for debugging. When the program is run from the command line and Ctrl/C is pressed these lines will be executed.

    • Line 20 will print a message indicating that the user stopped the program.
    • Line 21 stops the monitoring of the GPIO pins.

    Lines 22-26 are run when the shutdown switch has been pressed.

    • Line 23-25 cause the LED on the switch to turn off briefly to give the user feedback that the switch was pressed.
      • Line 23 turns the LED off.
      • Line 24 causes the program to sleep for half a second.
      • Line 25 turns the LED back on.
    • Line 26 uses the "subprocess.call" function to execute the "sudo halt" command which will cause the Raspberry Pi to shutdown.

    When the Raspberry Pi has shutdown the LED on the shutdown switch will turn off indicating it's safe to unplug the camera from the power outlet.

    Step 21: Possible Improvements

    As I was constructing the Raspberry Pi Motion Sensitive Camera, a number of improvements came to mind:

    Control whether photos are taken or videos are recorded

    The camera is set to take photos or record video by specifying the "-p" or "-v" option on the "post-up python /home/pi/python_programs/rpi-ms-camera.py" command that is set in "/etc/network/interfaces". This can be inconvenient if you desire to change the camera mode frequently. The way the mode is changed in the current design is to use a program to "ssh" into the camera, edit the "/etc/network/interfaces" file, and reboot.

    A good improvement would be to add a two position switch to the lower part of the right panel for setting whether the camera would be in photo or video mode (photo 2). The switch would need to be wired to the Raspberry Pi and lines 113-126 of "rpi-ms-camera.py" would have to be modified to remove the checks for the "-p" or "-v" options. Line 38 would have to be replaced with a check the value of the GPIO pin to which the switch is attached to determine what action to take.

    Reduce side motion from triggering the camera

    While using the camera, I've noticed that the PIR sensor is sensitive enough to detect motion from its sides in addition to motion in front of the sensor. This causes photos to be taken or videos recorded when the moving object may not be in view of the camera. The design could be improved to eliminate the superfluous photos/videos by restricting the sensor. This could be done by using a 1 1/2" PVC bushing (photos 3 and 4) mounted using glue or tape to the front panel (photos 5 - 7).

    Reduce the size of the case

    The case for the camera is fairly large. The reason for the large size is the use of the USB panel mount connectors and cables. I wanted to use these because I really liked the idea of being able to plug the WiFi adapter and the USB power cable into the sides of the case. Because of the thickness of the panel mount cables, extra room is needed inside the case to accommodate them (photo 8). The case could be made significantly smaller if the Raspberry Pi is mounted similarly to the way the Raspberry Pi was mounted in my Raspberry Pi PirateBox instructable. In that design, the Raspberry Pi was mounted adjacent to the side panel to allow access to USB connectors for the WiFi adapter; the power cable was plugged directly into the Pi and out through a hole in the side panel. Doing this with the camera would allow the case to be significantly smaller (photo 9).

    Tech Contest

    Participated in the
    Tech Contest

    Formlabs Contest

    Participated in the
    Formlabs Contest

    Microcontroller Contest

    Participated in the
    Microcontroller Contest