The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1

176 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE


For the temperature sensor, it’s as easy as deleting the humidity block and replacing it with
temperature , then running your program again. You’ll see a temperature in degrees Celsius
(Figure 7-20). 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-20: Displaying the temperature sensor reading

Environmental sensing in Python
To start taking readings from sensors, create a new program in Thonny and save it as Sense
HAT Sensors. Type the following into the script area, as you always should when using the
Sense HAT – and remember to use sense_emu if you’re using the emulator:

from sense_hat import SenseHat
sense = SenseHat()
sense.clear()

It’s always a good idea to include sense.clear() at the start of your programs, just in
case the Sense HAT’s display is still showing something from the last program it ran.
To take a reading from the pressure sensor, type:

CHALLENGE: SCROLL & LOOP
Can you change your program to take a reading from each
of the sensors in turn, then scroll them across the LED
matrix rather than printing them to the stage area? Can
you make your program loop, so it’s constantly printing the
current environmental conditions?
Free download pdf