Micro:bit Extensions: Add extra sensors and devices

I have been using the BBC Micro:bit modules (~$20) with our kids to teach them basic hardware and software. The platform offers a block programming interface (very much like Scratch) and Python programming. The interesting thing is that the interface will toggle seamlessly between the two programming languages.

To add functionality to your projects Micro:bit supports extensions. This feature allows Arduino and Raspberry Pi sensors and devices to be connected to the basic module.

In this blog I wanted to:

  • Show a extensions example with deviices that are not directly available in the basic list
  • Comment on some limitations
  • Document how to add Micro:bit parts in Fritzing wire drawing tool

An Extension Example

Use the Extension menu item to add a new set of functionality to your Micro:bit’s project.

For this example I wanted to use some devices/sensors that I was using on my Arduino projects. These devices included:

It is important to note that the extension may not be readily available from the Microbit web page. For my project I did an Internet search to find the required github links. Once you have the URL it can be pasted into the Extension’s page:

Below is a picture of the Micro:bit with the three added devices

The Micro:bit logic used an on_start block to setup the pins for the TM1637 4-digit display, and initialize the OLED display.

The forever block:

  • queried the DHT11 sensor (on Pin 0)
  • showed the humidity on the Micro:bit display
  • showed the temperature on the TM1637 display
  • showed both the temperature and humidity on the 0.91″ OLED
  • cycled every 5 seconds

The block code can be viewed (or edited)in Python:

"""

Devices:

DHT11 Temperature/Humidity Sensor 

TM1637 4-Digit Display 

I2C 9.91" OLED Display

Show Temperature on TM1637

Show Humidity on Microbit screen

Show both Temperature and Humidity on the the OLED

"""
tm = TM1637.create(DigitalPin.P13, DigitalPin.P14, 7, 4)
MuseOLED.init()

def on_forever():
    dht11_dht22.query_data(DHTtype.DHT11, DigitalPin.P0, True, False, True)
    basic.show_string("H: " + str(dht11_dht22.read_data(dataType.HUMIDITY)) + " %")
    tm.show_number(dht11_dht22.read_data(dataType.TEMPERATURE))
    MuseOLED.clear()
    MuseOLED.write_string("Temperature: " + str(dht11_dht22.read_data(dataType.TEMPERATURE)) + " C")
    MuseOLED.new_line()
    MuseOLED.write_string("Humidity: " + str(dht11_dht22.read_data(dataType.HUMIDITY)) + " %")
    basic.pause(5000)
basic.forever(on_forever)

Limitations

Some of the limitations that I found were:

  • Not all Arduino sensors and devices were supported
  • Not all Arduino functionality is available with Micro:Bit. For example fonts on OLED devices.
  • Finding the correct extension can be tricky. For example searching 0.91 OLED doesn’t return any hits.
  • Some devices were supported in software, however they required 5V. A good example of this is the 2×16 LCD display

Documenting Wiring in Fritzing

Fritzing is an excellent free tool for wiring drawings (Note: for some platforms a donation might be required).

To add some Micro:bit parts to Fritzing see:

Once a parts file is downloaded it is imported into a “My Parts” grouping.

Summary

By adding extension you can greatly extend the usability of Micro:bits.

I found that for many simple projects block programming was quicker to create than Python, but it nice that the Python code gets autogenerated.

Micro:bits and Node-Red

BBC Micro Bit, (micro:bit) is an open source hardware ARM-based embedded system designed by the BBC for use in computer education in the UK. The device is half the size of a credit card and has an ARM Cortex-M0 processor, accelerometer and magnetometer sensors, Bluetooth and USB connectivity, a display consisting of 25 LEDs, and two programmable button.

Depending on where you purchase it the price ranges between $15-$20. So this is a very attractive module for the beginning programmer.

The micro:bit module has 2 buttons to interface to it and a small 5×5 LED screen. This is good for small tests but its a little limiting.

For the most part micro:bit is a standalone unit so in this blog I wanted to show how to put micro:bits information on to a Node-Red web dashboard that could be viewed from a smart phone, tablet or PC.

mp_nr_overview

Micro:bits Setup

The micro:bits has a USB connection that can be used for communications to PCs or Raspberry Pi’s. For my setup I used a Raspberry Pi Zero W, with a microUSB-to-USB adapter to connect into the micro:bit.

mp_pi_setup

The micro:bit can be programmed via a nice Web Interface, for details see: https://microbit.org/guide/quick/. For this application I programmed with blocks.

My logic had the temperature and light sensor values written out ever 10 seconds, in the format of: T=xxx, L=xxx, I used a comma separator between the data pieces. Button presses were sent as either A=1, or B=1, .

mp_usb_logic

 

Node-Red Setup

Node-Red is pre-install on the Raspberry Pi image, if you want to use a PC instead see the Node-Red installation documentation.

A Node-Red has a Serial port component (https://flows.nodered.org/node/node-red-node-serialport) that can be loaded manually or via the Palette Manager.

The first step is to insert a serial input node and define the serial interface. Double-click on the serial input node and edit the serial connection. The interface will vary with your setup but node-red will show a list of possible USB ports. The default baud rate of the micro:bits USB port is 115200. I used a timeout of 200ms to get the messages, but you could also look for a terminating character (the comma “,” could be used).

nr_serial_edit

The logic used 4 Javascript function nodes to parse the micro:bit message

nr_serial_logic

“Get Temp Value” Function:


// Pull out the temperature
//
var themsg = msg.payload;

if (themsg.indexOf("T=") > -1) {

var msgitems = themsg.split(",");

var temp = msgitems[0];
temp = temp.substring(2,4)
msg.payload = temp;
return msg;
}

“Get Light Value” Function:

// Pull out the Light Sensor Value
//
var themsg = msg.payload;</pre>
if (themsg.indexOf("T=") &gt; -1) {

var msgitems = themsg.split(",");

var light = msgitems[1];

light = light.substring(2,5)

msg.payload = light;

return msg;

}

“Check Button A” Function:

// If the message is Button A pressed
// "A=1,"
if (msg.payload == "A=1,") {
msg.payload =  1;
return msg;
}

“Check Button B” Function:

// If the message is Button B pressed
// "B=1,"
if (msg.payload == "B=1,") {
msg.payload =  1;
return msg;
}

Chart nodes are used to show the results. (Note: you’ll need to create a dashboard name).

For the button presses a 1-0 transition is needed after a button press, otherwise the chart will always show a value of 1. The 0-1 transition is done using a trigger node.

The final web dashboard is available at: http://your_node_red_ip:1880/UI.

mp_screen

Final Comments

The next step will be to add the ability to have Node-Red write values to the micro:bit. This would be done with the Node-Red serial output node. Micro:bit’s have a serial read function that would then process the command.