Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 37 ยท GPIO and Python

GPIO Basics and Pin Numbering

Learn how GPIO pins work: digital high and low, inputs and outputs, BCM versus physical numbering, safe wiring with resistors, and Python libraries.

The GPIO header is where the Raspberry Pi stops being just a computer and becomes a tool for interacting with the physical world. A GPIO pin is a tiny electrical switch that you control from software. This lesson explains the ideas you must understand before you connect anything: digital signals, the difference between input and output, the two pin-numbering systems that trip up every beginner, and how to wire safely.

Digital signals

A GPIO pin is digital: at any moment it is either high or low. High means about 3.3 volts. Low means about 0 volts (ground). Programmers call these 1 and 0, True and False, or on and off. Nothing in between matters for a digital pin.

A pin works in one of two directions:

  • Output. Your program decides the level, and the pin pushes current out or sinks it. Use outputs for LEDs, buzzers and transistors that drive bigger loads.
  • Input. The outside world decides the level, and your program reads it. Use inputs for buttons, switches and sensors that give an on/off signal.

A pin can only do one at a time, and you can change it in code at any time.

Two ways to number the pins

This is the single biggest source of confusion. The same pin has two numbers:

  • Physical (board) numbering counts the pins on the header: 1 to 40, left to right, top to bottom.
  • BCM numbering uses the Broadcom chip's own names, such as GPIO17.

For example, the pin at physical position 11 is called GPIO17. Pin 12 is GPIO18. Modern libraries such as gpiozero use BCM numbers by default, so LED(17) means GPIO17 (physical pin 11). Mixing the two systems wires LEDs to the wrong pin, so always say which one you mean.

Same pin, two numbers

Physical 11 Position on the header
BCM GPIO17 Name from the chip
1 / 2

The stand-in converts between the two numbering systems with a lookup table. Use it to check your wiring before you plug in a jumper.

Python Stand-in: BCM and physical pin converter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Safe wiring rules

A GPIO pin is not a power outlet. It is fragile and can be damaged by mistakes:

  1. 3.3 V only. Never connect a 5 V signal to an input. Use a level shifter or a voltage divider.
  2. Low current. A pin should supply only a few milliamps. A single LED is fine with a resistor; a motor or relay coil needs a transistor or driver board.
  3. Use a resistor with an LED. Without it the LED can draw too much current and fail.
  4. Wire with the power off. Shut down the Pi before changing connections.
  5. Never short pins together. Especially 3.3 V or 5 V to ground.

To size an LED resistor use Ohm's law: R = (Vsupply - Vled) / I. For a red LED on 3.3 V with a forward voltage of about 2.0 V and a target of 10 mA, R = (3.3 - 2.0) / 0.010 = 130 ohms; use the next standard value up, 150 ohms, or the common 220 ohms for a safer margin.

Python Stand-in: LED resistor calculator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The blue LED shows a real limit: with a forward voltage of 3.0 V there is little headroom left from 3.3 V, so the calculation needs a small resistor and the LED will be dim.

Reading and writing a pin (real code)

On a real Pi, the library does the electrical work. With gpiozero, the easiest library, you write (read-only):

Python
from gpiozero import LED, Button

led = LED(17)        # output on GPIO17
button = Button(2)   # input on GPIO2

led.on()
print(button.is_pressed)

Behind it, current Raspberry Pi OS uses lgpio (a modern kernel interface) on the Pi 5 and older libraries such as RPi.GPIO on older boards. gpiozero hides the differences, so your code works on all of them. Install it with sudo apt install python3-gpiozero if it is missing.

Pull-up and pull-down resistors

An input that is connected to nothing floats: it reads random highs and lows from electrical noise. A pull-up resistor ties the pin to 3.3 V so it reads high until something pulls it low. A pull-down resistor ties it to ground instead. The Pi has built-in programmable pull resistors, so you rarely need external ones. You will use them in Buttons and Digital Input.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What voltage does a GPIO pin output when it is high?

    A high GPIO output is about 3.3 volts.
  2. 2. In gpiozero, what does LED(17) refer to?

    gpiozero uses BCM numbering, so 17 means GPIO17 on physical pin 11.
  3. 3. Why use a resistor in series with an LED?

    Without a resistor too much current flows and can damage the LED or the Pi.
  4. 4. What happens to an input pin connected to nothing?

    A floating input picks up noise; pull resistors give it a defined level.

Summary

  • GPIO pins are digital: high is about 3.3 V, low is about 0 V; each pin is an input or an output.
  • BCM numbers (GPIO17) differ from physical numbers (pin 11); gpiozero uses BCM.
  • Respect 3.3 V, low current and resistors; power off before wiring.
  • Floating inputs need pull-up or pull-down resistors.

Next: Blinking an LED with gpiozero