MicroPython for BBC Micro.bit Technical Workshop
MicroPython for BBC Micro.bit Technical Workshop
Agus Kurniawan
This book was written to help anyone want to get started with BBC micro:bit
board using MicroPython. It describes the basic elements of the development of
BBC micro:bit board.
Agus Kurniawan
1.2.2 Fritzing
Store website: http://shop.fritzing.org/ .
You can buy Fritzing Starter Kit with Arduino UNO or Fritzing Starter Kit with
Arduino Mega.
I also used Arduino Sidekick Basic kit for electronic components. I have some
another electronics kit included sensor and actuator.
2. Set up MicroPython on BBC micro:bit Board
This chapter explains how to prepare BBC micro:bit board development for
MicroPython.
2.1 Getting Started
MicroPython is a lean and efficient implementation of the Python 3
programming language that includes a small subset of the Python standard
library and is optimised to run on microcontrollers and in constrained
environments. There are various IoT boards with supported MicroPython.
Further information about MicroPython, you can visit official website
on https://micropython.org.
2.2 Connect BBC micro:bit to Computer
You need a micro USB cable to connect BBC micro:bit board to computer
through this cable. After connected, you should see this board run as external
flash dis. You can see it in Figure below.
You can verify your board using Terminal. In Mac, BBC micro:bit is detected as
/dev/cuXXX where XXX board serial number
$ ls /dev/cu*
In Linux, you can check it in /dev/tty*. For Windows users, you can verify it on
Device Manager which is recognized as COM
2.3 Flashing MicroPython Firmware
In this section, we learn how to flash MicroPython firmware into BBC micro:bit
board. I show you two methods to flash the firmware.
If you have a computer with installed Ubuntu/Debian Linux, you can build
MicroPython firmware. You can type these commands to install required
libraries.
$ uflash
Connect BBC micro:bit board and then set port of your board on CoolTerm with
baudrate 115200.
On Terminal option, you don't set anything.
Next, click Connect button. You should see ">>>". If not, press ENTER on
terminal of CoolTerm.
Now you can type these scripts on CoolTerm
display.scroll("Hello, World!")
After done, you should see
2.5 Uploading Python file to BBC micro:bit
In this section, I show you how to upload our Python file to BBC micro:bit board
and then execute it. Firstly, we use the same scripts on previous section. Then,
we create a file, called hello.py, and write these scripts.
def run():
display.scroll("Hello, World!")
To upload this file, we can use ampy from Adafruit. You can install it using pip.
After that, open a serial app, CoolTerm. Connect to the board. You can verify
hello.py file already uploaded by typing these commands.
>>> import os
>>> os.listdir()
Now we can run our program by typing these commands.
>> import os
>> os.remove('hello.py')
3. Display Programming
In this chapter I'm going to explain how to work with a display on BBC
micro:bit board.
3.1 Getting Started
BBC micro:bit has a display that consists of some 5 x 5 LEDs. We can create a
text or picture using this display.
def run():
display.scroll('microbit!', wait=False, loop=True)
For demo, we can write these scripts and save as displayimage.py file.
def run():
display.show(Image.HEART)
In this chapter we learn how to work with GPIO on BBC micro:bit board using
MicroPython.
4.1 Getting Started
General-purpose input/output (GPIO) is a generic pin on an integrated circuit
whose behavior, including whether it is an input or output pin, can be controlled
by the user at run time. GPIO pins have no special purpose defined, and go
unused by default.
To understand GPIO on BBC micro:bit board, you can see it in Figure below.
To work GPIO, I use bread:bit breakout board to attach our BBC micro:bit. You
can see it on https://shop.pimoroni.com/collections/micro-bit-uk/products/bread-
bit-breakout-board-for-micro-bit. The following is my wiring.
For demo, we build two programs: blinking and led-button
4.2 Wiring
In this section, we build a simple program to build blinking. We use two LEDs
which attach to GPIO pin P8 and pi P12. If you use GPIO breakout such as
bread:bit breakout board, you connect these LEDs in breadboard on GPIO P8
and P12 pins.
def run():
print('blinking demo')
while 1:
pin8.write_digital(1)
pin12.write_digital(0)
time.sleep(2)
pin8.write_digital(0)
pin12.write_digital(1)
time.sleep(2)
def run():
print('demo digital I/O')
while 1:
led1_state = button_a.is_pressed()
led2_state = button_b.is_pressed()
pin8.write_digital(led1_state)
pin12.write_digital(led2_state)
Now you can upload and run the program by typing this command.
This chapter explains how to work with reading analog input and PWM on BBC
micro:bit.
5.1 Getting Started
We access serial port from micro USB on BBC micro:bit. In this chapter, we try
to communicate with BBC micro:bit board through analog and PWM pins.
You can see ANALOG IN as analog input pin. For PWM pins, you can use any
digital pin.
https://microbit-micropython.readthedocs.io/en/latest/pin.html
5.2 Reading Analog Input
In this section, we learn how to read analog input from BBC micro:bit board. For
demo, I use SparkFun Electret Microphone Breakout from
SparkFun, https://www.sparkfun.com/products/12758.
5.2.1 Wiring
For demo, we perform the following wiring:
def run():
print('analog input demo')
while 1:
analog_in = pin1.read_analog()
print('AIN: ' + str(analog_in))
time.sleep(2)
Save this program as analogdemo.py.
5.2.3 Testing
Now you can upload the program into BBC micro:bit board. Then, run it. You
can type this command on your serial application.
Then, try to make noise on the sensor device so you get analog output on the
Terminal.
pin 1: red
pin 2: common pin. It could be connected to GND or VCC
pin 3: green
pin 4: blue
Let's start.
5.3.1 Wiring
For our testing, we configure the following PWM pins.
time.sleep(2)
def run():
print('print PWM with RGB led')
while 1:
print('red')
set_rgb(1023, 0, 0)
print('green')
set_rgb(0, 1023, 0)
print('blue')
set_rgb(0, 0, 1023)
print('yellow')
set_rgb(1023, 1023, 0)
print('purple')
set_rgb(323, 0, 323)
print('aqua')
set_rgb(0, 1023, 1023)
5.3.3 Testing
Now you can upload the program file and then run it. Type these commands on
serial app Terminal.
In this chapter I'm going to explain how to work with SPI on BBC micro:bit
board.
6.1 Getting Started
The Serial Peripheral Interface (SPI) is a communication bus that is used to
interface one or more slave peripheral integrated circuits (ICs) to a single master
SPI device; usually a microcontroller or microprocessor of some sort.
For testing, you can write this program to send and receive data over SPI.
def run():
print('demo spi')
spi.init(2)
while 1:
tx = ''.join(chr(random.randint(50,85)) for _ in
range(4))
rx = bytearray(4)
spi.write_readinto(tx,rx)
time.sleep(2)
You should see received data from SPI as shown in Figure below.
7. Working with I2C
In this chapter we learn how to work with I2C on BBC micro:bit board using
MicroPython.
7.1 Getting Started
The I2C (Inter-Integrated Circuit) bus was designed by Philips in the early '80s
to allow easy communication between components which reside on the same
circuit board. TWI stands for Two Wire Interface and for most marts this bus is
identical to I²C. The name TWI was introduced by Atmel and other companies to
avoid conflicts with trademark issues related to I²C.
I2C bus consists of two wires, SDA (Serial Data Line) and SCL (Serial Clock
Line). BBC micro:bit has I2C pins on P19 (SCL) and P20 (SDA).
For testing, I used PCF8591 AD/DA Converter module with sensor and actuator
devices. You can find it on the following online store:
Amazon, http://www.amazon.com/PCF8591-Converter-Module-Digital-
Conversion/dp/B00BXX4UWC/
eBay, http://www.ebay.com
Dealextreme, http://www.dx.com/p/pcf8591-ad-da-analog-to-digital-digital-
to-analog-converter-module-w-dupont-cable-deep-blue-336384
Aliexpress, http://www.aliexpress.com/
In addition, you can find this device on your local electronics store/online store.
This module has mini form model too, for instance, you can find it on Amazon,
http://www.amazon.com/WaveShare-PCF8591T-Converter-Evaluation-
Development/dp/B00KM6X2OI/ .
This module use PCF8591 IC and you can read the datasheet on the following
URLs.
http://www.electrodragon.com/w/images/e/ed/PCF8591.pdf
http://www.nxp.com/documents/data_sheet/PCF8591.pdf
In this chapter, we build a program to access sensor via I2C using MicroPython
on BBC micro:bit board.
7.2 Wiring
We use PCF8591 AD/DA Converter as I2C source. You can connect PCF8591
AD/DA Converter to JBBC micro:bit board board directly.
PCF8591 AD/DA Converter SDA --> BBC micro:bit board SDA (pin P20)
PCF8591 AD/DA Converter SCL --> BBC micro:bit board CLK0 (pin
P19)
PCF8591 AD/DA Converter VCC --> BBC micro:bit board VCC 3.3V
(+3.3V)
PCF8591 AD/DA Converter GND --> BBC micro:bit board GND
def run():
print('read sensor from i2c protocol')
PCF8591 = 0x48 # I2C bus address
PCF8591_ADC_CH0 = '\x00' # thermistor
PCF8591_ADC_CH1 = '\x01' # photo-voltaic cell
PCF8591_ADC_CH3 = '\x03' # potentiometer
while 1:
# read thermistor
i2c.write(PCF8591, PCF8591_ADC_CH0)
time.sleep(1)
i2c.read(PCF8591, 1)
data = i2c.read(PCF8591, 1)
print('Thermistor: ' + str(ord(chr(data[0]))))
# photo-voltaic cell
i2c.write(PCF8591, PCF8591_ADC_CH1)
time.sleep(1)
i2c.read(PCF8591, 1)
data = i2c.read(PCF8591, 1)
print('photo-voltaic: ' + str(ord(chr(data[0]))))
# potentiometer
i2c.write(PCF8591, PCF8591_ADC_CH3)
time.sleep(1)
i2c.read(PCF8591, 1)
data = i2c.read(PCF8591, 1)
print('potentiometer: ' + str(ord(chr(data[0]))))
time.sleep(2)
If done, open Serial monitor tool and connect to BBC micro:bit. Then, type these
commands.
You should see sensor data from I2C. The following is a sample output.
8. Working with Accelerator and Compass Sensors
This chapter explains how to develop BBC micro:bit with utilizing built-in
sensors, accelerator and compass sensors.
8.1 Getting Started
In this chapter, we explore how to work with built-in sensors, accelerator and
compass.
Let's explore.
8.2 Working with Accelerator Sensor
First demo, we build a program to access accelerator sensor. We can use
accelerator object. You can read the API in this site, https://microbit-
micropython.readthedocs.io/en/latest/accelerometer.html.
For demo, we get sensor data: x, y and z values from the sensor. You can write
this program.
def run():
print('accelerometer demo')
while True:
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
gesture = accelerometer.current_gesture()
print('x= ' + str(x) + ' ;y= ' + str(y) + ' ;z= ' +
str(z))
print('gesture: ' + gesture)
sleep(500)
Now you can upload it into the board. You can run this program from
MicroPython terminal by typing these commands.
def run():
print('calibrate')
compass.calibrate()
while True:
x = compass.get_x()
y = compass.get_y()
z = compass.get_z()
Firstly, the program performs calibration. You can should move the board until
all LEDs are lighting. After that, you should see sensor data on Terminal.