BBC micro:bit

My son, like many Year 7 students, received his BBC micro:bit this week, a tiny little PCB with a surprising amount of functionality, intended to get kids coding.


There’s four main languages that can be used to create code, Code Kingdoms Javascript (a drag and drop editor), Block editor (a Scratch-like visual drag and drop editor), Touch develop (a script based language with a touch interface for mobile devices) and MicroPython, a completely text-based editing environment using python, that can be coded in a web browser and downloaded to the micro:bit.

I like the idea behind it, it’s much more self-contained than a Raspberry Pi and has a 5×5 array of LED’s that can be used to display scrolling messages or anything else you can get within the confines of 25 LED’s. Kids can get a ‘Hello World’ style app up and running in seconds.

My son has jumped straight into the Python side of things, which makes me very happy as it’s closest to the sort of coding he might end up doing on a PC or other device.

I’ve been having a little play with it today and we’ve coded up a quick ‘spirit level’ style app that shows if the micro:bit is level or displays an arrow to show which way it’s tilting, using its inbuilt accelerometer. You can change the sensitivity of the unit, by changing the sensitivity variable; setting it to very low values will require a very steady hand to hold it level. It seems that the values from the accelerometer are scaled -1024 to 1024 from my brief experiments.

Anyway, thought I’d stick the code up here in case anyone fancies trying something a bit beyond the ‘Hello World’ code with their child.

from microbit import *

#Program to read accelerometer and show tilt direction

#Sensitivity of readings (lower value to make more sensitive)
#Suggested range 20 - 256
sensitivity = 20

#Create image for when micro:bit is level
#a simple cross with a bright centre
#each number = led brightness 0-9
centred = Image("00400:"

#Main loop
while True:
	#Get accelerometer x and y values
    tilt_x = accelerometer.get_x()
    tilt_y = accelerometer.get_y()
#Check if micro:bit is tilted
    #If tilted forward and level in x
    if tilt_y < -sensitivity and (-sensitivity < tilt_x < sensitivity):

    #If tilted forward and right
    elif tilt_x > sensitivity and tilt_y < -sensitivity:

    #If tilted right and level in y
    elif tilt_x > sensitivity and (-sensitivity < tilt_y < sensitivity):

    #If tilted back and to the right
    elif tilt_x > sensitivity and tilt_y >sensitivity:
    #If tilted back, but level in x
    elif tilt_y > sensitivity and (-sensitivity < tilt_x < sensitivity):
    #If tilted back and to the left
    elif tilt_x < -sensitivity and tilt_y > sensitivity:
    #If tilted left, but level in y
    elif tilt_x < -sensitivity and (-sensitivity < tilt_y < sensitivity):
    #If tilted forward and left
    elif tilt_x < -sensitivity and tilt_y < -sensitivity:
        #If level
This entry was posted in Coding, Gadgets, micro:bit. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.