aoakley.com

Raspberry Pi Goblin Detector

A Python programming project for the Raspberry Pi together with the Pibrella add-on, plus a standard passive infra-red (PIR) movement sensor, for the Raspberry Jam at PyCon UK 2014. Also suitable for other 3-wire sensors.

My eight-year-old daughter Annabel's bedroom is plagued by goblins (her five-year-old sibling twins). Annabel and I created a Goblin Detector, which guards her bedroom while she's out, like a burglar alarm. If it detects movement, it sounds a buzzer. We used the Pibrella add-on to make the wiring easier, to give us some lights, a button and a buzzer ready-to-go.

Update: You can read Annabel's write-up and photos (PDF) of Raspberry Jam, Coventry, 20 Oct 2014. This page was also featured on the official Raspberry Pi blog on 14 Oct 2014.

All content on this page, including the PDF, is public domain - you may copy, re-use and adapt it in any way you see fit, for any purpose, without the need for attribution or permission.

Equipment

  • 1x Raspberry Pi - any model A, B v1, B v2, B+ - around £25.
  • 1x Pibrella - £10
  • 1x HC-SR501 PIR Motion Sensor Module - should be around £3-£5 from eBay. Teachers - look out for listings with bulk discounts for 3, 5, 10 sensors; I've seen ten for under £10 if you're prepared to wait for delivery from China.
  • 3x Male to Female Jumper Wires - usually supplied as a coloured strip of 40 or so for about £2-£4
  • 1x small empty clean margarine tub - optional, to house the sensor.
  • A case, optional. I used the Pibow Coupé (£8.50) which gives easy access to the GPIO pins but there are many cheaper alternatives or even a paper/card case for the model B and for the B+ that you can print out for free (remember to select the "actual size" option when you print).

Preparation

With the power off, fit the Pibrella to the GPIO pins on the Raspberry Pi.

The HC-SR501 module will have three pins; VCC (voltage in), OUT (signal out) and GND (ground). The sensor takes power from VCC and GND, and then sends a signal along OUT when it detects motion. Using the jumper leads, wire this up to the Pibrella as follows:

  • Motion sensor VCC to Pibrella Out E+
  • Motion sensor OUT to Pibrella In A-
  • Motion sensor GND to Pibrella Out E-

The sensor needs to be pointed in the direction of expected movement. We used a plastic tub to mount the sensor. We cut a square hole (about 22mm) into the lid of an empty clean margarine tub, and fixed the sensor from the inside with a bit of Blu-tak. We cut a small hole in the base of the tub for trailing wires. We chained together several male-female jumper wires (they plug into each other) to give us a bit more length.

Calibration

The HC-SR501 module comes with two potentiometers (turny things) which can be turned using a screwdriver. Despite being cross-head, I recommend a small electrical flat-head screwdriver, and be gentle. If you look closely you'll see the circuitboard beneath them is labelled Sx and Tx. With the sensor dome face down, and the potentiometers towards you:

  • On the left - Sx - sensitivity. All the way anticlockwise will detect small movements; all the way clockwise might require a car to pass by! The minimum is about a 7 o'clock position, and the maximum around 5 o'clock. I set this at about an 11 o'clock position.
  • On the right - Tx - time. How long to send a signal after motion is detected. All the way anticlockwise gives you about one second; all the way clockwise gives you twenty or so seconds. The minimum is about 7 o'clock position and the maximum around 5 o'clock. I set this at about a 9 o'clock position, around two seconds.

If you find the sensor is going off for trivial reasons (e.g. curtains flapping slightly) then turn up Sx (twist clockwise). If it's not going off then turn Sx down (twist anti-clockwise).

Installation

Now we will install the Pibrella library for the Python programming language. From a terminal, do:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-pip
sudo pip install pibrella

Basic first program

We're going to create a very simple program that just tests that everything is working. From a terminal, do nano goblin-test.py and then create the following program:

import pibrella
import time

pibrella.output.e.on()

while True:
  if pibrella.input.a.read():
    print "I saw you move!"
  else:
    print "No movement."
  time.sleep(0.25)

Exit and save with CTRL-X . Now run the program with: sudo python goblin-test.py

The screen should fill with "No movement". When it detects movement it will change to "I saw you move" for a second or so, then go back to "No movement". End the program with CTRL-C .

You can download the goblin-test.py program here (right-click and Save As).

The proper program

Now we're going to create the proper program. From a terminal, do nano goblin.py and create the following program:

import pibrella
import time

pibrella.output.e.on()

is_button_pressed=False

def button_pressed(pin):
  global is_button_pressed
  is_button_pressed=True

pibrella.button.pressed(button_pressed)

while True:
  print "I'm waiting..."
  pibrella.light.yellow.blink(0.5,0.5)
  time.sleep(5)
  pibrella.light.off()
  pibrella.light.green.on()

  print "I'm looking for movement..."
  while not pibrella.input.a.read():
    time.sleep(0.25)

  print "I saw you move!"
  pibrella.light.off()
  pibrella.light.red.blink(0.1,0.1)
  pibrella.buzzer.fail()
  is_button_pressed=False

  while not is_button_pressed:
    time.sleep(0.25)

  print "You pressed the button, so I'll reset now."

  pibrella.light.off()
  pibrella.buzzer.off()

Exit and save with CTRL-X . Now run the program with: sudo python goblin.py

You can download the goblin.py program here (right-click and Save As).

The yellow light should flash for 5 seconds, during which time you can get yourself settled and and stay still, or leave the room. After that, then green light will come on. If it detects movement when the green light is on, it will sound the buzzer and flash the red light. After the buzzer sounds, you can reset it by pressing the Pibrella's button. Once done, end the program with CTRL-C .

Congratulations! May your bedroom be free of goblins forever.

Next Steps

What else could you do?

  • Take a photo with the Raspberry Pi Camera when movement is detected - for evidence, or maybe a nature watch project
  • Attach some more powerful speakers and play an .mp3 file loudly
  • Send an email to a smartphone when movement is detected (maybe email a photo?)
  • Use speakers and a speech synthesis module to warn intruders
  • More motion detection ideas

Without the Pibrella

You can connect a motion sensor directly to the Raspberry Pi's GPIO pins without a Pibrella. You will need female-to-female jumper cables.

Public Domain - Andrew Oakley - 2014-09-19

Top - More Computing Articles - Article Index - aoakley.com