Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท GPIO and Python

Buses: I2C, SPI and UART

Learn the three serial buses on the Raspberry Pi: I2C, SPI and UART. Enable them, scan for devices with i2cdetect and read registers from Python.

Many sensors and displays do not use a single pin. They send data as numbers, so they need a communication bus: a small set of wires and a protocol. The Raspberry Pi header has three common ones: I2C, SPI and UART. Once you understand them, you can connect hundreds of different chips: temperature sensors, accelerometers, small OLED screens, analog converters and more.

The three buses at a glance

Bus Wires Speed Typical use
I2C 2 (SDA, SCL) plus power and ground Slow to medium Many sensors on one pair of wires
SPI 4 (MOSI, MISO, SCLK, CS) Fast Displays, ADCs, memory chips
UART 2 (TX, RX) Medium GPS modules, serial consoles, other computers

All three are enabled with raspi-config (Interface Options). They are off by default, because they claim pins that would otherwise be general-purpose.

Pick a bus

I2C 2 wires, many devices
SPI 4 wires, fast
1 / 2

I2C

I2C (Inter-Integrated Circuit) uses two lines: SDA (data) and SCL (clock), GPIO2 and GPIO3 on physical pins 3 and 5. Several devices share the same two wires. Each has a unique 7-bit address, for example 0x76 for a common pressure sensor or 0x68 for a real-time clock. The Pi is the controller and sends a clock; devices are targets that answer when their address is called.

The pins have built-in pull-up resistors on the Pi, which I2C requires. Wire the sensor's VCC to 3.3 V, GND to ground, SDA to pin 3 and SCL to pin 5. After enabling I2C and installing the tools, scan the bus (read-only):

Terminal
sudo raspi-config nonint do_i2c 0
sudo apt install -y i2c-tools
i2cdetect -y 1

A typical output shows a grid with an address where a device answered:

TEXT
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
60:                                                 
70: -- -- -- -- -- -- 76 --

Python reads from a device with the smbus2 library (read-only):

Python
from smbus2 import SMBus

with SMBus(1) as bus:                 # bus 1 on the 40-pin header
    chip_id = bus.read_byte_data(0x76, 0xD0)   # register 0xD0
    print(hex(chip_id))

A device exposes numbered registers. You write to a register to configure it and read a register to get data.

SPI

SPI (Serial Peripheral Interface) has separate wires for each direction: MOSI (controller out, GPIO10), MISO (controller in, GPIO9), SCLK (clock, GPIO11) and a chip select per device: CE0 (GPIO8) or CE1 (GPIO7). Because data lines are separate and the clock can run fast, SPI is much quicker than I2C. The price is more wires and one chip-select pin per device. With spidev (read-only):

Python
import spidev

spi = spidev.SpiDev()
spi.open(0, 0)                  # bus 0, chip select CE0
spi.max_speed_hz = 1_000_000
reply = spi.xfer2([0x01, 0x80, 0x00])
spi.close()

SPI transfers are always full duplex: while you send a byte you also receive one.

UART

UART is the old-fashioned serial port: TX (transmit, GPIO14) and RX (receive, GPIO15). The two ends agree on a baud rate (bits per second, e.g. 9600) beforehand. Connect TX of one device to RX of the other and share ground. Use it for GPS receivers, some fingerprint scanners and for a serial console to the Pi when there is no network. Enable the serial port in raspi-config, then (read-only):

Python
import serial

with serial.Serial("/dev/serial0", 9600, timeout=1) as port:
    line = port.readline().decode(errors="replace")
    print(line)

Simulating a register-based device

The stand-ins let you practice the register idea. The first one models an I2C bus where a fake device answers at an address, and a scan finds it.

Python Stand-in: scan a simulated I2C bus

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.

Decoding sensor bytes

Sensors give you raw bytes. You have to combine them into numbers, a skill used with every bus. This stand-in turns two bytes into a signed 16-bit value and scales it, the way a temperature chip might.

Python Stand-in: combine bytes into a value

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.

Choosing the right bus

If a sensor offers more than one interface, think about your needs: use I2C when you have many slow devices and few free pins, SPI when speed matters (displays, ADCs), UART for a single device far away or a console. Watch for address clashes: two I2C devices with the same address cannot share a bus unless one has a selectable address pin or you use a multiplexer.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which bus uses only two signal wires and gives each device an address?

    I2C uses SDA and SCL, and devices are selected by address.
  2. 2. Which command scans the I2C bus for devices?

    i2cdetect from the i2c-tools package lists the addresses that answer.
  3. 3. Which bus is usually the fastest?

    SPI has a dedicated clock and separate data lines, so it runs much faster.
  4. 4. What must both ends of a UART connection agree on?

    UART has no clock line, so both sides must use the same baud rate.

Summary

  • I2C uses SDA and SCL with addressed devices; SPI uses four wires and is fast; UART uses TX and RX with an agreed baud rate.
  • Enable each bus in raspi-config; use i2cdetect, smbus2, spidev and pyserial.
  • Devices expose registers; combine raw bytes into scaled numbers.
  • Choose the bus by speed, pin count and number of devices.

Next: Temperature and Humidity Sensors