App - run it as it is

Motion Trap: a camera that only records when something moves

Source:
apps/motion_trap.py
Widgets:
Camera, Toggle, Status, Button, Slider, Files
Hardware:
A Raspberry Pi with a camera (any Pi before the Pi 5; a NoIR camera for night), an HC-SR501-style PIR sensor; optionally an infrared illuminator, a transistor and a setup LED
Status:
Tested with a real PIR sensor on a Pi 3A+. The infrared light output is built but no real illuminator has been wired to it yet, and picture quality has not been tuned in daylight at a real site.

Motion Trap: a camera that only records when something moves

A camera that records all day fills a card with nothing. A camera trap waits, and records only when something walks past. This app turns a Pi, a camera and a PIR motion sensor into one, and puts the whole thing on your dashboard.

How it behaves

The PIR sensor watches. When it fires, the camera starts recording to the Pi's own storage and the dashboard gets a notice: Motion detected: recording. Each further movement keeps the recording going. Once nothing has moved for a while (10 seconds by default, a slider on the dashboard) the recording stops and a second notice says what was saved. Live video keeps working the whole time, from the same single encode.

Nothing glows at the wildlife

A trap that scares what it is trying to film is a poor trap, so this one is built to be dark:

  • An infrared light for a NoIR camera at night is on exactly while the camera is: switched on just before it opens, so the first frame is lit, and off when it closes, whatever opened it.
  • The Pi's own status lights are switched off while the app runs, and come back when it stops. (Once per Pi, sudo pihub board-leds allow gives the app permission to do that.)
  • A visible LED that lights on motion exists, but only for setting the sensor up (--led-pin 27), and it is off unless you ask for it.

Catching the start of the event

By the time a PIR fires, the interesting part has begun. Pre-roll keeps the camera running quietly and, when motion triggers a recording, includes the seconds before it. On a real Pi a recording started about 5.7 seconds before its trigger with no gaps. It keeps the camera running all the time, so it is off by default; turn it on under Device Information > Video.

On the dashboard

The camera with its buttons, an Armed toggle (off means motion is ignored), a Motion status, and a Test motion button that does exactly what the sensor does. It lets you check the whole chain without waving your arm at the box. A Files section lists the Photos and Videos folders, so a saved recording can be pulled straight to your own machine, peer to peer, without SSH or pulling the SD card.

The wiring

PIR   VCC -> pin 2 (5 V)   OUT -> GPIO 17 (pin 11)   GND -> pin 6
IR    GPIO 22 (pin 15) -> 1 kΩ -> base of an NPN transistor (2N2222 or similar)
      emitter -> GND (pin 9); collector -> the illuminator's minus; its plus -> 5 V (pin 4)
LED   GPIO 27 (pin 13) -> 330 Ω -> LED anode; cathode -> pin 14 (GND)   (setup only)

An infrared illuminator draws far more than a GPIO pin can give, hence the transistor: never wire one straight to a pin. Use one that can be switched, not one with its own light sensor. 940 nm is invisible to animals; 850 nm shows a faint red glow but lights further.

What was tested

On a Pi 3A+ with a real PIR sensor, 24 recordings were triggered, every one complete with the right number of frames. The whole path was watched on the dashboard: Motion, REC and its timer, Recording saved, back to Watching. One recording was played to its end in Chrome. Switching the Pi's own lights off was proven on a Pi Zero W. The infrared light output is built and tested, but no illuminator has been wired to it yet, and the Pi's lights have not been checked dark after a reboot.

Running it

python apps/motion_trap.py --register TOKEN   # first run, or use `pihub setup`
python apps/motion_trap.py                    # every run after

Recordings go to ~/thepihub-recordings (--directory to change it, a USB stick's mount point for example). No hardware? --simulate uses pretend pins and a test-pattern camera, and the Test motion button sets it off, so the whole thing runs on any computer.

← Back to Projects