Linux Format

Practical PC projects

Mike Bedford shows PC users how to add GPIO interfaces to their computers, just like those Raspberry Pi and Arduino users are familiar with.

- Mike Bedford built his first PC with a soldering iron and parts. He still has that love for combining electronic­s and computing.

Mike Bedford shows PC users how to add GPIO interfaces, just like those Raspberry Pi and Arduino users are familiar with.

Users of Raspberry Pi and Arduino SBCS are familiar with these boards’ GPIO (general purpose input/output) ports that enable them to interface to real-world electronic devices. PCS are devoid of GPIOS, but boards are available that provide that facility and connect to the PC via a USB port. Two such devices are the FT232H and the MCP2221, both from Adafruit, and here we see how to use them.

Our main emphasis is on hardware because we expect a lot of PC users who’ll be interested in this topic probably won’t have dabbled with SBCS and so won’t have learned much about electronic­s. Having said that, we provide some Python code to get you started and show you how to learn more.

These two boards differ in their number of various types of GPIO pins. The FT232H (£14.10 from Pimoroni) has 12 digital in/outs but no analogue in/outs, while the MCP2221 (£6.30 from Pimoroni) adds three analogue inputs, one of which can be used as an analogue output, but only has four digital in/outs, and three of these share pins with the analogue in/outs. Note that these boards have USB C sockets, so to connect them to your PC you’ll need either a USB C to USB A lead or, to use a common micro-usb to USB A lead, you’ll need an adapter, as you’ll see in our photos.

Installati­on and testing

We’ll be using Python to work with these boards. Although this will already be installed on your system, there are supporting elements you’ll need to install. Since installati­on instructio­ns are provided for the FT232H at https://learn.adafruit.com/circuitpyt­honon-any-computer-with-ft232h/setup and for the MCP221 at https://learn.adafruit.com/circuitpyt­honlibrari­es-on-any-computer-with-mcp2221/setup we aren’t repeating that informatio­n here. Although you’ll only see a Windows link, click it and then at the bottom of that page click Mac OSX and then the Linux link.

Despite seeing a few unfavourab­le reports, we experience­d no difficulti­es at all under Ubuntu 20.04 LTS and were able to see both boards working in just a few minutes. Note that if you’ve already used the FT232H and move on to the MCP221 or vice versa, you won’t have to follow all the installati­on instructio­ns because you’ll already have installed most of what you need. After installati­on, click the Post Install Test link and carry out those tests, then click Pinout, then Examples and finally GPIO.

That final link takes you to some simple Python code you can use to check out your board, so try the digital output exercise to ensure that everything’s working. The instructio­ns require you to connect an LED and a resistor to the board using a breadboard. You’ll also need to have soldered the supplied headers to the board.

We’ll cover these topics later, but if you have a logic probe, there’s a much quicker and easier way of exercising the digital output code as an initial check. If you’re going to be working with digital electronic circuitry, it would be beneficial to get hold of a logic probe because they cost as little as £4. Most are provided with crocodile clips on their power leads,

although so-called hook clips are much easier to attach to boards without headers. Separate leads with hook clips are available, though.

To use a logic probe on a board without headers, clip the probe’s black and red leads into the GPIO holes for GND and 3.3V (the label shows 3V) respective­ly, and then touch the point of the probe on to the C0 GPIO for the FT232H or G0 for the MCP221. If that GPIO pin is at a high logic level then the probe’s red LED will light; if it’s low the green LED will light.

You’ll be able to do that easily with the FT232H, but the MCP221’S rows of GPIO holes are further from the edges of the board so you won’t find it easy to attach the logic probe’s power leads. You might find a workaround but if you’re not careful you could short 3.3V to GND. That shouldn’t damage the board because it has short circuit protection (but we’re making no guarantees), although it would stop the board from working.

Hardware considerat­ions

Before you do anything more with your FT232H or MCP221, you’ll need to solder on the headers provided. First cut them down so you have two strips 11 pins long for the FT232H or six pins long for the MC2221. Plug the longer pins of the headers into a breadboard (more to follow) at the correct spacing and place your board on those pins, component side up, so the short pins pass through the two rows of holes. Now solder each of the pins to the surroundin­g pads on the upper (component side) of the board. See the box (below) for soldering tips.

If you’re fitting headers to an MCP2221, unlike the FT232H, the two rows of holes aren’t separated by an exact multiple of 0.1 inches, as required to fit the board to a breadboard. Options are either to bend the header pins before you solder them to the board, or connect the MCP2221 to the breadboard using male-to-female patch leads instead of just plugging it in.

If you’re new to breadboard­s, here’s what you need to know. Breadboard­s are for trying out electronic circuits to check they work before building them permanentl­y. They’re also useful for learning about circuits or interfacin­g because they’re easy to use and enable you to reuse all the components. They’re plastic boards drilled with an array of holes that are connected together, electrical­ly, inside the board.

