Raspberry Pi GPIO (General-Purpose Input/Output) pins are the physical interface that lets your Pi talk to the outside world, allowing you to read sensors, control LEDs, motors, and other electronics. You connect wires and components to these pins and control them with software, making them the foundation of almost every Raspberry Pi project. This guide explains what the pins are, how they work, how to use them safely, and common pitfalls to avoid.
What Are the GPIO Pins?
All current Raspberry Pi models (from the Pi 2 onward, including the Pi 3, 4, and 5) come with a 40-pin header, often called the GPIO header. The original Pi 1 models had a 26-pin header, but the 40-pin layout is now standard. These pins provide two main functions: power and data.
- Power pins: Provide 3.3V, 5V, and ground (0V) connections. The 5V pins can supply more current (up to about 3A total from the USB-C or micro-USB power input), while the 3.3V pins are limited to about 50mA combined.
- GPIO pins: These are the programmable pins that can be set as inputs or outputs. They operate at 3.3V logic levels—a high signal is 3.3V and a low is 0V.
Each GPIO pin has a number (like GPIO17 or GPIO22) that you use in code, but the physical pin position on the header is different. For example, GPIO17 is physically pin 11. This numbering confusion is a common source of errors, so always refer to a pinout diagram.
How GPIO Pins Work
GPIO pins are digital: they can be either HIGH (3.3V) or LOW (0V). You control them by writing to the pin in software, or you read their state when they are connected to an external signal.
When a pin is configured as an output, the Pi drives it to either 3.3V or 0V. For example, to turn on an LED, you connect the LED (with a resistor) between a GPIO pin and ground, then set the pin HIGH.
When a pin is configured as an input, the Pi reads whether the voltage at the pin is above a threshold (around 1.8V) to determine if it’s HIGH or LOW. Many sensors output a digital signal—like a motion sensor that goes HIGH when it detects movement—which you can read directly.
Some pins also have special functions, such as I2C, SPI, and UART, which are communication protocols that allow the Pi to talk to more complex sensors like temperature probes, displays, or GPS modules. These are often labeled on pinout diagrams (e.g., SDA, SCL for I2C).
How to Choose the Right Pins
For simple projects, any GPIO pin works. However, there are a few considerations:
- Default state: Some pins have pull-up or pull-down resistors enabled by default, which can affect sensors that rely on floating states. Check the pinout for the default state.
- Special functions: If you plan to use I2C, SPI, or UART, you must use the dedicated pins for those protocols (e.g., GPIO2 and GPIO3 for I2C).
- PWM: Only a few pins support hardware PWM (pulse-width modulation) for dimming LEDs or controlling servo motors. On the Pi, GPIO12, GPIO13, GPIO18, and GPIO19 have hardware PWM.
- Interrupts: All GPIO pins can generate interrupts, so any pin can wake the Pi or trigger a callback when its state changes.
Common Mistakes and How to Avoid Them
Many beginners damage their Pi or components by making simple mistakes. Here are the most common:
- Using 5V on a GPIO pin: GPIO pins are not 5V tolerant. Applying 5V to a GPIO pin can permanently damage the Pi. Always use 3.3V logic or a level shifter.
- Drawing too much current: Each GPIO pin can source or sink a maximum of about 16mA (the total across all pins should stay under 50mA). Driving a motor or high-power LED directly will damage the pin. Use a transistor or motor driver.
- Connecting LEDs without resistors: An LED without a current-limiting resistor will draw too much current and burn out. Use a resistor (typically 220Ω to 1kΩ) in series.
- Shorting pins: Accidentally connecting a GPIO pin to ground or 5V can cause a short circuit. Always double-check your wiring before powering on.
- Wrong pin numbering: Using the physical pin number instead of the GPIO number in code is a classic error. Always verify which numbering scheme your library uses.
How to Control GPIO Pins in Software
There are several ways to control GPIO pins, but the most common is using Python with the gpiozero library, which comes pre-installed on Raspberry Pi OS. Here’s a simple example to blink an LED on GPIO17:
from gpiozero import LED
from time import sleep
led = LED(17)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
For more low-level control, you can use the RPi.GPIO library, but it is now deprecated. The gpiozero library is recommended for new projects. You can also use command-line tools like pinctrl (on newer Pi OS versions) or raspi-gpio for quick testing.
Safety and Best Practices
Working with GPIO pins is safe if you follow a few rules:
- Always power off the Pi before connecting or disconnecting wires.
- Use a breadboard and jumper wires for prototyping to avoid accidental shorts.
- Double-check your wiring against a pinout diagram before applying power.
- Never connect a GPIO pin directly to a 5V source, a motor, or a relay coil without a driver circuit.
- If you need to interface with 5V logic (e.g., an Arduino), use a level shifter or optocoupler.
For higher-power devices like motors or solenoids, use a separate power supply and a transistor or relay module. The Pi’s 5V pin can supply a limited amount of current, so don’t rely on it for heavy loads.
Alternatives to GPIO Pins
If you need more inputs/outputs than the Pi provides, or if you want to avoid the risk of damaging the Pi, consider these alternatives:
- I2C/SPI expanders: Chips like the MCP23017 (I2C) or MCP3008 (ADC) add extra digital or analog inputs.
- Arduino or micro:bit: These microcontrollers are more forgiving and can handle higher currents on their pins, making them good for learning or for driving motors.
- USB interface: For simple data acquisition, a USB-to-serial adapter or a USB data acquisition device can be used without touching GPIO.
- HATs and add-on boards: Many HATs (Hardware Attached on Top) provide built-in circuitry for specific tasks, such as motor control or relay switching, so you don’t have to build the interface yourself.
Frequently Asked Questions
Can I use a GPIO pin as both input and output?
Yes, you can change a pin’s direction in software at any time. For example, you might set a pin as an output to send a trigger signal, then switch it to an input to read the response. Just be careful not to have two devices driving the same pin at the same time, which can cause a short.
What is the difference between BCM and BOARD numbering?
BCM refers to the Broadcom chip’s GPIO number (e.g., GPIO17), while BOARD refers to the physical pin number on the header (e.g., pin 11). In Python libraries, you can specify which scheme to use. BCM is more common because it’s independent of the board layout, but BOARD is easier for beginners to follow a physical diagram.
How do I know which pins are safe to use?
All GPIO pins are safe as long as you stay within the 3.3V logic level and current limits. Avoid using the pins that are reserved for special functions (like I2C or SPI) unless you need them, but even those can be used as general I/O if you disable the special function in software.
Can I connect a 5V sensor to the GPIO?
Not directly. If a sensor outputs 5V, you must use a level shifter or a voltage divider to bring it down to 3.3V before connecting to a GPIO pin. Similarly, if you need to drive a 5V device, you should use a transistor or a logic-level converter.
Why is my LED not turning on?
Check the wiring: the LED’s anode (long leg) should go to the GPIO pin (through a resistor) and the cathode (short leg) to ground. Also, make sure you are using the correct GPIO number in your code, and that the pin is set as an output. If the LED still doesn’t light, test the LED with a 3.3V and resistor directly to confirm it works.
What is the maximum current I can draw from the 5V pin?
The 5V pin can supply whatever current your power adapter provides, minus what the Pi itself uses. A typical power supply is 2.5A to 3A, so you might have around 1A left for external components. However, it’s safer to use a separate power supply for anything drawing more than 500mA.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Mac Start Up Automatically After Power Failure?
- Expose Self-Hosted Service Securely Without Cloudflare Tunnel
- Nano Delivery for Functional Foods: What It Can and Can’t Do4.00
- Disable Apple Watch Turn-by-Turn Vibration While Driving
- Convert Play FLAC in iTunes: Full Guide
- Soda Foundation Charter: What It Means