Taking a Temperature Reading

What you will do:
Take a temperature reading and show it on the display.
What you will learn:
How to use the temperature sensor, how to create a variable, how to display a variable on the segments.

Open a code editor

We can use Thonny Python IDE or Mu from the programming menu on the Raspberry Pi.

Whichever one you choose, open a new project.
Type in these lines of code:

import rainbowhat

temperature = rainbowhat.weather.temperature()

We have imported the rainbowhat module like we did last time.
On the second line we create a variable called "temperature", and write down what to store in it - using the weather part of the Rainbow HAT, take the temperature.

Now we've stored the temperature, we need do something with it. Add these lines of code:

rainbowhat.display.print_float(temperature)
rainbowhat.display.show()

Last time we got the display to print_str which put the string (the word) into the buffer. This time we need the display to put the temperature into the buffer, but it needs to be a floating point number, so we ask it to print_float. After that we need to tell it to show the contents of the buffer on the display.

A floating point number is one which stores the digits in the right order, and also stores the location of the decimal point separately.

Run your code

Run your code by pressing "play" in Thonny or Mu. You should see the temperature appear on the display.

Next steps

You will notice that the temperature is higher than you might expect.

  1. Why is the temperature higher?
  2. Can you add in a bit of maths inside the brackets on the third line to correct it?

Finished? Now try taking a pressure reading.