How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (2023)

GPIO pins let your Raspberry Pi communicate with the outside world.

Beyond USB and HDMI connections, the GPIO pins let your Pi send eletrical signals to all manner of electronical components. This opens up a world of possibility.

Needless to say, I was eager to get started as I’m sure you are too, but I kept seeing these terse warnings around the internet:

“Be careful! Doing the wrong things can destroy your Pi!”

But what things exactly?

It was kind of absurd actually the way every explanation was something along the lines of, “don’t be dumb.”

I wasn’t planning on going bananas plugging all sorts of things into my Pi, but as a beginner, I didn’t know what was “dumb” and was right.

Well, I did some research (okay, a LOT of research) and created this tutorial where you’ll learn not only the basics of how to use the Raspberry Pi’s GPIO pins, but also a series of safety tips to prevent you from making one of these Pi-breaking mistakes.

What you’ll learn

In this tutorial, you’ll learn:

  • The available pins and their layout on your Raspberry Pi
  • Safety precautions for using the GPIO pins
  • How to power an LED light with a 3V3 pin
  • How to programmatically power an LED light with a GPIO pin

As an introductory tutorial, your final result (a blinking LED light) won’t be spectacular, but by the end, you’ll have a foundational understanding of how to use the GPIO pins.

A closer look at the GPIO pins

The original Raspberry Pi included 26 pins, but ever since the Raspberry Pi 2, it has included a 40 pin board.

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (1)

While they all look the same and we commonly refer to them as the “GPIO pins” collectively, only 26 of them are actually GPIOs.

GPIO stands for “general purpose in/out” and these pins are designed to be controlled with software i.e. code that you write. For example, you can use a simple programming language like Python to tell a connected LED to turn on and off.

There are also two 5V (volt) pins, and two 3V3 (3.3 volts) pins. These pins are unconfigurable and serve as constant power supplies.

In addition, there are eight ground pins used for completing your circuits. When using one of the power supply pins, you need to eventually connect back to one of the ground pins to complete your circuit or there will be no current.

Lastly, there are two EEPROM pins which are specifically used by HATS and not something you need to worry about using directly yourself.

Tallying up the pins, here’s what you’ve got at your disposal:

  • GPIO – 26
  • 5V – 2
  • 3V3 – 2
  • Ground – 8
  • EEPROM – 2

This diagram from the official Raspberry Pi website is a great reference for using the pins:

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (2)

While you’re learning to use the GPIO pins, it may be helpful to order a new case like the Argon ONE which includes the pin labels etched into the case itself (and it looks sick).

Now let’s talk a bit about safety before you light your first LED.

Safety tips for the GPIO pins

The Raspberry Pi was my first foray into circuits and electronics, so naturally, I had a lot of newbie questions. If that’s true for you too then I think you’ll find these tips helpful and demystifying.

(Video) Raspberry Pi LESSON 4 : Understanding and Using GPIO Pins

This is not a comprehensive collection of everything that could go wrong, but it covers just about everything you would reasonably deal with as a beginner. I expect that as you move onto more advanced projects doing things like adding external power supplies with higher voltage, you will continue to learn about keeping your Pi safe.

With that out of the way, let’s address the basics.

Can I electrocute myself?


The highest voltage coming from a Raspberry Pi pin is 5V. This voltage isn’t enough to hurt you or even give you a shock. Although there are many factors to consider, 50V is generally regarded as the point at which electrical power becomes dangerous.

This means you can safely handle the Pi and electrical components without fear of hurting yourself. It also means that using the Raspberry Pi safely is more about protecting your hardware than yourself, which brings me to the next point.

Keep your Pi off when making changes

As a rule of thumb, keep your Pi turned off while making new connections and moving jumper cables.

You don’t have to do this forever, but while you’re getting started it’s a good idea so you can review your connections before you supply your circuit with power. It’s easy to make a mistake your first couple of times using the pins.

And while we’re on the subject, an on/off switch like this makes life a little easier.

Don’t connect pins to each other

Never connect a pin directly to another pin.

This can potentially overload and destroy pins. For instance, the GPIO pins are only 3V3-tolerant so connecting directly to a 5V pin would overload them.

Even as a beginner, I can’t imagine why you would do this, but if you’re working with many jumper cables at once, it’s certainly possible to do so by accident, so it’s good to be aware of this rule.

Use resistors to limit the current

Resistors are used to reduce the power of an electrical current.

Connecting a 5V pin to a simple LED like the one used in this tutorial will cause it to draw more power than it can handle resulting in it burning out or bursting. A resistor can be added to control the current so that less power is delivered to the LED.

Understanding how much power you’re delivering and how much your components can handle is a key part of using the GPIO pins. Most tutorials you find online will have the calculations done for you, but it is helpful to understand the math yourself when making your own custom circuits.

