Skip to content


Spirit level

Intermédiaire | MakeCode, Python | Accéléromètre, Son, Écran LED | Capteurs, Logique booléenne, Opérateurs de comparaison, Sélection

Étape 1 : Fais-le

Qu'est-ce que c'est ?

Make a tool for making sure pictures, shelves or work surfaces are level. The new micro:bit's built-in speaker makes it easy to improve your spirit level with audio feedback.


Guide de codage

Ce que vous apprendrez

  • How to use the accelerometer sensor to measure angles
  • What 'pitch' and 'roll' are
  • How to combine sensor readings in two dimensions to find a level surface

Comment ça marche

diagram showing pitch and roll on the micro:bit
  • The micro:bit's accelerometer can measure angles of tilt in two directions: up and down (pitch) and side to side (roll). This project uses this to show when the micro:bit is level on the LED display and by making a sound, which could be useful when hanging a picture or making a work surface.
  • A loop keeps the program constantly measuring the micro:bit's angle.
  • If the accelerometer measures an angle of tilt between +5 and -5 degrees of both pitch and roll, the micro:bit must be reasonably level. It then shows a tick on the LED display output and plays a musical tone.
  • To hear the tone attach headphones or a speaker to pin 0 and GND, or if you have a new micro:bit you will hear it on the built-in speaker.
  • If either the pitch or the roll is outside the range +5 to -5 degrees, it shows a cross on the LED display and stops the sound.

Ce dont vous aurez besoin

  • Un micro:bit
  • MakeCode ou éditeur Python
  • un boîtier de piles (optionnel)

Étape 2 : Programme-le

Étape 3 : Améliore-le

  • Can you make the musical pitch change depending on the angle?
  • Could you code an LED 'bubble' that moves around the screen like a real spirit level?