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.



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:

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.


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:

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).


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 and then create the following program:

import pibrella
import time


while True:
    print "I saw you move!"
    print "No movement."

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

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 program here (right-click and Save As).

The proper program

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

import pibrella
import time



def button_pressed(pin):
  global is_button_pressed


while True:
  print "I'm waiting..."

  print "I'm looking for movement..."
  while not

  print "I saw you move!",0.1)

  while not is_button_pressed:

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

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

You can download the 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?

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 -