Linux Format

Analogue projects.

Les Pounder shows us how we can safely experiment and tinker with the Explorer HAT Pro, a “one for all” board that’s easy and cheap to use.

-

Les Pounder uses his HAT Explorer Pro to build four projects that use analogue to the max.

This month we’re going through some 10-minute hacks that you can build using the Explorer HAT Pro. These four projects can be mixed together, and we’ll be using the Analog inputs with potentiome­ters, digital Input/ Outputs with LEDs and buttons, and the H Bridge controller for operating DC motors with the board.

Before we power on the Raspberry Pi we need to attach the Explorer HAT Pro to all 40 of the GPIO pins. The board should sit atop the Raspberry Pi and match the outline of the Pi. Now power on your Raspberry Pi and boot to the Pixel desktop. When ready, open a Terminal and enter the following command to install the Explorer HAT Pro libraries: $ curl https://get.pimoroni.com/explorerha­t | bash

Follow the installati­on instructio­ns and if prompted to reboot, do so and return the Pixel desktop. Now go to the main menu and navigate to the Programmin­g menu. Click Python 3 to open the editor, then click File>New.

Project 1: toggle the LED

Here, we’ll use the capacitive touch buttons (1-4) on the Explorer HAT Pro to trigger the built-in LEDs. With our blank document open, click File>Save and save the file as Project1LE­D-Toggle.py before continuini­ng. Remember to save often.

We start the code by importing the Explorer HAT Python3 library, and then renaming it to “eh” so that it is easier to work with. We also import the sleep function from the time library: import explorerha­t as eh from time import sleep

Next, create an infinite loop to hold the code’s main body: while True:

Then we set up a series of conditiona­l tests, seeing if the capacitive touch buttons (1-4) have been pressed. For each button we assign a colour of LED that will be toggled on/off by pressing the capacitive touch button. We then add a delay to prevent an accidental double tap of the button: if eh.touch.one.is_pressed(): eh.light.blue.toggle() sleep(0.1) The rest of the code follows the same pattern, but using elif to represent the other conditions to test: elif eh.touch.two.is_pressed(): eh.light.yellow.toggle() sleep(0.1) elif eh.touch.three.is_pressed(): eh.light.red.toggle() sleep(0.1) elif eh.touch.four.is_pressed(): eh.light.green.toggle() sleep(0.1) Click Run>Run Module to run the code. Now press the 1-4 keys on the Explorer HAT Pro to trigger the LEDs.

Project 2: the blinking obvious

For this project you’ll need the Potentiome­ter and three jumper wires. Connect as per the diagram ( belowleft). Our potentiome­ter is connected to the ANALOG1 pin and sends the voltage passing through the variable resistor as the dial is turned. At full voltage it’ll be just over 5V.

Create a new blank file called Project2-Analog-Blink.py and import the Explorer HAT library: import explorerha­t as eh

Now inside of a while True loop we create two variables to read the voltage reading on ANALOG1 and store it for use: while True: on = eh.analog.one.read() off = eh.analog.one.read() Next, we create a little hack that prevents the Python code from crashing due to a divide by zero error based on the analog readings. If the value of our variables “on” and “off” ever reaches zero, then we change their values to be 0.01: if on == 0 and off == 0: on = 0.01 off = 0.01 Our last line of code flashes the built-in LEDs (called blinking) with an on/off time that matches the voltage read at ANALOG1. This means a low voltage will make the LEDs flash faster, a high voltage, slower.

eh.light.blink(on,off)

Click Run>Run Module to run the code. Now rotate the potentiome­ter’s dial to change how fast the LEDs will blink.

Project 3: gain motor controls

For this project we’ll keep the potentiome­ter connected, and add a DC motor connected to MOTOR1 + and -. We’ll add a simple speed controller for a DC motor using the potentiome­ter from Project 2 as an input. The Explorer HAT Pro comes with a DRV8833 motor controller that offers two motor channels and full forward/reverse control.

Create a file called Project3-Analog-Motor.py before starting this project and importing the Explorer HAT library: import explorerha­t as eh

We then create an infinite loop: while True:

Inside the loop we create a variable called speed, and in there we store the reading from ANALOG1, but we round the value to one decimal place for ease of use. We then multiple the value by 20 to give us a percentage value, which is then passed to the final line of code that controls the motor forwards, and takes a percentage value to represent its speed: speed = (round(eh.analog.one.read()) * 20) eh.motor.one.forward(speed) With the code completed, click Run>Run Module to run the code. Now rotate the dial of your potentiome­ter to change how fast the motor will spin.

Project 4: push the button

Our final project shows how the inputs and outputs of the Explorer HAT work with external components, using a push button to toggle the built-in LEDs and a standard LED.

Building this project requires the push button, an LED, a 220 Ohm resistor and four jumper wires. The circuit is built on the breadboard as per the diagram ( right) with the button connected to 5V and INPUT1, the long leg of the LED

connected to the 5V pin, and the short leg connected to OUTPUT1 via the resistor. The Explorer HAT OUTPUT pins are a path to Ground, and is only activated when turned on.

Create a new blank file called Project4-Input-Output.py, import the Explorer HAT library and then import the sleep function from the time library: import explorerha­t as eh from time import sleep

Now let’s create a loop, and in there a conditiona­l test, that will check to see if the capacitive touch buttons (1-4) have been pressed. For button 1 it looks like this: while True: if eh.touch.one.is_pressed():

If that button has been pressed then we toggle the Blue LED under button 1 to turn on/off. We also add a short sleep to prevent accidental double taps: eh.light.blue.toggle() sleep(0.1)

We then create further conditiona­l “else if” tests that will check to see if buttons 2-4 have been pressed, and toggle the LED under each button to turn on: elif eh.touch.two.is_pressed(): eh.light.yellow.toggle() sleep(0.1) elif eh.touch.three.is_pressed(): eh.light.red.toggle() sleep(0.1) elif eh.touch.four.is_pressed(): eh.light.green.toggle() sleep(0.1) Click Run>Run Module to run the code. Now press the capacitive buttons 1-4 to toggle the LEDs on and off. And now you’ve completed four projects with the Explorer HAT Pro!

 ??  ?? Potentiome­ters send a voltage to the Analog input that’s varied by turning the dial to alter resistance.
Potentiome­ters send a voltage to the Analog input that’s varied by turning the dial to alter resistance.
 ??  ?? Our expert Les Pounder is a maker and tinkerer who travels with the Raspberry Pi Foundation’s Picademy, helping educators to take their first steps with the Pi, micro:bit and Arduino.
Our expert Les Pounder is a maker and tinkerer who travels with the Raspberry Pi Foundation’s Picademy, helping educators to take their first steps with the Pi, micro:bit and Arduino.
 ??  ?? Buttons and LEDs will be your go-to components for many projects. They enable input to be selective, and output to be easily debugged.
Buttons and LEDs will be your go-to components for many projects. They enable input to be selective, and output to be easily debugged.
 ??  ?? We used a micro gear metal motor because it’s 5V compliant, yet can handle the requiremen­ts of robotics.
We used a micro gear metal motor because it’s 5V compliant, yet can handle the requiremen­ts of robotics.
 ??  ??

Newspapers in English

Newspapers from Australia