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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
from microbit import * # function to read data file: def get_nv_data(name): try: with open(name) as f: v = int(f.read()) except OSError: v = temperature() try: with open(name, 'w') as f: f.write(str(v)) except OSError: display.scroll('Cannot create file %s' % name) except ValueError: display.scroll('invalid data in file %s' % name) v = temperature() return v # function to write data file: def set_nv_data(name, value): try: with open(name, 'w') as f: f.write(str(value)) except OSError: display.scroll('Cannot write to file %s' % name) min = get_nv_data('min.txt') max = get_nv_data('max.txt') while True: currentTemp = temperature() if currentTemp < min: min = currentTemp set_nv_data('min.txt', min) if currentTemp > max: max = currentTemp set_nv_data('max.txt', max) if accelerometer.was_gesture('shake'): display.scroll(currentTemp) if button_a.was_pressed(): display.scroll(get_nv_data('min.txt')) if button_b.was_pressed(): display.scroll(get_nv_data('max.txt')) if pin2.is_touched(): display.scroll('clearing data') set_nv_data('min.txt', currentTemp) set_nv_data('max.txt', currentTemp)
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
max.txt which the Python program can read and update.
The program uses three variables to track and compare the temperature:
currentTempis the current temperature reading from the micro:bit's built-in temperature sensor
maxis the maximum temperature. This gets assigned a new value if the current temperature is greater than (>) the current value of
minis the minimum temperature. This gets assigned a new value if the current temperature is less than(<) the current value of
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 termperature.
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
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
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 sourrounding air. Measure the difference using a normal thermometer and add an offset to the program by subtracting the difference from the
- 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.