APC Australia - September 2019

(nextflipdebug2) #1

Explorer HAT Pro


We have long been fans of this board because it packs so much into a tiny package.
In this tutorial we used its analogue inputs to read voltage levels. But we also have
four protected inputs, rated up to 5V, and four outputs rated for 5V. There are two
motor connections for use with DC motors, so we can easily build robots.
We have four touch buttons that can be used to trigger projects to life. We also
have four capacitive touch connections for use with crocodile clips, enabling us to
make connections and circuits that react to human skin. There also four LEDs that
we used in these projects – these can be on or off, or we can fade their light levels
to create subtle glows.
On the right side of the board are connections for I2C and SPI (3.3V only) which
means we can connect other sensors and components using their protocols. Lastly,
we have a breadboard built into the board and ready for use to build our projects
upon it. We get all of this versatility for around $38 – a bargain when you consider
how much we can learn from it.

flicker effect.
import time
import explorerhat as eh
import random
To run our code we need a loop and
for that we shall use while True ,
which will run our code until we stop it.
while True:
The following code is indented to
show that it runs inside of the loop. We
create four variables, called on ,
off , fade_in and fade_out.
These variables will be later used to
control the flicker effect of the LEDs.
But how do we create that? Well, we
need to introduce a little
randomisation. Using the random.
uniform function we can generate a
random float (a number with a decimal
point) between 0.0 and 1.0, and then
store it in the variable:
on = random.
uniform(0.0,1.0)
off = random.
uniform(0.0,1.0)
fade_in = random.
uniform(0.0,1.0)
fade_out = random.
uniform(0.0,1.0)
To detect that someone has ‘blown
out’ the candle, we need to read the
sensor connected to Analog 1. Using
the Explorer HAT library we read the
value of the sensor and store it as a
variable called blow , which is then
printed to the Python shell for debug
with a small delay for legibility.
blow = eh.analog.one.
read()
print(blow)


time.sleep(0.1)
If we run the code as is, it will print
the sensor reading, which in our case
was 5.00. This is the voltage from the
sensor. Using the trim pot (blue dial) on
the microphone, we can tune the
sensor; we chose 1.5 as the trigger
point. So if the voltage goes over the
trigger point it means that a loud sound
(blowing out the candle) has been
detected.
if blow > 1.5:
Now we need to turn the Explorer
HAT lights off. Using the Explorer HAT
library we turn off all the lights and
then wait for two seconds before the
loop repeats.
eh.light.off()

time.sleep(2)
But if we haven’t blown out the
candle, the else condition activates
and it uses the pulse function to
make the lights flicker using the on ,
off , fade_in and
fade_out values.
else:
eh.light.pulse(on,
off, fade_in, fade_out)
Save the code and then run it. Now
try to blow out the candle. If it works,
great; if not, adjust the values of your
code and the trim pot on the
microphone.
So there we have it, two simple
projects from one great board.

The microphone/ sound sensor has a blue trim
pot that can be used to alter the output
voltage. This is how we can calibrate the
sensor for the environment.

Using a microphone/sound sensor we
can measure the sound level in a room
and use that as a trig ger in our projects.
Free download pdf