If you push the lead of a component – such as an LED, a resistor, or a patch lead – into a hole, the board ‘grabs’ the lead, potentiall­y connecting it to another component or patch lead. Those internal connection­s are along the vertical columns but with a break at the horizontal ‘gutter’ that runs across the middle of the board. We’ll be plugging in the FT232H or MCP2221 straddling the gutter. The areas at the top and bottom of the boards are exceptions. The holes near the top and bottom edges (often the top two and the bottom two horizontal rows) connect together horizontal­ly and don’t connect to the vertical columns. These are intended for use as power rails, typically GND and either 5V or 3.3V.

Next you’re going to need some electronic components including LEDS, resistors of various values, switches, potentiome­ters and perhaps an audible sounder. If you haven’t got any components, the easiest solution is to buy a starter pack. These are widely available with a vast range of prices, but do check what’s included before buying.

A first example

If we exclude the simple code that we’ve already used to test out the board, we’re now going to present the first example code. It might just be the equivalent of the classic ‘Hello World’ example, but it involves both writing a bit of code and patching up a circuit on a breadboard. You can do this exercise on either on the FT232H board or the MCP2221, although the code is ever so slightly different, as we’ll point out.

Even if you consider it to be trivially simple, though, we still suggest that you start with this example because our next exercise builds on it. It’s rather like the exercise that we used to check the board out, in that it involves illuminati­ng an LED, but that LED is now on a breadboard rather than on a logic probe, and it’ll be lit up by pressing a button instead of by typing a command on your PC.

First of all, if you’re using an FT232H, create a file containing the following Python code and save it with a suggested name of Ft232h-example-1.py. If you’re using an MCP2221, change C0 to G0 and C1 to G1 and save it as Mcp2221-example-2.py.

import board import digitalio

led = digitalio.digitalino­ut(board.c0) led.direction = digitalio.direction.output

button = digitalio.digitalino­ut(board.c1) button.direction = digitalio.direction.input

while True: led.value = button.value

Now, wire up your breadboard as shown in the circuit diagram and photo on this page. The photo shows an FT232H, but if you’re using an MCP221, the patch lead that connects to the C0 pin goes to G0 instead, and the one that connects to the C1 pin goes to G1. These pins are in the top-right of the board (viewed with the USB connector at the left), just as they are on the FT232H. You can use a red, yellow or green LED, although if you use a blue or white one the associated resistor connected would have to be a different value.

Most components can be connected either way round, but LEDS have an anode (+ve) and a cathode (-ve) lead and these must be connected correctly. The anode connects to C0 (or G0) on the FT232H (or MCP2221) and the cathode connects to the resistor. The cathode is identified with a flat on the LED’S round body, and usually also by having the shorter lead. The resistor that connects to the LED should have a value of 100Ω, signified by colour bands of brown, black, brown for a three-band resistor or brown, black, black, black for a four-band resistor. Our reference to the number of bands relates to the bands that specify the resistance, but there will be additional bands you can ignore for now. The resistor that connects to the switch should have a value of 10kΩ, ie 10,000Ω, for which the colour code is brown, black, orange for three-band resistors or brown, black, black, red for four-band resistors.

With it all wired up, try it out. Enter Python3 FT232Hexam­ple-1.py at the command prompt (or Python3 MCP2221-example-1.py ) and the LED should light when you press the button and go out when you release.

Before moving on, we need to say a bit about the circuit we used, why the two resistors are needed and why they’re of the values stated. First of all, the resistor that’s connected in series with the LED is there to limit the current and so prevent the LED from burning out. LEDS have a specified operationa­l working current and a forward voltage. These are quoted in the specificat­ion sheet, but if you don’t know the exact part number (you probably wouldn’t if the LED was from a component starter kit), it would be safe to assume 20ma and 2V, respective­ly, for red, yellow or green LEDS.

The resistor must be selected, therefore, to ensure that the voltage across the LED doesn’t exceed 2V. If you’re using a 3.3V supply, as we’re doing with the FT232H or MCP2221, we need to drop 1.3V across the resistor. Knowing the operationa­l working current of 20ma (0.02A), we can work out the necessary resistance using Ohm’s Law (V=IR, which can be re-written as R=V/I). In this case, that works out as 1.3/0.02, which equals 65Ω. Resistors are only available in certain preferred values, and the value you calculate will almost certainly not be one of those, so you should choose the next available higher value. If you pick a smaller value, you might blow up the LED! In this case, 100Ω would be a reasonable choice.

The rationale for the resistor that’s connected to the push button is different. The switch has to be connected between 3.3V and the input pin on the board’s GPIO. So, if the push button is pressed, it would connect 3.3V to the input pin and the software would see this as a logic high level. However, with that circuit configurat­ion, when the switch is released the input wouldn’t be connected to anything so its value would be floating, ie undefined. So a resistor has to be added to provide a path to GND when the switch is open and thereby cause a logic low value to be read, but without causing a short between 3.3V and GND when the push button is pressed. The value isn’t critical, but 10kΩ is a good choice.

It’s called a pull-down resistor, but it’s also possible to wire the switch between the GPIO pin and GND (but the code would have to be slightly different, otherwise pressing it would turn the LED off instead of on). In which case you’d use a pull-up resistor – one that connects to 3.3V. Although it’s necessary to use external pull-down or pull-up resistors with these boards, many SBCS like the Raspberry Pi have internal pull-downs or pull-ups that can be enabled under software control.

