Skip to content

Activity

Fahrenheit thermometer

Intermediate | MakeCode, Python | Buttons, LED display, Temperature sensor | Functions, Input/output, Measurement

Step 1: Make it

What is it?

Use a simple function to convert centigrade readings from the micro:bit's temperature sensor to Fahrenheit.

How it works

  • The micro:bit's processor has a built-in temperature sensor input which gives readings in centigrade.
  • Using functions allows you easily to convert the temperature to Fahrenheit.
  • The convertCtoF function means you can re-use the conversion code easily, for example in a maximum-minimum thermometer.
  • The function is called by using convertCtoF in place of a variable or number when you press button B on your micro:bit.
  • We pass to the function the temperature in centigrade.
  • The function then takes the number passed to it, stored in a variable called C, and converts it to Fahrenheit by multiplying it by 1.8 and adding 32.
  • The function then returns the converted number so when you press button B the temperature is shown in Fahrenheit on the LED display output.
  • If you press button A, the temperature is shown in centigrade.

What you need

  • micro:bit (or MakeCode simulator)
  • MakeCode or Python editor
  • battery pack (optional)

Step 2: Code it

1from microbit import *
2
3def convertCtoF(C):
4    return C * 1.8 + 32
5
6while True:
7    if button_a.was_pressed():
8        display.scroll(temperature())
9    if button_b.was_pressed():
10        display.scroll(convertCtoF(temperature()))

Step 3: Improve it