Hackster Classroom for Teachers and Parents: Making Connections – Part 2

Helping parents and teachers navigate hands-on learning. This episode: part 2 of connectors, with a focus on software and protocols.

Nonscriptum
3 months agoHW101
A connector

As we saw in Part 1 of this article, we get beyond just powering something from a micro:bit with an alligator clip or two, we need to wade gingerly into the ocean of connectors. To wire up an arbitrary sensor, motor or other peripheral to our micro:bit, Circuit Playground or Arduino microcontrollers, we need to know a few things about which of several standards the add-on conforms to, and whether we need to add more to our power supply. These connector names can seem to be drawn from a bowl of alphabet soup, but there is (some) rhyme and reason to it.

We will often refer in this piece to connections inside a computer. Once we get past the realm of alligator clips and banana plugs, we will be dealing with the same kinds of connections that engineers have to plan out when they design a new computer. We will separate these connectors by degree of customization and complexity into Easy, Medium, and Hard types of projects.

Easy mode

Even for relatively simple projects, it can be hard to pick appropriate electrical connectors. They come in all shapes and sizes, and some are easier to use than others. This difficulty is influenced by how common the connectors are, how consumer-facing they are meant to be, the space they need to fit inside of, and what level of customization is required. We categorize these difficulties broadly on a spectrum from easy to hard.

Some connectors are named after what they are used for, like USB connectors that allow us to connect an external device like a phone that communicates with the USB protocol to a computer. We think of these aseasy-mode connectors since they are not custom, have well-defined properties, and so on.

Some other connectors fall into this easy category. When or desktop computer is assembled, for example, there are numerous multi-pin connectors, most of which have a name that specifies what it is used for, while also specifying (or at least implying) what type of physical connector is used, and a standard arrangement of the pins within that connector.

USB is one example, and HDMI is another. Connectors like these are (almost) never used for other incompatible purposes. We can almost always plug a USB or HDMI connector into a matching port and be confident that it will work as expected, or at least not damage anything if it does not. If we use an HDMI cable to plug one monitor into another, we will not get any signal, but we also will not short something out and cause a fire. Some connections inside a PC, like the SATA connectors used for storage drives, are almost as user-friendly.

With the exception of a few "cursed cables" these are consumer-friendly and difficult to mess up. Examples of these cursed connectors include the extension cord with a male plug on both ends (which is dangerous and should not exist), or the USB A-to-A cable (which is not much better). XKCD has some amusing fictional examples at https://www.explainxkcd.com/wiki/index.php/Category:Cursed_Connectors.

Medium

Other connectors are a bit harder to specify, and we can think of them as medium difficulty. Inside a laptop or desktop computer, there are connectors that can be more difficult to distinguish. For example, there is usually a connector from the power supply to the motherboard with two rows of 12 pins. This uses a Mini-Fit connectormade by Molex that is hard to mistake for anything else. Incidentally, the "Molex connector" used to supply power to power some disk drives inside PCs are actually "Mate-n-Lok" connectors, which are not even made by Molex.

However, there are also smaller Mini-Fit connectors with two rows of four pins for CPU power, and for powering other devices like GPUs. Despite both being 2x4 Mini-Fit connectors, these are not compatible, and bad things will happen if we manage to plug one in instead of the other. These connectors can be difficult to distinguish visually if they are not labelled, but they have specific names and are keyed to prevent incorrect connections. With enough force, we can probably plug in the wrong one, but they are designed to make it difficult enough that we will realize we are doing it wrong.

Worse, though, are modular power supplies that use similar connectors on the power supply end of these cables. The arrangement of the wires in those connectors varies by manufacturer, despite using the same connectors. Modular power supply cables from another manufacturer will usually fit physically, but will not have the right wires in the right places. Luckily, these power supplies come with a set of matching cables, but it is important to avoid mixing them up with another brand.

Hard

When working with DIY electronics, we often have to work with hard-mode connections. These connections may be difficult to identify, and identical connectors may be used for multiple purposes. Even when we have the right connector, we might be connecting it to the wrong pins. This might be because the connector was not keyed and we plugged it in backwards, or one pin off from where we meant to put it.

Some connectors are keyed to prevent these errors, but these connectors are used for many different applications, and we might have the wrong pinout (more on pinouts in a moment). 2-pin JST SHconnectors, for example, are commonly used to connect small, single-cell lithium batteries, but there's no standard for which pin is negative and which is positive! There is a standard that a black wire is used for the low, or ground (GND) side of the connection, and usually red for the positive DC voltage, but not for which side of the connector each wire connects to. Sometimes there are markings on the board indicating which arrangement it expects, but not always. For things other than simple power connections, these tiny markings usually cannot provide all of the information we might need.

