banner GPIO pin - Axel Richter/Unsplash

What are GPIO Pins Used for on Raspberry Pi? (Examples)

If you click our links and make a purchase, we may earn an affiliate commission. Learn more

The GPIO pins are probably the most confusing part of a Raspberry Pi for beginners. I remember wondering what all those tiny pins were even for. I’ve tested plenty of projects over the years, so I’ll explain how GPIOs work and share a few useful examples to get started.

The Raspberry Pi has 40 GPIO Pins that can be used for Input and output. These can be used to interface with various devices, such as sensors, switches, and motors.

In this article, I’ll talk about the different ways you can use the Raspberry Pi’s GPIOs. I’ll also give some examples of project ideas or use cases that show how useful they are.

If you’re feeling lost in all the Raspberry Pi jargon, I’ve got something to help you out. I’ve created a free glossary that explains all the essential terms and abbreviations in a way that’s easy to understand. It’s a great resource to have by your side. Get your free copy here.

Turn Things On and Off

From blinking LEDs to controlling your home.

The most basic thing we can do with the GPIO on a Raspberry Pi is turn things on or off. To put it simply, each GPIO pin can be in one of two states: On (High) or Off (Low). We can control the state of each pin using a program or script.

But what do we mean when we say “On” or “Off”? These are just two different voltage or power levels; electronic sensors and components can behave differently depending on the voltage applied.

If you are still unclear and want to learn more about how the GPIOs work in depth, please read this article: Getting Started With GPIO Pins on Raspberry Pi (Beginner’s Guide).

As a basic example, let us consider a simple circuit shown in the diagram below:

In this circuit, we have connected an LED to GPIO 16 of our Raspberry Pi through a resistor. We can now write a simple Python program, as shown below, and run it to turn this LED ON.

Something not working as expected?
You can get answers from real experts in minutes.
Get help with your setup
from gpiozero import LED

# Initialize LED on GPIO 16
led = LED(16)

# Turn LED ON
led.on()

If you are confused by basic electronic components, you can read our article on Raspberry Pi GPIO Basics: Components You Need to Know.

As you can see, by simply executing a single command, we changed the state of one of our GPIO Pins from off to on. We can use and apply this simple principle to more practical use cases as well.

However, one fundamental limitation of Raspberry Pi GPIOs is that they can only drive low-voltage (LV) loads, such as LEDs. To drive high-voltage (HV) loads, such as lights or fans, in your home, we need a way to convert the LV signal to an HV signal.

The actual circuit for such a signal transformation is relatively challenging for beginners. Luckily, prebuilt relay modules are available.

We can use a relay module like this and wire it to our Raspberry Pi. We can then use the relay in place of a traditional switch (as shown in the diagram below) to convert your normal fan into a smart fan.

You can replace the fan in this example with any device you want to control via your Raspberry Pi’s GPIO.

This kind of circuit enables you to automate devices in your home. You can schedule them to switch on and off at specific intervals or based on a specific sensor input. Turning your dumb devices into a smart home device.

Detecting Button States

Turning button presses into actions.

The opposite of turning things on and off with GPIO is using GPIO to sense whether something is on or off. GPIOs, in addition to serving as outputs, can also be used as inputs. This way, we can check in a program whether a Pin is in the On (High) or Off (Low) state.

We can see how the Raspberry Pi reads input via its GPIO using a push button as an example. The connection diagram is shown below:

In this example, we have connected a push button to GPIO 18 of our Raspberry Pi. Now we can use a simple program, such as the example shown below, to read the state of this push button.

from gpiozero import Button
from signal import pause

button = Button(18)

button.when_pressed = lambda: print("Button pressed")
button.when_released = lambda: print("Button released")

pause()  # keeps program running

As you can see from a simple Python program, we can read the state of our GPIO, which can then be translated into the button’s state, i.e., whether it is pressed.

If you are struggling to understand how to write programs to control GPIOs, we have a guide on the GPIO Zero library that can help.

You can use this push-button input as the starting point for taking action. For example, we can also connect an LED to our Raspberry Pi and turn it on when the button is pressed, as shown in this video

Recommended next step
Master Raspberry Pi in 30 Days

If you want a clearer path than jumping from tutorial to tutorial, my book gives you a step-by-step roadmap to really understand your Raspberry Pi.

Check the book here

