Press "Enter" to skip to content

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

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.

gpio pins
GPIO pins on the Raspberry Pi 4

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:

gpio pin layout reference
Image source | Download this graphic

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.

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?

No.

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:

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.

rayqualis starter kit
More images and reference

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.

led closeup

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:

connect led 3v3
If the pins used are unclear from the photos, please refer to the reference graphic above

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.

connect resistor led

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 build-electronic-circuits.com.

current flow
Image source

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.

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.

connect resistor ground

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.

led on

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.

connect gpio 17

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.

select mu editor

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

from gpiozero import LED
led = 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:

led.on()

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.

mu run code

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.

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

led.off()

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 
  led.off()
  # 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:

sleep(1)

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 LED
from time import sleep

led = LED(17)

while True:
  led.on()
  sleep(1)
  led.off()
  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:

breadboard

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.

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.

Get new post notifications

Subscribe to get new projects & tutorials in your inbox

    Unsubscribe at any time in one click.

    Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *