3. Thermometer
3.1. Temperature Function
The thermometer gets the temperature as an integer in degrees Celsius.
- temperature()
Return the temperature of the microbit in degrees Celsius.
The temp variable below is an integer. It has to be first converted to a string to join it with the unit symbol, C.
from microbit import *
while True:
temp = temperature()
display.scroll(str(temp) + 'C')
sleep(500)
The temperature the thermometer measures will typically be higher than the true temperature due to heat from the nearby electronics on the microbit.
Exercises
Plug in the battery pack and move the microbit to a warmer or cooler place. What temperatures do you get?
Compare the temperature inside with that outside.
Place the microbit on a paper towel in the freezer or fridge for 2 min and read the temperature.
Tasks
Use an if block so that one image is shown when it is below 20 degrees and another image is shown when it is above 20 degrees.
Use the A-button to store the temperature in the variable
tempA
. Use the B-button to store the temperature in the variabletempB
. Calculate the difference between them usingtempA - tempB
and scroll this difference.
Use an if block so that one image is shown when it is below 25 degrees and another image is shown when it is 20 degrees or higher.
from microbit import *
while True:
temp = temperature()
#display.scroll(temp)
if temp < 25:
display.show(Image.UMBRELLA)
else:
display.show(Image.TSHIRT)
Use the A-button to store the temperature in the variable tempA
. Use the B-button to store the temperature in the variable tempB
. Calculate the difference between them using tempA - tempB
and scroll this difference. To make sure that both temperature variables have a value, set them both before the while True
loop.
from microbit import *
tempA = temperature()
tempB = temperature()
while True:
temp = temperature()
if button_a.is_pressed():
tempA = temp
elif button_b.is_pressed():
tempB = temp
display.scroll(tempA - tempB)
sleep(200)
Project
from microbit import *
current_temp = temperature()
max_temp = current_temp
min_temp = current_temp
display.scroll(current_temp, delay=80)
while True:
current_temp = temperature()
if current_temp < min_temp:
min_temp = current_temp
elif current_temp > max_temp:
max_temp = current_temp
if button_a.is_pressed() and button_b.is_pressed():
display.scroll(max_temp - min_temp, delay=80)
elif button_a.is_pressed():
display.scroll(min_temp, delay=80)
elif button_b.is_pressed():
display.scroll(max_temp, delay=80)
sleep(1000)