This video tutorial by Khan Academy outlines the calculations very clearly.

Don’t connect motors directly to pins

Motors, in particular, should be powered by an external energy source and not one of the 3V3 or 5V pins. This is because moters create a phenomenom called a back EMF when turned off which results in a sudden voltage spike. This increase in voltage can overload and damage connected pins.

Pay attention to static electricity

After digging through message boards and articles about this topic, it seems that static electricity can damage your Raspberry Pi, but it is an unlikely occurrence.

If you know that your home has a lot of static electricity and you commonly get shocks, then you will want to discharge the static electricity from your body before handling your Pi. To do this, you can touch a door knob or a screw on a light switch.

With these safety tips in mind, let’s get to the hands-on stuff now.

How to use the GPIO pins

Powering an LED is a simple way to try out the pins. You’ll start by powering it with a 3V3 pin and then hook it up to a GPIO pin and write a few lines of Python to make it blink.

Required materials

To complete the steps in this tutorial, you’ll need:

(Video) How to Prevent Damage to Raspberry Pi GPIO Pins (Linux Signal Handling with Python)

  • A Raspberry Pi (I recommend a starter kit)
  • 3 FF jumper cables (female-to-female)
  • 1 LED
  • 1 resistor (200-1,000 ohms)

If you buy these items individually, you’ll end up overpaying or getting way more than you need (like 100 LEDs). It makes a lot more sense to buy an electronics starter kit.

One such example is the CamJam EduKit sold by The Pi Hut. It’s only £5 ($6.46) and includes everything you need for this tutorial. That said, I think it’s well worth the investment to get yourself the REXQualis Complete Starter Kit instead.

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (3)

It’s more expensive at $46.99 but man, opening this thing up felt like Christmas morning. You won’t have to keep reordering components if you get this bad boy and it’s packed with cool stuff like an infrared motion sensor, three kinds of motors, a remote, and all the basics like buttons, LEDs, and resistors.

Requirements and recommendations aside, let’s move onto step one of the GPIO pin tutorial.

Turn off your Raspberry Pi now before moving on.

Supply the LED with power

The first step is to connect the LED with a power source.

An LED is a type of diode (LED = “light emitting diode”) which means it’s a conductor that only allows current to flow in one direction, whereas the jumper cables are standard conductors allowing current to flow in either direction.

Grab your LED and take a close look at it. You’ll notice that one leg is longer than the other.

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (4)

The longer leg is called the anode which takes a positive charge and the shorter is the cathode. You need to connect the anode to a power supply so that the current will pass through the LED causing it to light up. If you connected the 3V3 pin to the cathode instead, the current would be stopped and the LED wouldn’t light up.

Take one of your FF (female-to-female) jumper cables and attach it from the anode of the LED to the first 3V3 pin on your Pi, as pictured here:

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (5)

The color of the jumper cable does not matter, but it is standard convention to use red for the positive charge (the “hot” wire) and black for the negative charge.

Despite what you might expect, turning on the Pi now wouldn’t cause the LED to light up because the circuit hasn’t been completed. The voltage supplied essentially pushes electrons through the circuit, but since they have nowhere to go, they don’t move which means no current. Imagine a river that isn’t flowing. There’s plenty of water but no flow/current.

We need to complete the circuit or “close the loop” to get a current going, and to do that, we will connect the cathode to one of the ground pins.

However, if you did connect the LED’s cathode directly to a ground pin, there is a chance you could burn out the LED because 3V3 is at the upper limit of what the LED can handle.

We need to add a resistor to our circuit to reduce the current so the LED doesn’t draw too much power and damage itself.

Connect a resistor

Resistors come in all sorts of values, but 220Ω (ohms) will work well. It’s also a very common value for a resistor and easy to find. This will reduce the current to 15mA (milliamps) which is perfect for an LED.

You don’t have to run the calculation yourself, but if you’re curious, I’ll share this video by Khan Academy again because I think it’s the best for teaching this concept.

Connect one of the FF jumper cables to the LED’s cathode and the 220Ω resistor now.

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (6)

We’re connecting the resistor between the cathode and the ground, but it actually doesn’t matter where you place it. The resistor is going to slow the current equally across the entire circuit whether you include it before or after the LED.

Take a look at this GIF from

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (7)

The electrical current flows like water in a hose. Adding a resistor is like putting a kink in that hose. The water doesn’t build up behind the kink causing the hose to expand wider and wider. Adding a resistor similarly doesn’t cause a build up of electrons in the copper wire. A kink in a hose slows down the flow of water on both sides of the kink and a resistor works the same way leading the flow of electrons (current) to slow down equally across the entire circuit.

(Video) Raspberry Pi Zero: GPIO Pins

With the resistor added to the circuit, the final step is to make a connection from the other end of the resistor to a ground pin.