This example is obviously an oversimplification. You can do much more complex tasks, such as using the push-button input as the starting point for your Raspberry Pi script or as controls for a game you are making.

Want even more ideas? I put together a free resource with over 75 Raspberry Pi project ideas, each with a quick description, tutorial link, and hardware requirements. Whether you’re just starting out or looking for something to do this weekend, this list will keep you busy for a while. Just click here to get instant access.

Talk to Sensors and Measure Things

Reading real data like temperature and humidity.

In addition to simple inputs and outputs we have discussed thus far, GPIO can also be used for more complex communication with digital sensors.

Some practical examples of this capability include using an IR or motion sensor to detect movement. Your Raspberry Pi can use this sensor to detect movement and alert you accordingly. Sensors such as HC-SR501 are commonly used for this.

You can wire this sensor to your Raspberry Pi, and whenever the sensor detects motion, it will drive the corresponding GPIO Pin High. You can then read this High state in a Python program (as we did for the pushbutton) and use it to trigger actions, such as alerting you.

This kind of setup can be used in a security system or to monitor your kids or pets movements around the house.

Another option is to combine this with our previous example of controlling things. We can design a program that detects specific movements (e.g., our feet when we wake up) and uses them to switch on a device (e.g., the lights in our room).

The ability to get input from physical actions and control different electrical and electronic devices enables us to develop many project ideas. We can use this type of sensor with our staircase lights or to switch off our fans and lights when no one is in the room, conserving electricity.

While the IR Sensor behaves like a push button, sensors like DHT22 Pressure & Humidity Sensor need to communicate more than just a single bit. They need to communicate more complex data, such as exact temperature and humidity in the environment.

To do so, such sensors usually serialize the data using a specific protocol. By serializing, these sensors transmit data using a single GPIO pin.

Some sensors, such as the MPU-6050 Accelerometer & Gyroscope sensor, even require two-way communication, in which the Raspberry Pi sends instructions and the sensor responds accordingly.

In fact, many sensors can be interfaced with your Raspberry Pi via the GPIO pins.

Naturally, since these sensors use a specific communication protocol rather than simple high- and low-level signals, they require specific libraries. For example, for DHT22, you can use the adafruit-circuitpython-dht Python library.

These sensors generally provide significantly more advanced functionality. For example, you can use a DHT22 to monitor your environment’s temperature and humidity and, based on the sensor readings, control the Air Conditioning system.

Alternatively, you can use the MPU-060 Accelerometer & Gyroscope sensor in a robotics project to make your Raspberry Pi aware of its orientation and movement.

One really cool project I like is using the DHT22 Temperature & Humidity sensor along with a Light Sensor to create my own automatic gardening/greenhouse-controlled environment. Where watering, ventilation, and light levels are carefully controlled based on these sensors.

Once you realize how many sensors can be interfaced with a Raspberry Pi, it will truly open your horizons on what a Raspberry Pi can do for you.

Control Motors and Build Moving Projects

From simple mechanisms to full robots.

One of the use cases where I most commonly use GPIOs is for robotics-related projects. In robotics, a Raspberry Pi can act as the robot’s brain, receiving information from multiple sensors and, based on that information, controlling motors that drive wheels or arms.

There are numerous robotics kits available. For example, I particularly like this 4WD Robotic Car Kit.

This kit is easy to assemble and a great place to start for beginners. It uses an ultrasonic distance sensor, a line-tracking sensor, and a camera.

Projects like these can be extremely educational and fun. Below is a video of my son and I playing with the car that I made:

Another cool robotics kit that can be really fun is this ArmPi Robotic Arm Kit. You can use this kit, along with the included Raspberry Pi 5, to create robotic arms that are both fun and productive.

Once you really get accustomed to Raspberry Pi GPIOs and robotics, you can take it one step further and build your very own Raspberry Pi Drone. It is actually easier than you might think.

Quadcopter frame kits are readily available on Amazon. You can then combine this with a lightweight Raspberry Pi Zero or Raspberry Pi Pico, and sensors of your choice to make your very own FPV Quadcopter.

You can use Tim Hanwick’s Scout Flight Controller Program or make your own to control your quadcopter.

In fact, there is an ever-growing community of Raspberry Pi enthusiasts who are more than willing to help you with your Robotics projects. You will find detailed articles online that will guide you through every step of developing your own robot.