To make things worse, because these connectors are keyed so we cannot plug them in backwards, we cannot just plug it in the other way to make it work. With the right tools and good eyesight, it is often possible to rearrange the pins in these connectors. However, re-pinning a battery connector is hazardous because we cannot turn off power to the pins, which are easy to short together while they are removed from their housing.

Pinouts

When figuring out how to connect two devices, a key word to know is pinout. Searching Google for the name of a board or connector along with the word "pinout" is often the first step. This lets us locate diagrams (often color-coded) with labels indicating the names and capabilities of the device's various pins. You can see an example for an Arduino board here.

On a microcontroller board, most of the connections are usually made available in the form of header pins. If a daughterboard is designed to work with a particular microcontroller, the pinout will match, and we can just plug the two boards into one another (being careful to align them so we are not off by one pin). Arduino shields and Raspberry Pi "HAT" or "crust" boards are examples of daughterboards. These are relatively simple to use with the boards they're intended to work with, but will be difficult to connect to anything else.

There are also generic breakout boards that are not designed to be used with any specific board. To connect one of these to a microcontroller, we need wires, as we discussed in Part 1 of this article. Dupont connectors, which come in male and female variants, are designed to connect to header pins to create easily-customized wire-to-board connections (or wire-to-wire by plugging male Duponts into female Duponts). This offers a lot of flexibility, but also a lot of opportunities to make a mistake.

Many of these devices lack reverse voltage protection, and plugging a power connection into the wrong place (especially one above 5 volts) can easily damage something. These connections should be made with the power off, and double-checked before powering things on for the first time (often called a "smoke test", because the first sign that something was plugged in wrong may be smelling smoke shortly after applying power).

Most of the pins on a microcontroller are general-purpose input/output pins (GPIO), that send and receive "high" and "low" signals by turning a voltage on and off, but many devices use more sophisticated data protocols. While these protocols can (at least in principle) be implemented directly in a program by switching pins on and off (a process calledbit-banging), certain pins often include hardware support for common protocols like UART andI2C.

We should also note here that when connecting up devices, we will also need to find supporting software libraries that are compatible with both the device we are adding and the microcontroller library we are using.

UART

UART (Universal Asynchronous Receiver/Transmitter) is a serial data protocol that uses transmit (TX) and receive (RX) pins to trade information at a predetermined baud rate. It requires one device's TX pin to be connected to the other's RX pin, and vice versa. UART is often used in conjunction with a USB to serial converter (either as a separate module, a component on the circuit board, or within microcontrollers that support USB natively) to communicate between a microcontroller and a computer. In addition to TX and RX connections, we also need at least a GND connection to establish a common reference voltage between the communicating devices, and it is common for one device to also provide power to the other, for four wires in total.

Connections for programming a microcontroller using a USB to serial converter usually have a fifth wire connected to the microcontroller's reset pin. This allows the computer to reboot the microcontroller to talk to its bootloader, rather than to the program that we are trying to replace. Without this fifth wire, a carefully-timed press of the reset button may be required as part of the programming process. If our microcontroller has a USB port, we probably do not need to worry about this.

IC

