Lesson 2 (measuring temperature)

We connect to the Raspberry Pi with the Sense HAT like in the previous lesson and we enter to python environment.

We prepare Sense HAT like before, as well.

from sense_hat import SenseHat
sense = SenseHat()

To find out what temperature we have at the moment, we type:

sense.get_temperature()

If we need to store the temperature value, we type

temp = sense.get_temperature()

We stored the value and we gave it a name: temp

In programming, this is called a variable. The value of the variable is the temperature value we measured.

To see the value again, we type

print(temp)

The number is long, so we can shorten it by rounding it.

temp = round(temp, 2)

We confirm that it is rounded now

print(temp)

Try to display the temperature on Sense HAT array

sense.show_message(temp)

The command will fail. This is happening because temp variable is a number and HAT displays only letters. So we turn our variable to a word (in programming this is called “alphanumerical”) with this command

temp = str(temp)

And we try again.

sense.show_message(temp)