Skip to content

Activity

Simple tilt alarm

Beginner | MakeCode, Python | Accelerometer, LED display, Sound | Input/output, Sensors

Step 1: Make it

What is it?

Make an alarm that lights up and plays a tune when your micro:bit is moved.

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

Introduction

Coding guide

How it works

  • In this project you'll make an alarm that goes off when you move your micro:bit. You could hide it inside or place on top of something precious, or put it in a bag and use it as a reminder to check you have everything you need when you go out.
  • Your micro:bit contains an accelerometer which measures forces. The accelerometer detects a shake gesture and shows a pattern on the LED display.
  • It also plays a warning melody. To hear the melody, use the MakeCode simulator or attach headphones to pin 0 and GND. You can also use an amplified speaker.
  • Press the reset button on the back of your micro:bit to clear the screen and make it ready for using again.

What you need

  • a micro:bit
  • MakeCode or Python online code editor
  • battery pack
  • optional speaker and crocodile clips

Step 2: Code it

1from microbit import *
2import music
3
4while True:
5    if accelerometer.was_gesture('shake'):
6        display.show(Image.ANGRY)
7        music.play(["G4:4", "A4", "B4", "B4", "B4", "A4", "G4", "F4"])
8        

Step 3: Improve it

  • Change the face icon to your own picture, using the 'show leds' block in MakeCode or the Image instruction in Python.
  • Replace the melody with your own creation or a tune you love.
  • Make it more sensitive using direct readings from the accelerometer instead of the shake gesture.