Maximum PC

Operate LEDs and Motors With Your Pi

-

YOU’LL NEED THIS

RASPBERRY PI Any 40-pin model of Pi, with Raspbian Pixel.

EXPLORER HAT PRO Plus 1x LED, 1x 220 Ohm resistor, 1x push button switch, a DC motor, 6x male-to-male jumper wires, and a 10k potentiome­ter.

WE’VE GATHERED TOGETHER a quartet of 10-minute hacks that you can build using the Explorer HAT Pro for the Raspberry Pi. 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 to 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” to begin. –LES POUNDER

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 continuing. Remember to save often.

>> We start the code by importing the “Explorer HAT Python3” library, 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 color of LED that will be toggled on/off by pressing the button. We then add a delay to prevent an accidental double-tap:

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. 2 THE BLINKING OBVIOUS For this project, you need the potentiome­ter and three jumper wires. Connect as per the diagram [ Image A]. 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’s just over 5V.

>> Create a new blank file called “Project2-AnalogBlin­k.py,” and import the “Explorer HAT” library:

import explorerha­t as eh

>> Inside a “while True” loop, we now create two variables to read the voltage 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 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, 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 makes the LEDs flash faster, while a high voltage makes them flash 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 blink. 3 PUSH THE BUTTON This project shows how the inputs and outputs of the Explorer HAT Pro 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 shown below-left [ Image B], 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 only activated when turned on.

>> Create a new blank file called “Project3-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 whether 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, 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 whether 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. 4 GAIN MOTOR CONTROLS For our final project, we need to connect up the potentiome­ter once again, and add a DC motor connected to MOTOR1 + and -. We’re adding 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, along with full forward/ reverse control [ Image C].

>> Create a file called “Project4-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 multiply the value by 20 to give us a percentage value, which is then passed to the final line of code that controls the motor forward, and takes a percentage value to represent its 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 spins. And now you’ve completed four projects with the Explorer HAT Pro!

speed = (round(eh.analog.one.read()) * 20) eh.motor.one.forward(speed)

 ??  ??
 ??  ??
 ??  ??

Newspapers in English

Newspapers from United States