If you want to learn more about currents, this video by The Engineering Mindset is really good.

Connect to a ground pin

To complete the circuit, connect a jumper cable to the resistor and one of the ground pins.

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (8)

All of the ground pins are connected, so they all function the same. There are multiple ground pins mainly for convenience

With the circuit complete, the current can now flow. Turn on your Pi and you should see the LED light up immediately.

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (9)

As long as the Raspberry Pi is running, this LED will receive power from the 3V3 pin.

This is a great first step, but we haven’t technically used any GPIO pins yet which allow for a lot more flexibility. With a GPIO pin, you can send 3V3 of power at timed intervals, and that’s exactly what we’ll do next.

How to program a GPIO pin

So far, you’ve had the LED attached to a 3V3 pin. Remove the jumper cable from the 3V3 pin and attach it to GPIO pin 17.

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (10)

You can leave the Pi running while you do this. Just make sure you’re moving the right cable.

The LED is going to stay off since GPIO pin 17 isn’t sending it any power right now. We’re going to program the pin to start and stop sending power to make the LED blink.

You can use whichever IDE you’re comfortable with, but I’ll use the mu editor in this tutorial which comes included in the Pi OS.

To open the mu editor, click on the start button, hover your cursor over Programming, and then select mu.

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (11)

In the empty file, add the following two lines of code:

from gpiozero import LEDled = LED(17)

The first line imports a library called GPIO Zero. This code library is already bundled with Pi OS, so importing it here simply makes it available for use in this file. GPIO Zero handles more complex parts of the code for us so we can write really simple commands to interface with the pins.

The second line creates a Python variable named “led” and sets it equal to an LED object. The LED object is provided by GPIO Zero and lets us run methods (functions) like on() and off() to turn an LED on and off. We also give the LED() object a parameter (17) to instruct it to use GPIO pin 17.

Next, add the following code to line 3:


Here we are running the on() method provided by the LED() object to instruct pin 17 to turn on a connected LED.

With this code added, click the Run button in the mu editor and the LED should turn on.

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (12)

Congrats! You just used code to turn on an LED with a GPIO pin.

Now what’s interesting is that if you click the Stop button in mu, you’ll notice that the LED doesn’t turn off. Why?

Well, when you ran the code, it executed once turning the LED on. When you turned it off, you simply stopped running the code which was already finished running anyways. Even though the script was stopped, you never commanded the LED to turn off, so it’s still receiving power.

(Video) How to use GPIO pins with Klipper

Try switching line three to the following and run the code again:

The LED should turn off right away and then stay off.

With just a few more lines of code, we can make the light blink on repeat.

Make the LED blink

To make the LED blink, we need to write a loop that will continue executing. There are many ways to accomplish this, but a simple approach is to use a while loop, like this:

while True:

The while loop checks if the expression supplied is true and if it is, it runs the code inside. After running the code inside the loop, it will re-evaluate the expression again, and so on. Since we’ve used True as the expression, it will infinitely loop since True is always, well, true.

Inside the while loop, we will turn the LED on and off.

The next thing we need is a way to pause time in between turning the LED on and off. Otherwise it will be turning on/off many times per second likely without any effect. We can sketch the code out like this:

while True: led.on() # pause # pause

