GENERAL PURPOSE INPUT/OUTPUT (GPIO)

The purpose of this concept is to discuss one of the most important aspects of integrating your electronics hardware with your code. Specifically, we will explain how your code can "control" your circuit (GPIOs as Outputs) and how your circuit can "communicate" with your code (GPIOs as Inputs).

Introduction to GPIOs

In the early RaspberrySTEM projects, we wrote some basic code to print text to our Output Window and we built some simple circuits to light up an LED. While doing these types of projects is important to learn the basics of programming and electronics, the purpose of this kit -- and the part of programming and electronics that's really fun and interesting -- is getting your code and your electronics to work together.

In general, there are two ways that your code and your circuits can work together:

  1. Your software (code) can control your hardware (circuits);

  2. Your hardware (circuits) can communicate with your software (code).

The most common use for having your software control your hardware is to turn components on and off (like an LED or any other device that can turn on/off). And the most common use for having your hardware communicate with your software is to indicate that an event has occurred (like a button being pressed).

Understanding how this communication and control is implemented is the key to being able to develop software and hardware that work together to build valuable and interesting devices. As it turns out, there is a relatively simple mechanism for how to get our circuits and our code to work together, and it's called General Purpose Input/Output, or "GPIO" for short. GPIOs sit between your circuit and your software, and allow the two to interact:

In the next section, we'll give a brief overview of what GPIOs physically are, and then after that, we'll talk about how they work and how you can use them in your projects. Then, we'll use these concepts in most of our future projects.

What Are GPIOs?

GPIOs are specific pins (little metal "legs" on the bottom or sides of a computer chip) that provide a connection between a physical circuit and the software inside the chip. We make these connections to the computer chip available on the RaspberrySTEM CREATOR Kit by plugging wires into the Lid Connector Board holes.

The Raspberry Pi has 26 GPIOs, each of which are referenced by number (for example, GPIO17), and each pin (hole) on the Lid Connector Board is labeled with the GPIO it is connected to. So, if you want to hook up a component to GPIO17, you'll insert a wire from the component to the hole on the Lid Connector Board labeled "17".

GPIOs Come From the Raspberry Pi

The Lid Connector Board was designed to make connecting to the Raspberry Pi pins cleaner and easier. In fact, the whole purpose of the ribbon cable and the Lid Connector Board is simply to bring the GPIO pins (and other pins) from the Raspberry Pi to the RaspberrySTEM Lid, where they are easier for you to attach your components.

But, if you had a Raspberry Pi computer without the RaspberrySTEM CREATOR Kit, you'd still be able to get access to the Raspberry Pi GPIOs. The GPIOs are made available through the pins on the Raspberry Pi (the pins that the ribbon cable is attached to). If you were to remove the ribbon cable, you could connect wires directly to the Raspberry Pi GPIO pins and attach them to your components directly.

How Do GPIOs Work

While GPIOs all look the same, there are actually two different ways that they can be used in your electronics projects. We discussed above that there are two important uses for GPIOs -- allowing your software to control your hardware and allowing your hardware to communicate with your software:

A GPIO can do either of these two things, but (generally) not at the same time. You must decide beforehand whether a GPIO is going to be used to allow software to control hardware or whether it will be used to allow hardware to communicate with software. Once you make that decision, you will "configure" the GPIO to perform that task. By configure, we just mean that you will write a tiny bit of code that tells the GPIO which of the two functions it will perform.

When we configure a GPIO to allow software to control hardware, we say that we're "using the GPIO as an output":

And when we configure a GPIO to allow hardware to communicate with software, we say that we're "using the GPIO as an input":

Now that we have a basic definition of GPIOs and how they can be used as either outputs or as inputs, let's talk in more detail about how GPIOs work in each of these situations.

GPIOs as Outputs

The first part of our discussion will focus on how your code can control your circuit. To understand this concept, we'll need to think back to a previous discussion about how and why electric current flows through a circuit.

If you recall from our earlier discussion in INTRODUCTION TO ELECTRONICS, electric current will only flow through a circuit when there is a voltage difference between the beginning and the end of the circuit.

