Linux Format

PC-based GPIO......................

Les Pounder shows us how to revive an aging computer using Raspberry Pi Desktop and a spare Pi Zero.

-

We’ve heard of doing crazier things, but now you can gain GPIO access via your USB connection.

The Raspbian operating system, based upon Debian, has long powered the Pi, and now it’s available for desktops and laptops, reviving tired old computers with a light and usable operating system called Raspbian Desktop. In this tutorial we shall use Raspberry Pi Desktop on a spare computer and then attach a Raspberry Pi Zero as a GPIO expander device using the micro USB port of the Zero in USB OTG (On The Go) mode as a USB gadget. This will give our laptop the same OS as our Pi, and grant it access to the GPIO for projects.

Getting started

Downloadin­g the Raspberry Pi Desktop is easy. Visit www.raspberryp­i.org/downloads/raspberry-pi-desktop and click the download link. This will download an ISO file – a disk image that we can write to a spare USB flash drive. The best and easiest tool to use to write the image is Etcher ( https://etcher.io) which is a great interface to safely and easily write ISO images to flash drives.

Download Etcher and extract the file to your desktop. Now go to your desktop and double-click the file to open Etcher. The program has three stages to write an image to a drive. First it asks you to tell it where the file is that you wish to write (ours is the Raspberry Pi Desktop ISO). Then insert your USB flash drive and Etcher will detect the drive and use it. The final step is to click Flash and supply your password to write the image to the flash drive.

Once the flash drive has been created, you can remove it from your computer, insert it into an older machine and power up. Depending on the machine, you’ll need to select the boot device, either via the BIOS or by pressing a key when booting – typically a Function key. On first boot choose to run Raspberry Pi Desktop from the USB flash drive, and use persistenc­e to save configurat­ions and files to the drive without touching the hard drive inside the computer. The boot process should take around a minute, and when completed you’ll see the familiar Raspbian desktop.

GPIO Expander

Something new that comes with Raspberry Pi Desktop is GPIOExpand­er. This tool enables a spare Raspberry Pi Zero to be used as a slave device, offering its GPIO for use with Scratch 2 and Python. This uses a USB/Ethernet gadget mode for the Pi Zero, which makes it possible for the Pi Zero to be configured as an Ethernet-enabled computer running a remote GPIO pin service, and means we can control the pins over a network connection – in this case a USB connection. To use this feature we need to open a Terminal in the desktop, and type the following. $ sudo apt update && sudo apt install usbbootgui

After a few moments all of the necessary software will be installed. Now take your Pi Zero and a micro USB lead. Insert the micro USB connection into the micro USB port marked USB on the Pi Zero. Insert the larger USB plug into a spare port on the Raspberry Pi Desktop computer. A few seconds later a menu will appear asking you to select the role for the device. Choose GPIO expansion board and click OK. A new dialog will appear and go through the steps to set up the Pi Zero with the necessary configurat­ion.

We now have only a couple more steps to take so that we can use GPIOExpand­er with Python 3. The first step is to

export the GPIO Zero pin factory, so that we use pigpio for working with the GPIO. This is completely transparen­t to the end user.

In a Terminal type the following. export GP I O ZERO_ PIN_ FACTORY= pig pio

The second and final step is to tell pigpio where to find the GPIO pins, and this is via our Pi Zero, connected as an Ethernet device (over USB). Here we use an IPv6 address to identify the Pi.

In a Terminal type the following. export PIG PI O_ADDR=fe80::1%u sb 0

Python3 project

So now let’s use our new GPIOExpand­er to create a quick test project using GPIO Zero to create a quick random sparkle LED project. We press the button and the Python code generates a random number used to blink the single RGB (red, green, blue) LED. The wiring for this project can be found in the download for this project.

To code the project, we shall use the Python 3 editor, found in the Programmin­g menu. Click File>New to create a new file and in there we shall write the code.

First, in the code we import from four libraries, from GPIO Zero we import the LED and Button classes, and from time we import the sleep function used to pace our code. Then we import the uniform function from random, which isused to generate a random float value. Finally, we import pause from signal, which prevents our code from ending. from gpiozero import LED, Button from time import sleep from random import uniform from signal import pause

Then we create four variables used to identify where each pin of our RGB LED and the button are connected to the GPIO. red = LED(17) blue = LED(27) green = LED(22) button = Button(10)

To control our RGB LED we shall use a function called sparkles . This function chooses a random float value between 0.1 and 3.0, which is then printed to the shell for debugging purposes: def sparkles(): x = uniform(0.1, 3.0) print(x) We next use a for loop that will iterate over a sequence three times. The code inside the for loop turns the red, blue and green LED on and off using sleep to create a blink effect thanks to x, our randomly chosen float value: for i in range(3): red.on() sleep(x) red.off() sleep(x) blue.on() sleep(x) blue.off() sleep(x) green.on() sleep(x) green.off() sleep(x)

Our last two lines of code are used to handle a button press. Here it waits to run the sparkles function when the button is pressed. Then we use pause to prevent the code from exiting. button.when_pressed = sparkles pause()

Save the code as sparkles.py to your home directory (/ home/pi/) and exit the Python3 editor. Now open a Terminal and to launch the code type the following: $ python3 sparkles.py Now press the button and watch the RGB LED light up! We have successful­ly re-used an old computer and a spare Pi Zero with which we can now create physical projects using many different languages.

 ??  ?? Using Raspberry Pi Desktop on an older, Core 2 Duo laptop, we have slightly more power and many of the default applicatio­ns that we find on the Pi are present.
Using Raspberry Pi Desktop on an older, Core 2 Duo laptop, we have slightly more power and many of the default applicatio­ns that we find on the Pi are present.
 ??  ?? Connecting the Pi Zero to the computer’s USB brings up this menu. Selecting GPIO expansion board and clicking OK will set up the Zero, ready for use.
Connecting the Pi Zero to the computer’s USB brings up this menu. Selecting GPIO expansion board and clicking OK will set up the Zero, ready for use.
 ??  ?? The circuit diagram for this project is relatively easy to build. Notice that we’ve replaced three separate LEDs with one RGB LED.
The circuit diagram for this project is relatively easy to build. Notice that we’ve replaced three separate LEDs with one RGB LED.

Newspapers in English

Newspapers from Australia