I’m using a pound sign (#) to write comments which are like notes in the code that are human-readable and don’t actually execute.

To add the pausing functionality, we’ll turn to the time module provided by Python. The time module includes a very handy function called sleep() that allows us to delay code execution.

We can import the sleep function, like this:

from time import sleep

With the sleep() function imported, we can pause the code for one second, like this:


Now let’s put it all together.

Here’s how the completed file should look with the new import and the comments replaced with the sleep functions:

from gpiozero import LEDfrom time import sleepled = LED(17)while True: led.on() sleep(1) sleep(1)

Click the Run button again, and you should see your LED blink on/off every second.

It will continue to blink as long as the code is running because the while loop is set to run forever.

Breadboards make everything easier

One thing you may have noticed during this tutorial is that connecting the jumper cables directly to the LED and resistor is pretty janky. The resistor likely fell out once or twice while you were getting everything connected.

A more comfortable way to make these connections is to use something called a breadboard which looks like this:

How to Use the GPIO Pins on Your Raspberry Pi (with Safety Tips) (13)

I wanted to keep this tutorial as straightforward as possible which is why I didn’t involve a breadboard since they take some time to learn as well.

That said, you should use a breadboard for your future circuits because the connections are physically more secure, and it also enables you to make more complex circuits in an organized way.

(Video) Raspberry Pi Workshop - Chapter 2 - Use GPIO to Connect to the World Around You

You can follow the steps in this tutorial to get started now:

How to Connect a Breadboard to the Raspberry Pi 4 (Beginner’s Guide)

Thanks for reading, and if you’ve got some lingering questions about how to use the Raspberry Pi’s GPIO pins, please post in the comments section below.


How do I use GPIO pins on Raspberry Pi? ›

How to use the Raspberry Pi GPIO pins
  1. Prepare your Raspberry Pi. ...
  2. Prepare the extra hardware. ...
  3. Get to know the GPIO pins. ...
  4. Setting up the hardware part by using a 40 pin GPIO cable and a T-cobbler (1st option = our preference) ...
  5. Setting up the hardware part by using jumper cables (2nd option) ...
  6. Write the code.
Aug 11, 2021

How do you protect GPIO pins on Raspberry Pi? ›

Re: Protection GPIO pins

One way to protect the input pin is to add a diode between 3.3V and the data wire. If the voltage on the wire exceeds the max voltage it will be fed into the 3.3V line instead of breaking the GPIO input. You could also add another diode between the GND and the input pin.

How do GPIO pins work? ›

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. GPIOs have no predefined purpose and are unused by default.

Which function is used to control GPIO of pins on Raspberry Pi? ›


Here is a very simple standalone example that toggles an output pin on and off for 200 milliseconds, ten times. It also reports the level on input pin 31. If you put the commands in a file and make it executable, you can directly run it as a program.

What is the GPIO connector on Raspberry Pi? ›

The Raspberry Pi GPIO connector controls general-purpose input/output (GPIO) pins on a Raspberry Pi core device. This connector polls input pins at a specified interval and publishes state changes to MQTT topics. It also accepts read and write requests as MQTT messages from user-defined Lambda functions.

How to configure GPIO pin as input? ›

To configure a GPIO pin either as Input or Output, you have to use the pinMode() function. Once the pins is setup, you can read from an input pin using digitalRead() function and write to an output pin using digitalWrite() function.

How do you protect GPIO? ›

Each GPIO is protected by a 330ohm resistor to limit excessive current, and a zener diode to sink any voltage up to 5v meaning it is safe enough to use 5v logic sensors quickly and easily with your Raspberry Pi.

How do you power off a Raspberry Pi GPIO? ›

Press and hold GPIO17 button a little longer to initiate the shutdown command. You'll need to give it a few seconds for the Pi to shutdown. You'll notice the monitor disconnect first if you have one attached. At this point, you should be good to remove your finger off the button.

What are the most common functions of GPIO pins include? ›

The GPIO pins are one way in which the Raspberry Pi can control and monitor the outside world by being connected to electronic circuits. The Pi can control LEDs, turning them on or off, drive motors, and interact with many other objects.

What does GPIO means? ›

General Purpose Input/Output (GPIO)

What is the power of GPIO pins? ›

The operating voltage of the GPIO pins is 3.3v with a maximum current draw of 16mA. This means that we can safely power one or two LEDs (Light Emitting Diodes) from a single GPIO pin, via a resistor (see resistor color codes).

What is the command GPIO input used for? ›

The gpio command is used to access General Purpose Inputs/Outputs.

How do I protect my microcontroller pin? ›

With all of this in mind, the first attempt to protect the input pin is found within current limiting. The simplest protection mechanism is a current limiting resistor, as shown in Figure 6. The input resistor is sized so that the voltage drop across it does not affect the voltage at the microcontroller input.

What are the anti static precautions for Raspberry Pi? ›

Re: Advice on Anti-Static Precautions

Just make sure you discharge yourself by touching the metal work on something like a PC, which should be earthed. Avoid things that generate static like carpets, synthetic clothing.

What are the limitations of GPIO in Raspberry Pi? ›

The operating voltage of the GPIO pins is 3.3v with a maximum current draw of 16mA. This means that we can safely power one or two LEDs (Light Emitting Diodes) from a single GPIO pin, via a resistor (see resistor color codes).

Does Raspberry Pi have current protection? ›

The Raspberry Pi has a polyfuse to protect it against over current, which is caused by drawing too much power from the GPIO pins. The polyfuse won't protect the Pi from over voltage, unfortunately. The Pi is engineered to work at 5 volts, plus or minus 5% (4.75 - 5.25 volts).


1. [ Let's Pi ] GPIO Header, Safety and Our First LED!
(Nicholas Cullen)
2. A Button to Switch your Pi Safe On and Off
3. The Raspberry Pi's GPIO Pins
(Gaven MacDonald)
4. Raspberry Pi Power Options - 3 Ways to Power Your Pi or other SBC (MEHS) Episode 44
(Michaels Stuff)
5. Raspberry Pi Tutorial 20 - GPIO How to use a Push Button
(Alexander Baran-Harper)
6. Raspberry Pi - How to Handle GPIO Interrupts with Python 3
(Robotics Back-End)
Top Articles
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated: 26/08/2023

Views: 6449

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.