Step 1: Make it
What is it?
Track highest and lowest temperatures by leaving this program running on a micro:bit.
These two videos show you what you'll make and how to code it:
Introduction
Coding guide
How it works
- Like the Thermometer project, this uses the temperature sensor inside the micro:bit’s CPU (central processing unit) to measure the temperature in °C (Celsius).
 - This program keeps track of the lowest and highest temperatures recorded by using 3 variables: currentTemp is the current temperature reading, max is the maximum and min is the minimum.
 - At the start of the program they are all set to the same value; an infinite (forever) loop ensures that every two seconds it takes a reading, and the program compares the current temperature with the max and min variables.
 - If the current temperature is less than (<) than the value stored in the min variable, it changes the min variable to be the same as the current temperature.
 - If the current temperature is greater than (>) the max variable’s value, it changes the max variable to be the same as the current temperature.
 - The program also flashes a dot on the LED display every time the infinite loop runs so that you know it’s working.
 - Press button A to show the minimum and button B to show the maximum temperatures recorded.
 - You could leave the micro:bit running for 24 hours, record the maximum and minimum temperatures and plot on a chart at the same time every day and then reset.
 
What you need
- micro:bit (or MakeCode simulator)
 - MakeCode or Python editor
 - battery pack (optional)
 - a source of heat or cooling, like a fan, if you want to see the temperature change quickly – or take the micro:bit outside
 - graph paper if you want to keep a chart of temperatures over time
 
Step 2: Code it
1from microbit import *
2
3currentTemp = temperature()
4max = currentTemp
5min = currentTemp
6
7while True:
8    display.show('.')
9    currentTemp = temperature()
10    if currentTemp < min:
11        min = currentTemp
12    elif currentTemp > max:
13        max = currentTemp
14    if button_a.was_pressed():
15        display.scroll(min)
16    if button_b.was_pressed():
17        display.scroll(max)
18    sleep(1000)
19    display.clear()
20    sleep(1000)
21Step 3: Improve it
- Compare the reading with another thermometer. How accurate is the micro:bit? Do you need to modify the micro:bit reading to get the air temperature? How could you do that?
 - Convert the temperature to Fahrenheit.
 - Use radio to send temperature readings to another micro:bit.
 
This content is published under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licence.


