Don’t Steal My Pig! (Burglar Alarm)

Build a burglar alarm that is activated with a crash sensor and PIR (Passive Infrared) sensor, producing warning music from a buzzer and animation on the LED display of the micro:bit.

Create a model of a pig pen enclosure.

You'll need

  • 1 x Breakout board
  • 1 x Buzzer
  • 1 x Crash sensor
  • 1 x Motion sensor
  • 2 x Jumper Wires (female-female)
  • Cardboard (ideally from packing boxes)
  • Ice-cream sticks
  • Masking tape
  • 1 x Small piece of paper

Step 1


Insert the micro:bit into the breakout board. There is no difference between using the black or white colour breakout board.


• 1 x Breakout board
• 1 x micro:bit

Step 2


Connect the Octopus buzzer to P0, motion sensor to P1 and the crash sensor to P2.

Ensure the following points:
• Bumpy end of the connector goes to the sensor
• Flat end goes to the breakout board
• Make sure the wire colours match up to the rows of pins

(If you don’t have an Octopus buzzer, see the next step)


1 x Buzzer
1 x Motion sensor
1 x Crash sensor

Step 3


If you don’t have an Octopus buzzer (instead you have the black buzzer with 2 straight pins sticking out), you need to connect jumper wires instead.

The colour of the jumper wire NEVER matters. A jumper wire is just a connector.

But the colour of the pin on the breakout board does.

The positive leg of the buzzer (in this example, the green jumper wire) goes to the yellow pin.

The negative leg (yellow jumper wire) goes to the black pin.


• 2 x female to female jumper wires
• 1 x Buzzer

Step 4


You'll need to download an extension to allow you to use the components in the Tinker kit.

Open the "Advanced" code drawer and select "Extensions", then search for Tinkercademy (or just "Tinker")

Step 5


Under the "Logic" code drawer, choose the "if-else" block and drag it into the "forever" block.

You'll notice the block says "if true then" -- so in our case, if it is true that either the motion sensor or crash sensor gets activated, we can make our alarm sound.

We also want our code to constantly be running to check for these conditions, which is why we put it in the forever block.

Step 6


Open the “Tinkercademy” and “Logic” code drawers and drag out the crash sensor, motion detector (sensor), and the Boolean codes “not” and “or”.

Place the “setup crash sensor” inside the “on start” block. This prepares (or initializes) the sensor to be used whenever the program begins to run.

Notice the other blocks that are shaded out? This means they will not run in the program until we arrange them correctly.

Step 7


Remember, we want to think about what condition can cause the alarm to activate.

Place “crash sensor pressed” inside the “not” code block. We want to create the situation that when the gate of the pig pen is closed, the crash sensor’s metal switch will be held in the closed position (or remain pressed). Thus, opening the gate (the condition that should activate the alarm) should cause the sensor to NOT be in the closed position.

We express this in our code by using the Boolean value "not" to reverse the expression so that "crash sensor pressed" no longer remains true.

Step 8


Though you could use 2 separate "if" statements to accomplish our task, we can be more efficient. So let's check if either conditions are true at the same time by placing them on either side of the "or" block.

Step 9


Final code:
Use the Basic and Music code drawer to get "show icon" and select the cross icon, as well as the "start melody" block, choosing "baddy" as the tune to play through the buzzer. Place these codes below the "if" block. Don't forget to add the "pause" block from the Basic drawer to give sufficient time for the melody to play through.

Now when we activate the alarm, we can cause a melody to play and our LED display to show a cross. But when the gate is closed or no motion is detected, we don't want to see or hear anything. We'll just need "clear screen" from the Basic drawer (select "more" after opening Basic to see it).

Feel free to create your own image or animations using the block "show leds" in the Basic drawer, or creating your own music with the "play tone" blocks in the Music drawer too!

Step 10


We've finished the coding. Now we'll construct the pig pen and place the components in the correct areas.

Using a penknife and cutting board, cut out the shapes needed to create the right side wall. Bend one edge of it to make a hinge for the front of the gate.

If you use Cardboard Box, before bending it, score the first layer in a straight line (ie. don’t cut all the way through). This will produce a ready-made hinge for you.

Of course, you're free to use other thinner cardboard if needed as well.


• Corrugated fiberboard (Cardboard Box)
• Masking tape
• Penknife
• Cutting board

Step 11


The length of the left wall should be equal to the right wall, up to where the wall starts to bend because of the hinge.

Attach both side walls to a back wall. Place everything on a cardboard base.

Step 12


Attach the crash sensor on the side of the left wall, facing inside. Ensure that the edge of the sensor matches the front edge of the wall. The metal switch should come out beyond the edge.

Step 13


Build the gate with the ice-cream sticks. Use a vertical stick to anchor the horizontal sticks. Use masking tape to hold them in position. You may also want to apply white glue to strengthen the structure.


x4 ice-cream sticks

Step 14


This picture shows the left wall view from the outside of the enclosure.

Bend a small piece of cardboard to act as a hinge. Attach the gate to one side of the hinge while the other side attaches to the left wall.

Step 15


This picture shows the left wall view from the inside of the enclosure.

Insert 2 short ice-cream sticks (snap a regular size ice-cream stick into half) and stick them in between the black connector and the wall. This will prevent the crash sensor from getting pushed in by the closing of the gate.


x2 ice-cream sticks

Step 16


Remember the hinge that you created in Step 5? It allowed a small flap to be made to serve as part of the front of the gate.

Using that flap as a template, cut a piece of cardboard (as pictured) of the same height but twice the width (eg: 6cm x 8cm). Fold it in half lengthwise.

Make a slit in the middle of the cardboard that’s wide enough for an ice-cream stick to fit. Break off ¼ of the stick.


x1 ice-cream stick (as seen in the next step)

Step 17


You’ve created the latch that can hold the gate closed.

Step 18


Attach the latch system as follows.

Before securing it firmly, test by moving the stick up and down to check that it can allow the gate to open when raising the latch, and holding it closed when lowering the gateb.

Step 19


Connect your battery pack to the microbit (you can tape the battery pack behind the back wall),

Place the buzzer in the back left corner, and attach the wires to the sides of the wall.

Step 20


Attach the PIR sensor to an ice-cream stick such that it points down over the entrance of the gate.

Roll a strip of paper into a cylindrical cone and slip it over the plastic dome of the sensor.

This is to restrict the sensor to activate only if someone is trying to open the gate. Without the paper cover, it would also activate if a nearby object moves past.

Congrats! You've alar-mi-fied your pig pen!


• Small piece of paper
• Masking tape

Code and References

Completed this tutorial?

Spread the word! Let's get this trending on social media with #letsgethacking #Don’tStealMyPig!(BurglarAlarm)