The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1
Chapter 7 Physical Computing with the Sense HAT 177

THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE


pressure = sense.get_pressure()
print(pressure)

Click Run and you’ll see a number printed to the Python shell at the bottom of the Thonny
window. This is the air pressure reading detected by the barometric pressure sensor, in
millibars (Figure 7-21). Try blowing on the Sense HAT (or moving the Pressure slider up in the
emulator) while clicking the Run icon again; the number should be higher this time.


5 Figure 7-21: Showing a higher pressure reading


To switch to the humidity sensor, remove the last two lines of code and replace them with:

humidity = sense.get_humidity()
print(humidity)

Click Run and you’ll see another number printed to the Python shell: this time, it’s the current
relative humidity of your room as a percentage. Again, you can blow on the Sense HAT (or
move the emulator’s Humidity slider up) and you’ll see it go up when you run your program
again (Figure 7-22, overleaf)– your breath is surprisingly humid!


CHANGING VALUES
If you’re using the Sense HAT emulator, you can change the
values reported by each of the emulated sensors using its
sliders and buttons. Try sliding the pressure sensor setting down
towards the bottom, then clicking Run again.
Free download pdf