A Smart Badge : BBC micro:bit & Micropython

The BBC micro:bit is a pocket-sized computer that was designed to introduce kids (of all ages) to how software and hardware work together. The BBC micro:bit has an LED light display, buttons, sensors and many input/output features that, when programmed, lets it interact with you and your world. The BBC Micro Bit is an open source hardware ARM-based embedded system designed by the BBC for use in computer education in the United Kingdom. The Micro:bit has become the heart of countless creative projects around the world and has helped build a trusted educational learning system.

BBC micro:bit kit

The BBC micro:bit can be purchased from your favorite brick and mortar electronic store including many online hobby electronic stores. Read more about the BBC micro:bit at www.microbit.org.

Front and Back of the BBC micro:bit (Image courtesy microbit.org)

At CoderDojo Altona North we’ve been working on creating new badges for each of the volunteers. I thought to myself, rather than going with a paper badge why not make use of the BBC micro:bit and program it in MicroPython. So i took on the challenge to pull together a very simple program that doesn’t just reads out my name but also then reads out values captured by the various onboard sensors, these include – temperature, light intensity and compass heading.

Check out the code below –

from microbit import *
#initializing images that we'll display later in the program
image1 = Image.HAPPY
image2 = Image.SILLY
image3 = Image.DUCK
image4 = Image.SNAKE
image5 = Image.SKULL
#used to calibrate the on-board compass
compass.calibrate()
#never ending loop
while True:
    display.scroll('Trevor')
    sleep(2000)
    display.show(image1)
    sleep(2000)
    temp = "The temp is {} DegC".format(temperature())
    display.scroll(temp)
    sleep(2000)
    display.show(image2)
    sleep(2000)
    lightlevel = "The light intensity is {}".format(display.read_light_level())
    display.scroll(lightlevel)
    sleep(2000)
    display.show(image3)
    sleep(2000)
    currentheading = "Your current heading is {} Deg".format(compass.heading())
    display.scroll(currentheading)
    sleep(2000)
    display.show(image4)
    sleep(2000)
    display.scroll('Trevor')
    sleep(2000)
    display.show(image5)
    sleep(2000)

Programming the BBC micro:bit is fairly easy. One can program the BBC micro:bit using blocks, using Javascript or even in MicroPython. The Micropython editor for the BBC micro:bit can be accessed here – https://python.microbit.org/v/beta. The code shown above is MicroPython.

Programming the BBC micro:bit in MicroPython

Feel free to download the code and make your own smart electronic badge. Let us know what modifications you made to your badge and how did things end up.

Update 07082022 – I updated the code, removed the compass heading functionality since it was causing issues, needing me to re-initialize the compass every few minutes. Here’s the updated code.

from microbit import *
#initializing images that we'll display later in the program
image1 = Image.HAPPY
image2 = Image.SILLY
image3 = Image.DUCK
image4 = Image.SNAKE
image5 = Image.SKULL
#used to calibrate the on-board compass
#compass.calibrate()
#never ending loop
while True:
    displaytext = "Name : Trevor || Temp is {} DegC ".format(temperature()) +" || Light intensity is {} ".format(display.read_light_level())
    display.scroll(displaytext)
    display.scroll("Welcome to the Raspberry Pi Makers Group")
    sleep(2000)
    display.show(image1)
    sleep(2000)
    displaytext = "Name : Trevor || Temp is {} DegC ".format(temperature()) +" || Light intensity is {} ".format(display.read_light_level())
    display.scroll(displaytext)
    display.scroll("Welcome to the Raspberry Pi Makers Group")
    sleep(2000)
    display.show(image2)
    sleep(2000)
    displaytext = "Name : Trevor || Temp is {} DegC ".format(temperature()) +" || Light intensity is {} ".format(display.read_light_level())
    display.scroll(displaytext)
    display.scroll("Welcome to the Raspberry Pi Makers Group")
    sleep(2000)
    display.show(image3)
    sleep(2000)
    displaytext = "Name : Trevor || Temp is {} DegC ".format(temperature()) +" || Light intensity is {} ".format(display.read_light_level())
    display.scroll(displaytext)
    display.scroll("Welcome to the Raspberry Pi Makers Group")
    sleep(2000)
    display.show(image4)
    sleep(2000)
    displaytext = "Name : Trevor || Temp is {} DegC ".format(temperature()) +" || Light intensity is {} ".format(display.read_light_level())
    display.scroll(displaytext)
    display.scroll("Welcome to the Raspberry Pi Makers Group")
    sleep(2000)
    display.show(image5)
    sleep(2000)

Please drop us a note at trevor at hack2 dot live if you have any comments or would like to chat.

Leave a Reply

Your email address will not be published.