APC Australia

Using analogue HAT interfaces

Les Pounder shows us two more projects using the deceptivel­y simple Explorer HAT Pro – and a little Python code.

-

This issue we’ll learn how to use the Explorer HAT Pro to work with analogue electronic­s by creating two projects. First we create a simple light-detecting nightlight, then we create an electronic candle that we can blow out. But first we need to set up our equipment.

With the power turned off to your Raspberry Pi, place the Explorer HAT Pro so that it connects to all 40 GPIO pins, and so that it fits neatly above the Pi. Now connect your keyboard, mouse, HDMI and so on to your Pi and boot up. For the next step you will need to connect to the internet using Wi-Fi or Ethernet. Once connected, open a terminal, the icon for which is found in the top left of the screen. In the terminal type the following to automatica­lly install and configure the Explorer HAT Pro software:

$ curl https://get.pimoroni. com/explorerha­t | bash

During the install you will be asked to configure I2C and to install example code which you can get from http://bit. ly/lxf252piha­t. Answer yes to these questions. Once installed, close the terminal window.

PROJECT 1: NIGHTLIGHT

To introduce analogue electronic­s we shall use the most simple device, a light dependent resistor (LDR). This component changes resistance based on light levels. For this project we shall place the LDR on the breadboard and connect one leg to 3.3V, and the other to GND via the 22K ohm resistor. Then we connect Analog 1 from the board to the same line as our resistor and LDR. We do this as it creates a voltage divider that we use to measure the light level as a voltage. See the diagram in the download for an accurate reference.

We now move on to the code. Using your favourite editor (IDLE, Mu, Thonny) create a new file and then save that new file as nightlight.py. Remember to save often! We start the code with two imported libraries,

time and explorerha­t , which we rename to eh for better convenienc­e. import time import explorerha­t as eh

Then we create a loop to constantly run our code. while True:

Let’s take a light level reading and store it inside a variable called light – this will show the voltage that our circuit is producing based on the resistance level of the LDR. This is printed to the Python shell for debug.

light = eh.analog.one. read()

print(light)

We then use a conditiona­l test that checks to see if the voltage detected is less than 2V. If that is the case then it must be getting dark, so the four LEDs on the Explorer HAT Pro will turn on. if light < 2.0:

eh.light.on()

But if it daylight then the voltage will be over 2V and so the else condition activates and turns off the Explorer HAT LEDs. else:

eh.light.off()

Save the code and when ready, run it. Cover the LDR with your hand and the LEDs should light up; move it and the LEDs turn off. If you need to tweak the values, take a look at the Python shell to see what voltage changes there are when the sensor detects darkness.

PROJECT 2: A DIGITAL CANDLE

This project is inspired by the Nintendo DS, specifical­ly a section in The Legend of Zelda: Phantom Hourglass where you are instructed to ‘blow out’ fire/torches in the game. To do this we blow on to the console and the game reacts. In reality we are blowing onto a microphone and an analogue signal is being measured to act as a trigger. To replicate this we shall control the LEDs present on the Explorer HAT Pro using a microphone board. The microphone is connected to our Explorer HAT Pro as: GND > GND, OUT > Analog 1 and VCC > 5V.

We now move on to the code. Create a new file as before and then save that new file as candle.py. The first three lines of our Python code import prewritten libraries of code. The first is called time and we use that to control the speed of our project. Next we import the explorerha­t library, but rename it to eh for ease of use. Lastly we import the random library which will be used for our candle

flicker effect. import time import explorerha­t 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 randomisat­ion. 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.

 ??  ?? This simple circuit offers a great introducti­on to analogue electronic­s. It requires only a few cheap components and is accessible to all.
This simple circuit offers a great introducti­on to analogue electronic­s. It requires only a few cheap components and is accessible to all.
 ??  ?? Using a microphone/sound sensor we can measure the sound level in a room and use that as a trigger in our projects.
Using a microphone/sound sensor we can measure the sound level in a room and use that as a trigger in our projects.
 ??  ?? 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 environmen­t.
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 environmen­t.

Newspapers in English

Newspapers from Australia