Wikijunior:Raspberry Pi/Raspberry Pi LEGO® Burglar Alarm

Tutorial by Andrew Oakley Public Domain 2016-04-24 updated 2018-03-01 www.cotswoldjam.org

LEGO® is a trademark of the LEGO Group of companies which does not sponsor, authorize or endorse this tutorial nor Cotswold Raspberry Jam.

Getting started
This tutorial shows you how to make a burglar alarm that sounds when a LEGO® door is opened.

At the end of each line, you will usually have to press the Enter key. Your tutor should have prepared your Raspberry Pi for you and given you a bag of components. If not, see the section "Preparation" at the end.

Piezo buzzer
A piezo buzzer makes a beep when electricity goes through it.

It has two "leg" connectors; one long and one short.


 * The long leg must be connected to the positive
 * The short leg must be connected to the ground (negative).

Reed switch
A reed switch is a glass tube with two bits of metal separated by a tiny gap. When there is a magnet nearby, the two bits of metal touch and allow electricity through. A reed switch can be connected positive or negative on either side.

Jumper wires
Jumper wires (also called DuPont wires) are used to connect electronic components to the Raspberry Pi or a breadboard.

You should also have four female-to-female jumper wires.

A small magnet and a LEGO® door
A small magnet and a LEGO® door. Your tutor should have already stuck the reed switch to the inside top of the door frame with sticky tape and stuck the magnet to the inside top of the door.

If you have a burglar alarm at home, get a step-stool and look at the top of the door. I bet you have some white plastic bits – one on the door frame, one on the top of the door. These contain a reed switch and a magnet, just like this tutorial.

The circuit


The Raspberry Pi uses two sets of numbers to refer to the General Purpose Input Output pins (GPIO pins).

"Board numbering" just starts from the bottom-left at 1, and works its way up and right, through to 40.

"BCM numbering" (Broadcom numbering) is the way the Raspberry Pi's processor sees the pin connections.

We're going to program with GPIO Zero, which uses BCM numbering.


 * 1) Connect the reed switch to GPIO pin 9 (GND) and pin 7 (GPIO 4).
 * 2) Connect the short leg of the piezo buzzer to GPIO pin 14 (GND) and connect the long leg to GPIO pin 16 (GPIO 23).

Detecting the door opening
Power up your Raspberry Pi and go to the desktop.

From the menu, select Programming - Python 3. Then use File, New Window to create a new program.

Type in the following program, or alternatively you can use File, Open to open the  program in the python/burglar folder.

Use File, Save to save this program as burglar1.py and the run it by selecting Run menu, Run Module.

Close the door and then open it. You should see the words "Door opened" on the screen.

Not working? Check you’ve got:


 * Wires going to correct GPIO pins.
 * The reed switch is aligned with the magnet. Rotate it gently so that the contacts are pulled together by the magnet.
 * The reed switch hasn't got broken (ask your tutor for another if so).
 * No spelling mistakes, no missing brackets, and you’ve used round brackets instead of pointy, curly or square ones.

What is this program doing?
The first line tells the computer to learn about a new thing. Computers can learn from programs that other people have written, and we call these other programs "libraries". Our instructions tell the computer to learn about buttons, which can either be pressed or released.

A reed switch is a special type of button. It is "normally open" (released) and no electricity goes through it. When a magnet is nearby, it becomes closed (like a pressed button) and electricity flows from the GPIO4 pin to the ground pin.

This causes the program to wait until the reed switch is newly opened – it has to change from closed to open. If the door was already open at the start of the program, that doesn't count.

This prints a message to the screen.

Looping the program
Let's add a loop so that we can keep opening and closing the door.

Alternatively, you can load in the  program if you don't want to type it in.

The  command repeats a section of the program; the word   makes it repeat forever.

Indentation matters in Python. For example, you must type the two spaces in the lines after. Indentations like these define a block of code.

Python is picky about punctuation and capitals, too. The initial letter of  must be a capital, and the line   must end with a colon.

Use File, Save to save this program as burglar.py and then run it by selecting Run menu, Run Module. Try opening and closing the door.

Halt the program by selecting Shell menu, Restart shell.

The buzzer
Change the program to add in the buzzer. Alternatively, load the  file.



Note that we have imported the Buzzer object from the library, as well as the Button.

Next, we set the buzzer to work on GPIO23.

When the door has been opened, we use:

This command has three numbers, called parameters:


 * 1) The number of seconds to sound the buzzer (1)
 * 2) The number of seconds to keep the buzzer quiet (0, zero)
 * 3) The number of times to loop through sound and quiet periods (1, once only)

will give us a single 1-second beep. You could change it to sound really annoying!

Use File, Save to save this program as burglar3.py and then run it by selecting Run menu, Run Module.

The buzzer should now sound for one second when you open the door.

Halt the program by selecting the Shell menu, Restart shell.

Preparation
Download the sample program files from: http://www.cotswoldjam.org/downloads/2016-04

Glass reed switches break easily. Hold the legs while bending them, don't rely on the glass holding them. Use Sellotape® to ensure any shards do not escape if the switch breaks.

Cjam-burglar-tutorial-2018.pdf
The original PDF for this tutorial is available on Wikicommons: