An Introduction to Raspberry Pi GPIO Pins (2024)

Originally published by Jun 18, 2020

In recent years, the Raspberry Pi has become popular largely as an inexpensive, compact Linux box for media and retro video games, as well as a network device. Some hobbyists go on to use their Pi these ways for years, all without knowing what the pins on the side of their device really do.

It’s these pins that hold the true power of the Pi. They can control homes, machines, new inventions, and even robots, so why is it so many people don’t know what they really are?

An Introduction to Raspberry Pi GPIO Pins (1)

Table of Contents

  1. What Do These Pins Actually Do?
  2. GPIO Pins Don’t Provide Much Power
  3. Connecting Your GPIO Pins to a Breadboard
  4. GPIO Pins With Special Uses
  5. Hardware PWM
  6. Serial Bus Pins
  7. Pull Up and Pull Down Resistors
  8. Using Software to Control GPIO Pins
  9. Let’s Use All This to Switch a Light On
    1. Connecting the Power Rails
    2. Connecting and Testing the Button
    3. Connecting and Testing the LED
    4. Controlling the LED With the Button
  10. Congratulations, You’re Using the GPIO Pins!
  11. Related Articles

What Do These Pins Actually Do?

These 40 (or 26, depending on your Pi model) pins are part of what’s known as the “GPIO Header”. Within this header, there are four main kinds of pin;

  • Power: These provide DC power at 3.3 and 5 volts
  • Ground (GND): These connect to ground, to close your circuit
  • DNC: This stands for “do not connect”, so don’t worry about them
  • GPIO: These can be set to either send, or receive control voltages

GPIO stands for ‘General Purpose Input/Output’, and it’s these pins that let the Raspberry Pi do its magic. This is because the pins have no specific function, and can be set to a dedicated purpose, such as controlling a signal.

An Introduction to Raspberry Pi GPIO Pins (2)

A GPIO pin set to output can provide either 3.3 volts, known as a HIGH signal, or 0 volts, known as a LOW signal. When set to input, it can read these same voltages.

GPIO Pins Don’t Provide Much Power

It’s important to remember that GPIO pins (and the 3.3-volt power pins) are meant to control and communicate with other components.

You can get about 51mA from all 3.3 volt pins combined, but you’ll want to take care when connecting; if your circuit tries to pull too much current through these 3.3 volt pins, you can fry the whole board.

The 5-volt power pins, on the other hand, give you all the power available from the power supply, minus the bit used by the Raspberry Pi itself.

Connecting Your GPIO Pins to a Breadboard

When you first start using these GPIO pins, it’s wise to use a breadboard. This makes it easy to build circuits without solder and to modify them.

If you’ve never used a breadboard before, familiarize yourself with the basics here:

A GPIO extension board also helps immensely. This connects the GPIO header via a ribbon and places the pins directly on the breadboard, in a clearly labeled manner.

It requires some real estate though: 20 rows each side of the breadboard. On a small board, that’s nearly the whole thing! A breadboard with 40 rows or so leftover gives plenty of space for beginner projects.

GPIO Pins With Special Uses

Every GPIO pin can be set to send and receive HIGH and LOW signals. Some have special uses too.

We won’t delve deep here; it’s just good to know what’s there.

Hardware PWM

GPIO pins output either 3.3 or 0 volts: a HIGH or LOW signal. Pulse width modulation, or PWM, is a way of simulating the range of voltages in between by flickering the pin on and off rapidly.

This isn’t a true analog signal, but it’s fine for something like dimming an LED. It will flicker faster than you can see and simply appear dimmer.

You can also use a low pass filter to smooth a PWM into an analog signal. This can be used for analogue audio, if you aren’t fussy about quality. It’s fine for a doorbell or toy.

You can generate a PWM signal from any GPIO pin using software, but the operating system juggles this with other tasks, so this signal can jitter.

Hardware PWM is available on GPIO pins 18 and 19. Hardware PWM and the headphone jack use the same circuits, so shouldn’t be used simultaneously.

Serial Bus Pins

If you take a look below at the diagram (known as a Raspberry Pi ‘Pinout’) you’ll see that some pins are I2C, SPI, and UART serial. These are serial bus protocols that can be used to send and receive data from other components.
An Introduction to Raspberry Pi GPIO Pins (3)

You can combine these with a digital-analogue converter, or DAC, to output an analogue signal. This can be preferable to PWM for high quality audio, or to control many components.

Pull Up and Pull Down Resistors

Often you will want your Raspberry Pi GPIO pin to read the position of a button or a switch. That’s easy to do by wiring it so that it closes a circuit attached to the control voltage to read HIGH, or to ground to read LOW.

The problem is that when this circuit is open and nothing else is attached to the pin, it might return any value. This is known as “floating,” and it’s extremely unhelpful.

You can prevent floating with “pull up” or “pull down” resistors.

A pull up resistor is wired to your control voltage; when nothing else is attached, the pin will read HIGH. A pull down resistor is wired to ground; the pin will read LOW. Use whichever provides the opposite value to your switch or button.

You don’t need to wire these resistors into your circuit. They’re inside the Raspberry Pi already and you can control them from software.

Using Software to Control GPIO Pins

Among the easiest ways to control GPIO pins is by using the GPIO Zero library in Python. If you’ve written any Python before, you’ll pick this up easy.

If this is your first time using Python, you might want to do a few introductory tutorials first. If you don’t, the commands below will still work; you’ll just be less able to follow along. The web version of ‘Automate the Boring Stuff With Python’ is excellent and costs nothing.

GPIO Zero is installed by default on Raspbian Desktop images. If you are using Raspbian Lite or a different operating system, you may need to install it.

Let’s Use All This to Switch a Light On

Let’s have a go at turning on an LED! A job this simple doesn’t really require a computer, but we’ll involve the Raspberry Pi in the GPIO pins.

For this, you’ll need….

A Raspberry Pi with power supply and an SD card with Raspbian installedAn Introduction to Raspberry Pi GPIO Pins (4)
A breadboardAn Introduction to Raspberry Pi GPIO Pins (5)
A GPIO extension board (optional, but recommended)An Introduction to Raspberry Pi GPIO Pins (6)
An LEDAn Introduction to Raspberry Pi GPIO Pins (7)

You’ll also need some more general equipment, such as;

  • A resistor with a value somewhere between 220 and 1000 Ohms
  • A USB keyboard, or an SSH connection: something that lets you type commands
  • Jumper cables or wires
  • A pushdown button
Connecting the Power Rails

If you’re using the extension board, connect it to the Raspberry Pi and to the breadboard. Then attach the 3.3-volt power pin to the positive power rail running across the bottom of the breadboard, and the ground pin to the negative power rail.

Connecting and Testing the Button

Now add your button to the middle of the breadboard. Connect one pin of the button to a Raspberry Pi GPIO pin. I’m using 13, because it’s my lucky number.

Then, connect the diagonally opposite pin of the button to the negative power rail. When you push this button down, the circuit closes.

Finally, we need to tell the Pi to pay attention to this pin, so let’s open the Python interpreter. At the command prompt, type:

python3

Now at the interpreter, type:

from gpiozero import Button

If you get a message saying ‘ImportError’, make sure you capitalized it correctly. If it says ‘ModuleNotFoundError’, you need to install GPIO Zero.
Otherwise, it’s time to assign our pin to the button:

button = Button(13)

This Button class takes care of assigning the pull up resistor. Now let’s test that it works by typing the following lines:

while True:
if button.is_pressed:
print(‘Sweet, the button works!’)
break

Python is fussy about indentation, so be sure to copy the spaces too. Then press Enter again to run the loop.
This loop will run until someone presses the button, so press it. It should produce a message saying the button works. This means you’ve successfully built a simple circuit that sends a message to your Raspberry Pi. Hooray!
If this doesn’t work, check that everything is connected properly and try again.

Connecting and Testing the LED

The D in LED stands for “diode”, which means electricity only runs in one direction through it.
You will notice that one leg of the LED is slightly longer: this connects to positive. Here, that means connecting to the GPIO pin. I’m using pin 26, for no particular reason.
Place the LED in the breadboard, making sure that the legs are spaced horizontally so that you aren’t shorting the connection out. Now connect this positive leg to your GPIO pin.
An LED should be wired in series with a resistor, so attach one end of your resistor to the short leg of the LED, and the other end to the negative power rail. Resistors can go in either way around.

An Introduction to Raspberry Pi GPIO Pins (8)

Now let’s go tell the Raspberry Pi what’s going on. Type: Image: Pi & Breadboard

from gpiozero import LED
led = LED(26)

If everything’s wired correctly, you can switch it on and off with these commands:

led.on()
led.off()

Controlling the LED With the Button

Now that everything’s connected and you’ve checked they work, type:

button.when_pressed = led.on

Now press the button. The LED should switch on and stay on. Now type:

button.when_released = led.off

Press the button again; the LED should switch off when the button is released.

Congratulations, You’re Using the GPIO Pins!

While these principles might seem simple, they can form the basis of thousands of projects, maybe even your own. Just remember to be safe, and careful, but most of all… have fun!

Related Articles

