A Raspberry Pi (Figure 1) is a tiny computer that you can use to write programs and build and control your own electronic circuits, like the motion alarm in this project. The circuit uses sensors to gather information from the world around it. You can use information from those sensors to tell your computer program what to do. Many electronic devices we use every day, like phones and video game controllers, use sensors in the same way.

raspberry pi 3B+

Building an alarm 

  • Raspberry Pi Projects Kit (if you do not already own a Raspberry Pi) it comes with:
    • Raspberry Pi model 3B+
    • Micro-USB power cable
    • HDMI cable*
    • Clear plastic case for Raspberry Pi
    • All the circuit components to do it all
    • 16GB SD card preloaded with Raspbian and quick link to Science Buddies instructions
  • Raspberry Pi Circuit Parts Only Kit comes with:
    • All the circuit components to do it all
    • 16GB SD card preloaded with Raspbian and quick links to Science Buddies instructions

To set up your Raspberry Pi, you will also need the following parts (not included in either kit option):

  • TV or computer monitor with HDMI input*
  • USB keyboard and mouse

*If you have a TV or monitor with a DVI or VGA input, you will need an adapter. If your monitor does not have built-in speakers, you will need external speakers or headphones. See FAQ for more information.

To do this specific project, you will need the following circuit parts from your kit:

  • Male-male jumper wires (3)
  • Motion sensor

Getting Started

Have you ever wished that you could set off an alarm when someone comes into you room? Or, what if you want to throw a surprise party for someone, flashing lights and playing sound when they walk in? This project will let you do just that when you connect a motion sensor to your Raspberry Pi. If you have not already, you will need to set up your Raspberry Pi before you begin. Watch this video for a short demonstration of the project:

Writing Your Program

If you have never written a program in Scratch before, watch this video to learn how.

This program will monitor an input pin to determine when the motion sensor is activated. Then, it will play a sound and display an on-screen message for whoever is near your Raspberry Pi! See if you can create the following program in Scratch:

raspberry pi 3.1 scratch program
Figure 2

Here are a couple helpful tips for making the program. The blocks are color-coded, which makes them easier to find under the correct menu:

  • The when (green flag icon) clicked is available under Events.
  • To create a variable, click Data then Make a variable.
  • The wait __ secsforever, and if __ then blocks are all available under Control.
  • The not __ block can be found under Operators. Along with an if block, you can use this block to check if a certain condition is not true.
  • The gpio __ is high? block is available under More blocks. If you do not see this block, click Add an extension, then click Pi GPIO and select OK.
  • The play sound __ block is availale under Sound. You can import additional sounds on the Sounds tab.
  • The say ___ for __ secs block can be found under Looks. This will make the on-screen sprite show a word bubble with text. You can type whatever you want in the text box to change the text, so you can change the message depending on whether you want to use the Raspberry Pi as an alarm, or to throw a surprise party!
A cat sprite from the program Scratch with the message
Figure 3
A cat sprite from the program Scratch with the message
Figure 4

Create your own program with customized sound effects and a message, and remember to save it when you are done!

Building Your Circuit

To build your circuit, you will need to use a breadboard. If you have not used a breadboard before, watch this video before you continue

To build your circuit, you will need three M-M jumper wires and your motion sensor.

M-M jumper wires
Figure 5
A PIR sensor
Figure 6

First, plug three jumper wires into your breadboard, as shown Figure 7.

Part Picture Hole
Black M-M jumper wire black M-M jumper A7
(Pi Wedge G4)
Red M-M jumper wire red M-M jumper A15
(Pi Wedge 5V)
Yellow M-M jumper wire yellow MM jumper A17
(Pi Wedge GND)
raspberry pi PIR breadboard jumpers
Figure 7
raspberry pi PIR breadboard jumpers photo
Figure 8

Next, plug the free ends of the jumper wires into the holes at the end of the motion sensor’s cable, as shown in Figure 9. Note that there is no white jumper wire, so plug the yellow wire in so it matches up with the white part of the cable, as shown in Figure 9.

raspberry pi PIR cable
Figure 9
raspberry pi PIR breadboard
Figure 10
raspberry pi PIR circuit
Figure 11

icon learn

Learn More About:  How does the circuit work? 


Testing Your Alarm

Now, position your motion sensor so the plastic dome is facing outward into the room (not down onto the tabletop). Run your program by clicking the green flag of the Scratch window. Try waving your arms around in front of the sensor. Your sound effect should play! Try experimenting with different sounds, and changing the “delay” variable to a number that works best for you (if the number is too small, the alarm might go off repeatedly). When you are ready, leave the program running and set up the sensor so it is aimed at a door to surprise the next person who walks in the room!

icon trouble

Troubleshooting:  My alarm does not work


Going Further

If you are having fun with your alarm, here are a few more things you can try:

  • Add an LED to your circuit and make the LED flash when the alarm is triggered. Use the set gpio __ to ____ block under More blocks to control the LED.
  • Create a new variable called “count.” When your program first starts, use a set count to __ block to reset count to zero. Then, use a change count by __ block to increase the count by 1 each time the alarm is triggered. This way, if you have to leave the room for a long period of time, when you get back you can see how many times the alarm went off!
raspberry pi PIR breadboard LED
Figure 12