Computer Shopper - UK (2020-10)

(Antfer) #1

92 OCTOBER2020|COMPUTER SHOPPER|ISSUE392


O


ne of the best things about
the Raspberry Pi is theway
thatyou can expand its
capabilities withadditional
hardware. One of the most
flexible ways of adding
features is with theSense HATadd-on.
We’ve used the cheaper Sense HAT(B) (£26,
http://www.amazon.co.uk). This simple boardadds
agyroscope to detect movement, plus
temperature, humidity andbarometric
sensors for monitoring theenvironment.
Youcan then do some clever things with
these readings, such as automatically
warning you whenthe temperatureisvery
hot (or cold), or when the humidity has
spiked. These readings can be useful in
maintaining aclean indoor environment,
helping you to recognise when you should
open awindowtoreduce humidity to
prevent mould growth, for instance.
Here, we’ll show you how to get started
with this module.

INSTALL THE SENSE HAT


First, you need to install the Sense HAT. It’s
compatible with all modelsofthe Pi,but it’s
easier to install on any of the models that
have the full GPIO pins (the Zero models
requireyou to do some soldering).
Ta ke your Raspberry Pi and turnitoff.
Place the Sensor HATupside down and
line up its GPIOconnector pins with those
on the Pi:the HATshould cover up most of
the Pi’s motherboard. Press firmly down to
makethe connection.
In the box, you’ll also findaGPIO
extension. This plugsintothe GPIO portfirst,
then its extra-long pins stick up through the
HAT’sboard, giving you away to use the
GPIO pins for other devices. Forexample,
you could automatically have an LED turn on
when humidity reaches acertain level.
This option can be good as it also lifts the
sensors further away from the Raspberry Pi’s
board. In particular,the Pi’s temperaturecan
affect the internal temperaturesensor.We’ll
show you how to account for this in code, but

the Sense HAT(B) also has an optional
external temperaturesensor available for it.
Once the HATisinplace, you can power
on your Pi and it will boot up as normal.

CONFIGURING THE SENSE HAT


Once your Pi has started up,you need to
follow the process to getitready to go.
First, you need to get up acommand
terminal and install some packages by
typing in the commands below:

wget http://wget http://www.airspayce.com/mikem/www.airspayce.com/mikem/
bcm2835/bcm2835-1.60.tar.gzbcm2835/bcm2835-1.60.tar.gz
sudo tar zxvf bcm2835-1.60.tar.gzsudo tarzxvfbcm2835-1.60.tar.gz
cd bcm2835-1.cd bcm2835-1.xxxx
sudo ./configuresudo ./configure
makemake
sudo make checksudo make check
sudo make installsudo make install
sudo apt-getinstallpython-pipsudo apt-getinstall python-pip
sudo pip install RPi.GPIOsudo pipinstall RPi.GPIO
sudo pip install spidevsudo pipinstall spidev
sudo apt-getinstallpython-imagingsudo apt-getinstall python-imaging
sudo apt-getinstallpython-smbussudo apt-getinstall python-smbus

Once you’vedone this, you need to enable
the I2C interface. To do this, typesudo

raspi-configand hit Enter.Select the
Interfacing Options, I2C and then select Yes.

CREATE YOUR CODE


Next, you need to build some code to
startreading your Sense HAT. There’s
some example code available on the
manufacturer’s website (tinyurl.com/
392piprojects2)thatyou can download.
Thefile is a7-Zip archive thatwefound
easier to extract on acomputer first.
ThePython examples arethe best to use.
Forthis project, we’ve edited the
temperatureand humidity codetodo
somethingslightly different. It’s available
online at (www.shopperdownload.co.uk/pi/
tandh.py), butthe code is as follows:

#!/usr/bin/python#!/usr/bin/python
#-*- coding:utf-8 -*-#-*- coding:utf-8 -*-
import ctypesimport ctypes
import sysimport sys
import timeimport time

class SHTC3:class SHTC3:
defdef___init_init___(self):_(self):
self.dll =ctypes.CDLL("./SHTC3.so")self.dll =ctypes.CDLL("./SHTC3.so")
init =self.dll.initinit =self.dll.init

Turn your Pi into an

environmentalsensor

Withthe Sense HATmodule,you canturnyour Pi intoacomplete

environmental sensortomeasuretemperature, humidityand more

BELOW:


PROJECT3

Free download pdf