This is why, for our basic LED circuit, the LED will illuminate when we attach one side of the circuit to 3.3V power and the other side to ground. Like this:

Given that, it should also make sense that, if there is no voltage difference between the beginning and the end of a circuit, no current will flow. For example, imagine our circuit above, but instead of attaching the end of the circuit to ground, we attach it to 3.3V power, just like the beginning of the circuit.

The circuit would look like this:

Note: The symbol at the bottom of the image above is just another way to indicate power in a schematic -- this is how power is typically represented when it's at the bottom of a circuit.

As you can probably guess, in the circuit above, no current would flow through and the LED would not illuminate.

Now, imagine that we wire a circuit -- similar to above -- but where the bottom of the circuit has a switch. And, that switch can be toggled between connecting to power and connecting to ground:

GPIO Attached To Power (LED Off) GPIO Attached to Ground (LED On)

Based on what we discussed earlier, it should be clear that when the circuit is configured like the left-hand circuit above, no current is flowing and the LED does not illuminate. But, when the circuit is configured like the right-hand circuit above, current will flow and the LED will illuminate.

This is conceptually how a GPIO works. Using software, we can set the GPIO to either power or ground.

Here is a schematic representation of a GPIO within a circuit:

"Active Low"

In the electronics world, we often use the term "low" to refer to ground. You might hear an engineer say something like, "Tie that component low," or "Set that pin low." That simply means to connect the component/pin to ground.

It's interesting -- and sometimes confusing -- to think about the fact that in the circuits above, when the GPIO is set low (attached to ground), current is flowing and the LED is illuminated. And when the GPIO is set high (attached to power), current stops flowing. We tend to think of current flowing when it's attached to power, not ground. But remember, the other side of the circuit is still attached to power, and it's the voltage difference that creates current flow.

This is why you'll sometimes hear engineers use the term "Active Low." It indicates that the GPIO needs to be set low (to ground) for the circuit to be active (turned on).

GPIOs as Inputs

The first part of our GPIO discussion focused on how we could control our circuit from our code (GPIO as Outputs). This part of our discussion will focus on how our circuit can communicate with our code. This is important because we often want to trigger our code to do certain things or take certain actions based on input or changes coming from our circuit.

For example, our code could count the number of times a button is pressed in a short a period of time -- if the button is pressed once, have our code do one thing and if the button is pressed twice, have our code do something else. In this way, we've added "intelligence" to our button presses. When our hardware can communicate directly with our code, we open a world of possibilities.

So, how do we do this? By using GPIOs in inputs. Much like GPIO outputs work by simply setting a voltage on the GPIO pin to either ground (0V) or power (3.3V), GPIO inputs work by reading the voltage -- either 0V or 3.3V -- being sent to the GPIO pin by the attached component.

For example, a GPIO can be used to get the current state of a button or switch (pressed or released). Based on the current state, the software can do what we expect when the button/switch is in that state.

"State"

"State" is a common term used in electronics and just means the current condition or situation of a component or circuit. For example, in the case of a button or switch, state would refer to whether the button is currently being pressed or released. For an LED, state might refer to whether the LED was illuminated or not.

For a component to communicate with our code, instead of wiring the component directly into the rest of the circuit, we instead wire the component directly to a GPIO. Typically, a component wired to a GPIO input would either generate 0V or 3.3V and the GPIO input would read that voltage and communicate it to our code -- hence the code knows about the "state" of the component.

Buttons are a little bit different, as they don't generate a voltage. But, with a little bit of behind-the-scenes configuration, we can wire a circuit so that our button can be used as a GPIO input and we can read the state of the button based on the voltages generated.

This is what the schematic would look like for a button wired to a GPIO as an input:

This circuit works by defaulting the GPIO voltage to 3.3V (that's the behind-the-scenes configuration we mentioned above, but not shown in the schematic). When the button is unpressed (circuit open), the GPIO reads the default 3.3V. When the button is pressed, the circuit is closed, the GPIO gets pulled low (to ground), and will read 0V. This way, the code can measure the voltage at the GPIO and know if the button is pressed (0V at GPIO) or unpressed (3.3V at GPIO).

home