Using a light sensor with BBC micro:bit and MicroPython

A light sensor is a small component with a particular characteristic: it is basically a resistor and its resistance decreases if the light is more intense. To use it with micro:bit we need to use one of the analogic ports. To build this circuit you will need a breadboard, 3 jumper wires, a 10k resistance and possibly a Kitronik breadboard kit.

The project

I wanted to realise a simple project where, depending on the light intensity captured by the light sensor, the micro:bit shows an image of the Sun if the light is intense and an image of the Moon if the light is less intense.

Here is the complete circuit scheme:


“Image Copyright © Kitronik”


and here is a picture of the finished project I created:


The source code I needed is available here:

and as a demo I realised this small video:

Prototyping BBC micro:bit projects with Kitronik breadboard kit

BBC micro:bit has a few IO pins that can be used to interact with external devices. The problem with the board is that it’s not easy to connect the classic jumper wires (those that we normally connect to a breadboard) to the micro:bit, unless using a crocodile clip and being limited to just 3 pins.

Kitronik breadboard kit solves this problem, offering an interface where the micro:bit can be plugged and all the pins are easily connectable to the breadboard using normal male/female jumper wires.

I’ve built a very simple circuit following an example you can find on this manual


To build the circuit you also need 4 male/female jumper wires and two buttons. All this circuit does is to connect the buttons to the micro:bit pins that relate to those buttons. Basically pressing those buttons is the same as pressing button A or button B on the micro:bit board. Here you can see the schema in detail:

Screenshot 2016-02-07 14.30.36

“Image Copyright © Kitronik”

I’ve also made a short video so that you can see it in action:

And of course the source code is available too:

Using BBC MicroBit accelerometer with Python

In these days I’m having a bit of fun with BBC MicroBit board and I’m learning how to use the different sensors available. The latest one I wanted to try was the accelerometer. The board can “sense” if you are moving it in any of the 3 dimensional axes: X, Y, Z. According to the documentation there are four methods available that can be used to get these values: microbit.accelerometer.get_values() will return you a tuple with all the 3 values, while microbit.accelerometer.get_x()microbit.accelerometer.get_y()microbit.accelerometer.get_z() will give you the single values.

The documentation on the official website doesn’t explain much and for example I didn’t even know what was the range of the values I can get back from these methods (by the way it’s between -1024 and 1024), so I decided to play with the code directly and write a very simple example. The small example I wrote, shows a smile on the board display if you keep it straight and shows a sad face if you bend it. This is the result:

and this is all the needed code of the application:

In the next days I will try to play with more sensors and to publish other examples.

Using Python PyPy in a virtual environment

Sometimes we need to test if our code also works with PyPy implementation of Python. Assuming you have already installed it in your system, first find out where it is installed:

then you need mkvirtualenv to create a virtual environment that will use this runtime:

That’s it! You can now use this virtual environment to run your Python application using PyPy environment.

Getting started with BBC MicroBit and Python

A few days ago I had the great opportunity to attend an event organised in collaboration with Python Software Foundation, a few primary school teachers and hosted by Computing at School, in London. The meeting was organised by Yvonne Walker (from CAS) and Nicholas Tollervey (PSF). The aim of the meeting was for teachers and developers to meet and discuss the opportunities offered by MicroPython on the BBC micro:bit. During the event a BBC micro:bit board was loaned to each person for the purpose of developing Python scripts, MicroPython itself or educational resources for the BBC micro:bit. Nicholas made it very clear that there is an NDA in place until the device is delivered to the kids and explained what we could or couldn’t do.


The Board


The board is a 4 x 5 cm device with an ARM Cortex-M0 processor, accelerometer and magnetometer sensors, Bluetooth and USB connectivity, a display consisting of 25 LEDs, two programmable buttons, and can be powered by either USB or an external battery pack (source: ).

Flashing the firmware

Once you get a new board, it probably doesn’t have a proper firmware and application flashed. I suggest you to download the Python MicroBit REPL from this repository:
All you need to do is to connect the board to your computer, using a micro-USB cable. The device will be mounted as a volume. At this point, drag & drop the file called firmware.hex into the mounted volume. The firmware will be flashed and during the operation you will see a yellow led flashing.

Using MicroPython micro:bit REPL

To start writing some Python code on micro:bit you first need to clone this repository

once you have cloned the repository, you need to install the Python dependencies (I suggest you to do it from inside a virtualenv)

start the MicroPython REPL

and the Python shell will open, so you can start writing commands, like this one

BBC micro:bit MicroPython Editor

Typing all the Python commands directly into the shell can be a bit difficult. You can use a very nice and dedicated editor to write code and produce the compiled application for the micro:bit. All you need to do is clone this repository

Open the file named editor.html with your browser and start writing your code. When your code is done, you can generate the .hex file clicking on Download button. To load the compiled application you just need to drag & drop the .hex file to the mounted device, exactly like you did the first time to flash it. If you need a reference for all the methods and libraries available, you can consult the official documentation here