The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1

174 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE


Sensing the world around you
The Sense HAT’s LED matrix is fun, but its real power lies in the various sensors it has. These
allow you to take readings of everything from temperature to acceleration, and use them in
your programs as you see fit.

Environmental sensing
The barometric pressure sensor, humidity sensor, and temperature sensor are all environmental
sensors; they take measurements from the environment surrounding the Sense HAT.

Environmental sensing in Scratch
Start a new program in Scratch, saving your old one if you wish, and add the Sense HAT
extension using the More Blocks category. Drag a when clicked Events block onto your
script area, then drag a set all pixels to white block underneath it and use the down arrow
to change it into a set all pixels to off block. It’s always a good idea to do this at the start of
your programs, just in case the Sense HAT’s display is still showing something from the last
program it ran.
Drag a say Hello! for 2 secs Looks block directly underneath your existing blocks. To take
a reading from the pressure sensor, find the pressure block in the More Blocks category and
drag it over the word ‘Hello!’ in your say Hello! for 2 secs block.

when clicked


set allpixels to off


say pressure for^2 secs


Click the green flag and the Scratch cat will tell you the current reading from the pressure
sensor in millibars. After two seconds, the message will disappear; try blowing on the Sense
HAT (or moving the Pressure slide up in the emulator) and clicking the green flag to run the
program again; you should see a higher reading this time (Figure 7-18).

EMULATING THE SENSORS
If you’re using the Sense HAT Emulator, you’ll need to enable
inertial and environmental sensor simulation: in the Emulator,
click Edit, then Preferences, then click on the boxes next to
‘Environment sensors’ and ‘Inertial Measurement Unit’ to tick them
before clicking the Close button.
Free download pdf