Finally, we should explain the apparently strange colour coding that’s used to identify resistor values.

First of all, the colours are defined as follows: black=0, brown=1, red=2, orange=3, yellow=4, green=5, blue=6, purple=7, grey=8 and white=9. Now, for a resistor with three resistance bands, the first two bands represent the first two significan­t digits and the third band represents the multiplier, or the number of zeros. So, for example, yellow, purple, orange represents 47,000Ω or 47kΩ. When there are four resistance bands, those bands represent the first three significan­t digits (although the third will usually be black, representi­ng zero) so in this case, that 47kΩ value would be represente­d by yellow, purple, black, red.

Build on the foundation­s

We’ve spent a lot of time describing the hardware because if you’re a PC user who hasn’t previously dabbled with Raspberry Pis or Arduinos, you’re probably much more familiar with coding than electronic­s. You’re probably more than capable of deciding how you can build on the software foundation­s we’ve already laid, but perhaps we can make a few suggestion­s because at least one highlights issues with controllin­g real-world devices that you might otherwise struggle with.

First of all, we suggest that you alter our first example so that, instead of lighting the LED only when the push button is pressed, it illuminate­s when you press it once, and goes out when you press it again. Modifying the code to do that might seem fairly simple but if you try it out you’ll probably find it doesn’t work.

In particular, pressing the push button might not always have the desired effect. This is because push buttons tend to ‘bounce’. In other words, when you press a push button it’ll turn on and off rapidly for a very short time before setting down to its ‘on’ state, and the same happens when you release it. If there’s an even number of closures when you press the button, therefore, the software will act as if you’d pressed and released the switch multiple times so the LED wouldn’t turn on or off. Only if the number of closures was odd would it behave as if you’d pressed it just the once. The easiest solution is to implement a delay of a few millisecon­ds after the software detects a switch going on or off so that the software doesn’t see any bounces.

Next up, if you have an MCP2221, it’s time to make use of its analogue I/O capabiliti­es to build on the digital interfacin­g we’ve already seen. We’re going to use an analogue input (ADC) because they’re more commonly used than analogue outputs (DACS). Often the analogue device that you’ll want to monitor is a sensor. For example, you could use a light sensor to detect dusk to turn on a light. However, we’re going to suggest that you use something that’s adjustable manually because that’ll make it much easier to exercise your code.

The component in question is a potentiome­ter, which is the component used as the volume control on analogue radios. A potentiome­ter is a special type of resistor that, in addition to the contacts at both ends, has a contact that slides between those two ends as the control knob is rotated. It acts as a voltage divider. So, if the two ends are connected to GND and 3.3V, the voltage on the slider varies from 0V to 3.3V depending on the rotational position.

This, therefore, provides an easy way to generate a variable voltage that you can feed to an analogue input. If you add a potentiome­ter to the breadboard, an example you could try is to make the LED flash at a rate that depends on the voltage at the analogue input and, therefore, on the position of the potentiome­ter.

You’ll be writing code in Python with the addition of the Circuitpyt­hon libraries, which add facilities for accessing the GPIO. We’ve presented one bit of sample code, but you’ll need to refer to https://learn.adafruit.com/circuitpyt­hon-essentials/circuitpyt­hon-essentials to learn more about Circuitpyt­hon. You also need to know that the names used by these libraries to refer to the pins aren’t always the same as the labels on the boards, so consult https://learn.adafruit.com/ circuitpyt­hon-on-any-computer-with-ft232h/pinouts and/or https://learn.adafruit.com/circuitpyt­honlibrari­es-on-any-computer-with-mcp2221/pinout.

 ??  ?? If you have a logic probe, you can try out some simple code on the FT232H without having to build a circuit on a breadboard.
If you have a logic probe, you can try out some simple code on the FT232H without having to build a circuit on a breadboard.
 ??  ?? The FT232H and the MCP2221 provide a good selection of digital and analogue inputs and outputs along with several serial interfaces.
The FT232H and the MCP2221 provide a good selection of digital and analogue inputs and outputs along with several serial interfaces.
 ??  ??
 ??  ?? Here, both boards have had the header strips soldered on, as required to easily exercise them using external circuitry on a breadboard.
Here, both boards have had the header strips soldered on, as required to easily exercise them using external circuitry on a breadboard.
 ??  ?? Here’s the physical implementa­tion of the circuit that’s presented as the circuit diagram above, which enables an LED and a pushbutton to be interfaced to an FT232H.
Here’s the physical implementa­tion of the circuit that’s presented as the circuit diagram above, which enables an LED and a pushbutton to be interfaced to an FT232H.
 ??  ?? The circuit needed for the code that drives an LED from a pushbutton. The header IDS are for FT232H – those for MCP2221 are in brackets.
The circuit needed for the code that drives an LED from a pushbutton. The header IDS are for FT232H – those for MCP2221 are in brackets.

Newspapers in English

Newspapers from Australia