From playing games to running a Kodi entertainment system, there’s a lot you could do with a Raspberry Pi. But you can do even more things if you knew your way around its general purpose input-output (GPIO). Here’s a guide to get you on track with working with the Raspberry Pi GPIO pins!
What Are Raspberry Pi GPIO Pins?
The Raspberry Pi’s GPIO pins are those metal pins that stick out from one side, right at the opposite of where the HDMI and power connectors sit.
However, not all of those pins count as “GPIO.” Among them, 26 can be set into input and output logic pins. Those are the GPIO pins. The rest are power pins.
By programming these into either input or output pins, you can turn them into a logical computer that can read inputs and show outputs. For example, you can connect an input pin to a pushbutton circuit and an output pin to an LED so it’ll light up whenever the pushbutton is pressed.
Raspberry Pi Pinout
Before you can program each pin into inputs and outputs, you first need to know which pins are which.
Each pin on the Raspberry Pi is numbered from 1 to 40. If you look at the Raspberry Pi in such a way that the USB ports are pointing at the floor, the top-left pin should be pin 1. The pin to its right is pin 2, and the count continues until you reach pin 40 at the bottom right.
Now the following pins are not GPIO – they are power pins that either constantly charged to output a voltage (3v3 and 5V) or meant to receive voltage (Ground).
- 1 & 17 (3v3)
- 2 & 4 (5V)
- 6, 9, 14, 20, 25, 30, 34, 39 (Ground)
Normally, you can call these pins by their names. So if you were referring to pin 7 as a GPIO, you can just call it pin 7. This is called the BOARD numbering.
On the other hand, you can also call them by their individual numbering based on the peripherals processor chip. This is referred to as BCM numbering and is named that way because the chip that runs these pins belongs to the BCM family of processors.
Tip: do you know you can use your Raspberry Pi as a video conferencing station? Learn how to do so.
Set Input and Output Pins
You can program the Raspberry Pi’s GPIO pins with Thonny, which is the default Python editor that comes with the Raspberry Pi OS.
The following code sets pin 7 (GPIO 4) as an output pin and pin 8 (GPIO 14) as an input pin using BOARD numbering.
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) // Set BOARD numbering. GPIO.setup(7, GPIO.OUT) // Set pin 7 as an output pin. GPIO.setup(14, GPIO.IN) // Set pin 14 as an input pin. GPIO.output(7, GPIO.HIGH) // Make pin 7 output 3 volts. GPIO.input(8) // Reads whether there's electricity passing through pin 8. Returns either a True or False that you can use in an if statement. GPIO.output(7, GPIO.LOW) // Make pin 7 stop outputting 3 volts.
Explanation of the code
To work with GPIO pins, you’ll need to import the Raspberry Pi GPIO library. This is done by entering import RPi.GPIO as GPIO
.
After that, you’ll need to set the mode. Use GPIO.setmode(GPIO.BOARD)
to tell the MicroPython interpreter to read in BOARD mode. Or use GPIO.setmode(GPIO.BCM)
to read in BCM mode.
To set the pins, you’ll need to use the GPIO.setup(<pin>, <GPIO.OUT or GPIO.IN>)
function. It takes two arguments. The first one is the pin number based on the numbering mode you selected earlier. The second one is the state, whether you want to set it as an output pin with GPIO.OUT
or an input pin with GPIO.IN
.
Next, with output pins, you can set them to either high or low using the GPIO.output(<pin>, <GPIO.LOW or GPIO.HIGH>)
. Setting an output pin to HIGH charges it up to 3 volts. This counts as a “yes” or “1” in computer logic. LOW does the opposite, charging it down to near 0 volts.
On the other hand, you can read the values of input pins. If they receive 3 volts, they will register as HIGH and return a boolean value with GPIO.input(<pin>)
. Booleans are values that are either True or False. You can use these values in while loops and if statements to make even deeper logical stuff with them.
Connecting GPIO and Power Pins to Peripheral Components
The GPIO pins on the Raspberry Pi are known as male pins. That’s because they have metal bits jutting out instead of a pin tray like the Arduino Uno.
Most prototyping components come with male pins to make them easier to place on a breadboard. You can use either a male-to-female jumper wires to connect these on a breadboard or female-to-female jumper wires to connect them directly to the components, themselves.
Also, in most components, you’ll sometimes find an “S”, “IN”, or “OUT” marking right on their pins. This means that the pin directly next to them should connect to GPIO pins. The markings depend on the component – a lot of components don’t even name them like this because they have around 3 to 9 signal, in, or out pins on the board.
To know which pin should go where, you can look up their “datasheet”, which is the manufacturer’s advice on how to use the component and their expected behaviors. These are specific to a model or brand, especially when it comes to modules that fit a ton of other components inside.
Taking Care of Raspberry Pi GPIO Pins
GPIO pins are sensitive stuff. It’s easy to break them if you’re not being careful.
For one, you have to make sure that only electric sources that run with 3 volt logic connect to these things. Applying 5 volts into any GPIO pin is one way to break them.
Another thing that could break them is by connecting an output pin directly to an input pin without any resistor or component in between. While the voltage remains the same, the current flowing from the output pin can potentially damage your input pin.
As soon as you learn how to use these properly, you can do a lot of things with a Raspberry Pi. For one, you could connect a DC motor to one and make it run on wheels or act as a tiny electric fan. And if you add wheels, you could even control a Raspberry Pi robot wirelessly through SSH on a power bank! These GPIO pins extend the Raspberry Pi’s capabilities to far more greater things than they could without them.
Image credit: Unsplash
Our latest tutorials delivered straight to your inbox