I2C (pronounced “i squared c," which stands for inter-integrated circuit) is another serial data protocol, but it works a little differently. It uses a serial clock (SCL) pin controlled by the microcontroller and serial data (SDA) controlled by whichever device is sending data at the moment. Multiple I2C devices can share a bus as long as one is the controller and each target device has a unique address.

I2C is designed as a bus for connecting multiple devices on the same circuit board, but it is not uncommon to see modules connected using short wires using the I2C protocol. There are now several competing (and somewhat interoperable) standards for this, as summarized at https://www.cable-tester.com/i2c-pin-out/ :

  • Sparkfun has a standard called qwIIC that uses 4-pin JST SH (1 mm pitch) connectors for I2C and power connections at 3.3V.
  • Seeed Studio has a standard called Grove that uses 4-pin JST PH (2 mm pitch) connectors 3.3V or 5V for a number of protocols, including I2C.
  • Adafruit uses both STEMMA connectors, which are pin-compatible with Grove, and STEMMA QT connectors that are pin-compatible with qwIIC, but may use 3.3V or 5V

These standards all use the same pin order (at least for I2C), so they are easily adapted to one another. Another company, DFRobot, uses the same 4-pin JST PH connector for I2C with 3.3V or 5V, but with a completely different pinout. Arduino has adopted the qwIIC standard for their Modulino product line.

There are other protocols that microcontrollers commonly support. Serial Peripheral Interface (SPI) used to be about as common as I2C, but it has mostly fallen out of favor except for a few specific applications, with some SPI devices even being adapted to be controlled using I2C instead. Other devices, like addressable LEDs, have their own custom protocol.

PWM

A simpler way of communicating with another device is by using Pulse-Width Modulation, or PWM. A PWM signal consists of a voltage that is rapidly turned on and off, many times per second, which can be used in a couple of different ways. The width of the pulse refers to how long the voltage is turned on before turning it back off (or sometimes vice versa).

While any output pin can, in principle, be used to produce a PWM signal, like other protocols, they often require precise timing that would make it difficult for our program to do other things at the same time. Most microcontrollers have special hardware built in that allows clean PWM signals to be generated in the background. This functionality is often tied to specific pins, and a pinout for our board will tell us which pins have this capability.

Depending on the device generating the pulses, the on-time, off-time, or the total cycle time may have a fixed value. The term duty cycle refers to the fraction of the cycle time that the voltage is on, while it is off the rest of the time. Lowering this duty cycle lowers the power output without changing the output voltage, which works well for things like dimming an LED. This also enables rapidly switching transistors on and off to control the power output to more power-hungry devices like motors.

Inexpensive motor drivers built around a component called an H-bridge take a PWM signal to control the speed (technically the power) of a brushed DC motor, and use a second pin that reverses the motor direction when voltage is applied.

More sophisticated controllers called ESCs (Electronic Speed Controllers), as well as the controllers built into hobbyservo motors, are also controlled using PWM, but the signals are interpreted differently. These controllers measure the length of each pulse to control the speed or position of the motor. The controller expects to receive a pulse between 1 and 2 milliseconds long, about once every 20 milliseconds. The length of this pulse specifies an angular position for servo motors, or a speed for ESCs.

Analog and digital

Any GPIO pin can typically produce a digital signal by turning a voltage on and off. These are logic-level voltages, typically 3-5 V. While these voltages can directly power some small components like LEDs, the current is very limited, and other circuitry is needed in between to power larger devices.

GPIOs can also act as inputs, reading a voltage supplied by an external device. If we want to know whether a button is being pushed, for example, we can connect one side to GND, and the other to a digital pin. When the button closes the connection, the GPIO pin will see zero volts, but what happens when it is disconnected? This is a state called floating, and the pin's reading is unpredictable in this state.

To reliably read this state, we need a positive voltage (preferably exactly the same level that is powering the chip and being provided by its outputs, but there's usually a bit of leeway) that is present when the button is disconnected. Some switches have an extra pin that they connect to when not pressed, but we do not actually need that when connecting to a GPIO. What we want is a pull-up resistor. As its name implies, this resistor pulls the voltage up when the switch is open. The resistor provides a weak connection to a voltage, and connecting the switch provides a stronger connection to GND, making the pin read as zero volts. Since some current will flow through the resistor in this state, resistors between 1 and 100 kiloohms are usually recommended. Most microcontrollers have internal pull-up resistors (and less commonly, pull-down resistors) that we can activate using the software running on the microcontroller.

In addition to digital input, some pins are also capable of reading an analog voltage. Some sensors produce analog voltage outputs that can be read this way. These are generally built around a component with a variable resistance.

  • A potentiometer is a variable resistor that we can control directly, often by turning a knob.
  • Aphotoresistor is a resistor that changes its resistance in response to the intensity of the light on it.
  • A thermistor works similarly to sense heat.

These generally need to be paired with a fixed resistor to form a voltage divider. A voltage divider lets us read an intermediate voltage between two resistors, one of which is connected to GND and the other to a positive voltage. What voltage corresponds to a given temperature or light level will depend on the value of this other resistor, so we need to choose one of an appropriate value to detect light or heat in the range of values that we care about.

To use these devices, we will also need to find supporting software libraries that are compatible with the microcontroller library we are using. For example, if we are using MakeCode with a micro:bit, we will need to find a MakeCode extension that supports, say, a thermistor. Some hunting around online may be required, as well as calibration.

Next steps

In this two-part series, we have covered the hardware and software of connecting up some representative extensions to our microcontrollers. Obviously this is a very large topic (more or less, the entire electronics industry!) and we were only able to give a few examples to get started. For more, check out projects on Hackster.io and elsewhere for ideas, or search on what function you are trying to implement (such as “soil moisture monitoring”, a perennial favorite). Read specifications carefully and be sure that the hardware pieces you are buying are compatible with each other. Finally, talk to us on the Hackster Discord channel (use this invite if not in the group already) in the #-educator-parent topic and let us know how it is going!

Nonscriptum
The creative partnership of Joan Horvath & Rich "Whosawhatsis" Cameron, creating content to teach 3D printing, electronics, math and more.
Latest articles
Sponsored articles
Related articles
Latest articles
Read more
Related articles