Learn more about the form and functions of Raspberry Pi with our in-depth guides:

  1. What is Raspberry Pi? Part 1
  2. What is Raspberry Pi? Part 2
  3. The History and Uses of Raspberry Pi!
  4. Making the GPIO Easier With GPIO Zero
  5. Using Raspberry Pi GPIO Pins With the RPi.GPIO Python Library
An Introduction to Raspberry Pi GPIO Pins (2024)

FAQs

How do I remember my GPIO pins on Raspberry Pi? ›

GPIO pin numbering

These are the GPIO pins as the computer sees them. The numbering of the GPIO pins is not in numerical order, instead relating to the numbering on the CPU of the Raspberry Pi, so there is no easy way to remember them.

What are the GPIO pins of Raspberry Pi? ›

GPIO is an acronym for General Purpose Input/Output. A Raspberry Pi has 26 GPIO pins. These allow you to send and receive on/off signals to and from electronic components such as LEDs, motors, and buttons.

What can I do if I don't have enough GPIOs? ›

If you're stuck with just a few GPIO pins, you can accomplish a variety of input and output tasks with just a little bit of code using direct port manipulation.

Is a Raspberry Pi GPIO pin only capable of supplying about? ›

The Raspberry Pi's GPIO pins are only capable of supplying a low amount of power. If you try to power a component that requires a lot of power, you can cause permanent damage to the Raspberry Pi. In this Instructable, we aim to show you how to get around this issue by using a Transistor.

How many GPIO pins are there in Raspberry Pi? ›

The Raspberry Pi 3 has 26 GPIO pins, the rest of the pins are power, ground or "other".

Are GPIO pins the same for all Raspberry Pis? ›

A 40-pin GPIO header is found on all current Raspberry Pi boards, although it is unpopulated on Raspberry Pi Zero, Raspberry Pi Zero W, and Raspberry Pi Zero 2 W. The GPIO headers on all boards have a 0.1in (2.54mm) pin pitch.

What is the purpose of GPIO pins? ›

A general-purpose input/output (GPIO) is an uncommitted digital signal pin on an integrated circuit or electronic circuit (e.g. MCUs/MPUs) board which may be used as an input or output, or both, and is controllable by software.

What do you use GPIO pins for? ›

GPIO allows you to programmatically interact with the physical world by attaching sensors, relays, and other types of circuitry to the Raspberry Pi. Each pin on the board either has a predefined function or is designated as general purpose.

How do I know if my GPIO pins are working? ›

In the following example, a GPIO test is done in the output mode with Serial Wire Debug (SWD) controls.
  1. Write the GPIO pins connected with drive strength H0H1: ...
  2. Set the GPIO pins to the output mode: ...
  3. Write the GPIO pins to the wanted state. ...
  4. Measure the voltage in the GPIO pins, for example, with an oscilloscope.

Can you add more GPIO pins to Raspberry Pi? ›

Raspberry Pi hardware has a limited number of digital I/O pins. You can add 16 digital I/O pins by connecting a MCP23017 I/O expander chip to the Raspberry Pi hardware. Then, using I2C, you can use the additional pins as digital inputs or outputs.

How do I get more GPIO pins? ›

A good way to add GPIO pins is the I2C expander like the MCP23008 (8 pins) or MCP23017 (16 pins). 8 such devices can be added to the I2C bus giving 64 to 128 GPIO pins. Each pin has an internal pullup resistor that can be enabled and each pin has pin change interrupt capability.

Is GPIO pin input or output? ›

GPIO pins that are part of a GPIO port cannot be retrieved or controlled as individual GPIOPin instances. The GPIO port is configured for input, and is only readable. The GPIO port is configured for output, and is both readable and writable. A GPIOPort instance can be opened by a call to one of the PeripheralManager.

Does Raspberry Pi have 5V GPIO? ›

This works when using an Arduino's pins because they can output 5V, a Raspberry Pi's GPIO pins are however limited to 3.3V. Since the 5V GPIO pins on the Raspi are sufficient, but cannot be switched via software, my plan is to switch this with one of its GPIO pins, and then use that signal to control the IRL44N.

Does Raspberry Pi 4 have GPIO pins? ›

The Raspberry Pi 4 board has a GPIO header with 40 pins. This GPIO header is also the same for Raspberry Pi 3 boards, so this guide applies to both versions. GPIOs allow you to easily use hardware features and communication, directly from a computer – the Raspberry Pi microprocessor.

What library is used to access GPIO pins? ›

The libraries are Wiring Pi, Pigpio, Gpiozero, Rpi. GPIO. Each library is explained with a description, its main features, a code example on Python, and a code example in C if supported by the library.

Top Articles
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 5877

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.