Skip to content


Tilt alarm

Advanced | MakeCode, Python | Accelerometer, Radio | Communication, Forces, Functions, Sensors

Step 1: Make it

What is it?

Keep your precious things safe with this radio-controlled burglar alarm.

These two videos show you what you'll make and how to code it:


Coding guide

How it works

  • When the micro:bit is shaken, the accelerometer detects movement and it shows an angry face on the LED display and plays an alarm sound.
  • It also sends a ‘thief!’ radio message, so that another micro:bit can warn you if someone is moving your valuable object.
  • If you have two micro:bits, flash this program onto both. Put one in, or on, a valuable thing you want to protect and keep the other near you.
  • As we want to show the angry face and play the ‘BADDY’ tune on both the movement sensor micro:bit and the alarm, this program uses a function (or procedure) called alarm.
  • Procedures and functions are really useful for sections of code we want to use at different points in a program. It saves duplicating code and makes the program more compact and efficient.

What you need

  • 2 micro:bits
  • a battery pack
  • something precious to keep safe
  • headphones, buzzers or powered speakers and two crocodile clip leads to attach them if using a V1 micro:bit

Step 2: Code it

1from microbit import *
2import radio
3import music
7def alarm():
11while True:
12    message = radio.receive()
13    if message:
14        alarm()
15    if accelerometer.was_gesture('shake'):
16        radio.send('thief!')
17        alarm()

Step 3: Improve it

  • What else could you protect with an alarm like this? How could it help protect trees?
  • Make it more sensitive using accelerometer readings or other gestures.
  • Split the program in two, so you have different programs on the sensor on your valuable thing and on the alarm you keep with you.
  • Send different messages and show different pictures for different alarm sensors on different valuable objects.