Built-in Sensors on BBC micro:bit
Built-in Sensors on BBC micro:bit
Read the accelerometer, compass, temperature, and microphone.
Built-in Sensors Overview
The micro:bit V2 has an impressive suite of built-in sensors ߀” no external wiring required:
| Sensor | MicroPython object | What it measures |
|---|---|---|
| Accelerometer | accelerometer |
Tilt, shake, acceleration on X/Y/Z |
| Magnetometer (compass) | compass |
Magnetic heading (0߀“360?°) |
| Temperature | temperature() |
Approximate ambient temperature via CPU die |
| Microphone | microphone |
Sound level, loud/quiet events (V2 only) |
| Touch logo | pin_logo |
Capacitive touch (V2 only) |
Accelerometer
Reads acceleration in milli-g (1000 = 1g = 9.8 m/s?²):
from microbit import *
while True:
x = accelerometer.get_x() # Left/right tilt
y = accelerometer.get_y() # Forward/back tilt
z = accelerometer.get_z() # Up/down (gravity ߉? -1024 when flat)
print(x, y, z)
sleep(200)
Tilt Detection
while True:
x = accelerometer.get_x()
if x > 300:
display.show("R") # Tilted right
elif x < -300:
display.show("L") # Tilted left
else:
display.show("-")
sleep(100)
Gesture Detection
The micro:bit firmware recognises common physical gestures:
while True:
gesture = accelerometer.current_gesture()
if gesture == "shake":
display.show(Image.SKULL)
elif gesture == "face up":
display.show(Image.HAPPY)
elif gesture == "face down":
display.show(Image.SAD)
Available gestures: "shake", "up", "down", "left", "right", "face up", "face down", "freefall", "2g", "3g", "6g", "8g"
Compass
The magnetometer measures Earth's magnetic field. You must calibrate it first ߀” the micro:bit displays a mini-game asking you to tilt the board to fill all LEDs.
from microbit import *
compass.calibrate() # Runs interactive calibration
while True:
heading = compass.heading() # 0߀“360 degrees (0 = North)
print("Heading:", heading)
sleep(500)
Compass Direction Display
directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
idx = int((compass.heading() + 22.5) / 45) % 8
display.show(directions[idx])
Temperature
The temperature is read from the CPU die ߀” it tends to read slightly higher than ambient:
temp = temperature() # Returns degrees Celsius as integer
display.scroll(str(temp) + "C")
Microphone (V2 only)
from microbit import *
while True:
if microphone.was_event(SoundEvent.LOUD):
display.show(Image.SURPRISED)
sleep(1000)
display.clear()
if microphone.was_event(SoundEvent.QUIET):
display.show(Image.ASLEEP)
sleep(1000)
display.clear()
Sound level as a number (0߀“255):
level = microphone.sound_level()
display.show(level // 30) # Map to 0߀“8 for rough display
Speaker (V2 only)
from microbit import *
import music
music.play(music.RINGTONE) # Built-in tune
music.play(["C4:4", "E4:4", "G4:4"]) # Note list
Notes format: "NoteOctave:Duration" ߀” e.g. "C4:4" = C in octave 4 for 4 ticks.
Combining Sensors
from microbit import *
while True:
if accelerometer.was_gesture("shake"):
display.scroll(str(temperature()) + "C")
if button_a.was_pressed():
compass.calibrate()
sleep(100)