The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1

178 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE


5 Figure 7-22: Displaying the humidity sensor reading

For the temperature sensor, remove the last two lines of your program and replace them with:

temp = sense.get_temperature()
print(temp)

Click Run again, and you’ll see a temperature in degrees Celsius (Figure 7-23). This might
not be the exact temperature of your room, however: the Raspberry Pi generates heat all the
time it’s running, and this warms the Sense HAT and its sensors too.

5 Figure 7-23: Showing the current temperature reading

Normally the Sense HAT reports the temperature based on a reading from the temperature
sensor built into the humidity sensor; if you want to use the reading from the pressure sensor
instead, you should use sense.get_temperature_from_pressure(). It’s also possible
to combine the two readings to get an average, which may be more accurate than using either
sensor alone. Delete the last two lines of your program and type:

htemp = sense.get_temperature()
Free download pdf