Step 1: Make it
What is it?
Turn your micro:bit into a self-contained data logger that records maximum and minimum temperature readings, and stores them so it keeps the data even if the batteries run out or you disconnect the power.
It's a Python program, but you don't need to understand anything about Python to use it.
What you'll learn
- How to use Python to read and write data to non-volatile storage that stays on your micro:bit even when the power is removed
- How to handle errors in Python programs
- How to convert numerical variables to strings and back again
What you need
- A micro:bit
- optional battery pack
How to use it
- Download the .hex program file and flash it onto your micro:bit.
- If you have a battery pack, attach it and leave the micro:bit somewhere where the temperature will vary. You could test it by putting it outside or in a fridge and leaving it for a few minutes.
- Shake it to show the current temperature.
- Press button A to show the minimum recorded temperature.
- Press button B to show the maximum recorded temperature.
- Press GND and pin 2 at the same time with your fingers to reset the maximum and minimum temperature to the current temperature.
Step 2: Code it
1from microbit import *
2
3# function to read data file:
4def get_nv_data(name):
5 try:
6 with open(name) as f:
7 v = int(f.read())
8 except OSError:
9 v = temperature()
10 try:
11 with open(name, 'w') as f:
12 f.write(str(v))
13 except OSError:
14 display.scroll('Cannot create file %s' % name)
15
16 except ValueError:
17 display.scroll('invalid data in file %s' % name)
18 v = temperature()
19
20 return v
21
22# function to write data file:
23def set_nv_data(name, value):
24 try:
25 with open(name, 'w') as f:
26 f.write(str(value))
27 except OSError:
28 display.scroll('Cannot write to file %s' % name)
29
30min = get_nv_data('min.txt')
31max = get_nv_data('max.txt')
32
33while True:
34 currentTemp = temperature()
35 if currentTemp < min:
36 min = currentTemp
37 set_nv_data('min.txt', min)
38 if currentTemp > max:
39 max = currentTemp
40 set_nv_data('max.txt', max)
41 if accelerometer.was_gesture('shake'):
42 display.scroll(currentTemp)
43 if button_a.was_pressed():
44 display.scroll(get_nv_data('min.txt'))
45 if button_b.was_pressed():
46 display.scroll(get_nv_data('max.txt'))
47 if pin2.is_touched():
48 display.scroll('clearing data')
49 set_nv_data('min.txt', currentTemp)
50 set_nv_data('max.txt', currentTemp)
51
How it works
This data logger stores its readings even when the power is disconnected from your micro:bit. It does this by storing the readings in non-volatile storage. This is computer memory that keeps its contents when the power is turned off, just like your micro:bit keeps a program you have flashed onto it when you unplug it from the computer.
It stores the data on your micro:bit in two text files called min.txt
and max.txt
which the Python program can read and update.
The program uses three variables to track and compare the temperature:
currentTemp
is the current temperature reading from the micro:bit's built-in temperature sensormax
is the maximum temperature. This gets assigned a new value if the current temperature is greater than (>) the current value ofmax
.min
is the minimum temperature. This gets assigned a new value if the current temperature is less than (<) the current value ofmin
.
Two functions, get_nv_data
and set_nv_data
, read and write numerical data to the non-volatile text files. These functions convert numbers to text and back:
int()
converts text (also called a string) to a numerical (integer) variable we can use to compare with the current temperature.str()
converts a numerical variable, like a temperature reading, to a text string so it can be saved in a text file.
The functions use try
and except
to catch errors reading or writing the data files. If there is no text file saved with the maximum and minimum temperatures, for example when you first run it, it sets the max
and min
variables to be the current temperature.
The main part of the program runs inside the while True:
loop. This behaves like the forever block in MakeCode.
Step 3: Improve it
- The micro:bit's temperature sensor is inside the processor and usually gives temperature readings higher than the surrounding air. Measure the difference using a normal thermometer and add an offset to the program by subtracting the difference from the
temperature()
reading - Convert the program to log different kinds of data, such as forces measured by the accelerometer.
- Add radio functionality to also send data to another micro:bit in another location.
This content is published under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licence.