Additionally, many robotics kits available are specifically targeted towards beginners and are a great starting point. Overall, this can be a great hobby that not only provides valuable learning opportunities but also a lot of fun once you get the hang of it.

Communicate With Other Electronics

Working together with microcontrollers like Arduino.

One niche, albeit a practical use of Raspberry Pi GPIOs, is to communicate with other electronic devices. One setup I often use in my designs involves pairing a Raspberry Pi with an Arduino Nano / Raspberry Pi Pico.

This combination allows the Raspberry Pi to manage the more complex tasks of my project, while the Arduino handles the interfacing with analog circuits or unsupported sensors.

However, to achieve this, I need a way for the Arduino and Raspberry Pi to communicate. You can use the Tx and Rx pins of the GPIOs to establish reliable communication.

This type of setup can help you combine the capabilities of multiple microcontrollers or development boards, and enables you to interface with sensors that you traditionally cannot with your Raspberry Pi.

Similar to Arduino, you can also use similar methods to establish communication between a Raspberry Pi and a Raspberry Pi Pico. In addition to UART serial communication, the Raspberry Pi supports I2C and SPI communication.

Several modules utilize these communication protocols and can be interfaced with your Raspberry Pi. For example, you can connect your Raspberry Pi to a 1602 LCD panel via I2C and use it to display important messages.

Similarly, you can connect a 1.8-inch color LCD display to your Raspberry Pi via the SPI interface and use it to display graphics.

In fact, many sensors and modules use these communication protocols. For example, the MPU-6050 Accelerometer & Gyroscope sensor we mentioned earlier also uses SPI communication.

The advantage of these advanced communication protocols is that a lot of data can be transmitted using only a few GPIO pins. Therefore, wiring for these modules is relatively straightforward.

And since these are pretty much universally accepted communication protocols, you can communicate with almost any electronic device or development board utilizing the GPIO on your Raspberry Pi.


🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Still stuck after following this guide? Drop your question in the RaspberryTips Community — real Pi users answer fast. Post your question here.

Add Features With Expansion Boards (HATs)

Add complex features without complex wiring.

Another neat thing you can do using GPIOs is to use HATs (Hardware Attached on Top) with your Raspberry Pi. HATs are prebuilt PCBs or Electronic Circuits that can be conveniently attached to your Raspberry Pi to enhance its functionality and capabilities.

There are several HATs to choose from, each geared towards a different capability. For example, you can use an M.2 NVME SSD HAT to add an NVME SSD to your Raspberry Pi 5.

This HAT uses the Raspberry Pi 5’s PCIe 2.0 port and is connected via a ribbon cable. However, it utilizes the GPIOs for power input and control signalling.

Another HAT that can be very useful for off-grid projects is the UPS HAT.

This HAT allows you to power your Raspberry Pi with 18650 Li-Ion cells, which can also serve as a backup power supply. This HAT uses the GPIO for power delivery and control signalling, such as battery percentage.

Most HATs are designed to be stacked on top of each other. However, to confirm compatibility, please consult the HAT manufacturer’s documentation. From my experience, most HATs are compatible with each other.

Another HAT I commonly use is this 3.5 Inch touch display HAT.

This HAT comes in handy for those occasions when you don’t have a dedicated display, keyboard, and mouse connected to your Raspberry Pi.

As you can see, HATs can be extremely useful and significantly enhance your Raspberry Pi’s productivity. There are many HATs available for Raspberry Pi, and I have only scratched the surface in this article.

GPIOs are pretty much what sets Raspberry Pi apart from a Mini PC. GPIOs allow direct control of electronic components and power delivery; therefore, there are a ton of things you can do with your Raspberry Pi GPIOs.

In fact, in most of my Raspberry Pi projects, the primary reason for choosing Raspberry Pi over a cheaper motherboard or PC is the availability of GPIOs that let me natively interface with electronic components and circuits.

Not getting the same result?

Even when you follow every step, small differences in OS version, hardware or config can change the outcome. Instead of wasting time guessing, get help from people who have already fixed the same kind of issue.

Ask your question now
🔒 No risk. Cancel anytime.
  • Get help on your exact issue
  • Access step-by-step videos for tricky setups
  • Browse the website without ads

Similar Posts

Leave a Reply

Community members only
Comments are reserved for RaspberryTips Community members. Join the community to ask questions, get help, and interact with other Raspberry Pi users.

Join the community  or  log in