Skip to content


Radio door alarm

Advanced | MakeCode, Python | Compass, LED display, Radio | Communication, Magnetism, Radio waves, Relational operators, Selection, Sensors

Step 1: Make it

What is it?

A wireless alarm to warn you when someone opens a door – or leaves it open.

a magnet on the edge of a door near a micro:bit on the door frame

How it works

  • Fix a magnet in the corner of a door and a micro:bit with the sensor program close to it on the door frame like in the picture.
  • The sensor program uses the micro:bit’s compass (magnetometer) input sensor and a loop to measure the strength of the magnetic field every 2 seconds. It uses selection so when it falls below a certain level (the threshold), it sends a radio signal ‘door open’. If the magnetism reading goes above the threshold, it sends ‘door closed'.
  • When you first use the compass sensor you have to calibrate it by playing a game to fill the screen with dots, like calibrating a mobile phone’s compass.
  • The alarm micro:bit displays a tick on its LED display output when it receives ‘door closed’. When it receives a ‘door open’ radio message it shows a cross and plays an audible alarm.

What you need

  • 2 micro:bits and at least 1 battery pack
  • a magnet
  • some blu-tak or similar to fix a magnet to a door and a micro:bit to a door frame
  • optional headphones, buzzer or speaker and crocodile clip leads to attach it

Step 2: Code it

Sensor / transmitter:

Alarm / receiver:

Step 3: Improve it

  • Press button A on the sensor to help calibrate the best threshold number for your magnet. It’s initially set to 100 microteslas in MakeCode which is the same as 100000 nanoteslas in Python.
  • Use multiple micro:bits to track the status of different doors by sending different radio messages, e.g. ‘back door open’.
  • Use a variable to measure how long doors are left open – could this help you save heating energy?