Skip to content
GEEKnodes

Electronics

I2C vs SPI vs UART: Differences for Arduino, ESP32 and Raspberry Pi

What I2C, SPI and UART are, how to wire them, their speeds and which pins to use on Arduino Uno, ESP32 and Raspberry Pi. Plus the most common mistakes.

Almost every sensor, display and module you connect to an Arduino, an ESP32 or a Raspberry Pi speaks one of these three languages: I2C, SPI or UART. Knowing how each one works saves you hours of debugging and helps you choose modules that won’t cause trouble when you combine them.

Summary

I2CSPIUART
Signal wires2 (SDA, SCL)4 (MOSI, MISO, SCK, CS) + 1 CS per device2 (TX, RX)
Devices per busMany (up to 127 7-bit addresses)Several, each with its own CS line2 (point to point)
Typical speed100 kHz, 400 kHz (1 MHz in Fast-mode Plus)From 1 to tens of MHz9600–115200 baud is common (up to Mbit/s)
ClockShared (generated by the controller)Shared (generated by the controller)None: both sides agree on the speed
DuplexHalf duplexFull duplexFull duplex
Best forSensors, small displays, RTCs, expandersTFT displays, SD cards, fast ADCs, memory chipsGPS, Bluetooth/LoRa modules, debug console

I2C: two wires for many devices

I2C (Inter-Integrated Circuit) uses two lines shared by every device:

  • SDA: data.
  • SCL: clock.
A controller and three peripherals share the SDA and SCL lines with pull-up resistors and different addresses.
I2C connects several devices on the same two lines; each one answers to its own address.

Each device has an address (for example, 0x27 or 0x3F on LCD adapters, 0x76 on the BME280 sensor). The controller calls an address and only that device responds.

Both lines need pull-up resistors to the bus voltage (usually between 4.7 kΩ and 10 kΩ). Most modules already include them; if you connect lots of modules with pull-ups, the combined value drops and can cause problems.

Pros: very few wires, easy to add devices. Cons: slow compared with SPI, sensitive to long wires and to duplicate addresses (two identical modules with the same address can’t share a bus unless you change it).

For a hands-on example, see setting up an I2C LCD display with a temperature sensor.

SPI: fast and simple

SPI (Serial Peripheral Interface) uses four lines:

  • SCK: clock, generated by the controller.
  • MOSI (or COPI): data from the controller to the peripheral.
  • MISO (or CIPO): data from the peripheral to the controller.
  • CS (or SS): chip select, one per device.
An SPI controller shares SCK, MOSI and MISO with two peripherals and selects each one with its own CS line.
SPI shares three signal lines and uses a separate CS line for each peripheral.

The SCK, MOSI and MISO lines are shared; the controller activates the CS line of the device it wants to talk to. There are no addresses.

Pros: high speed (ideal for displays and SD cards), simple protocol. Cons: more wires and an extra pin for every device. You have to set the correct SPI mode (0–3) for each chip.

UART: the good old serial port

UART (Universal Asynchronous Receiver-Transmitter) connects two devices with two crossed lines:

  • TX of one → RX of the other.
  • RX of one ← TX of the other.
  • Common GND.
Two UART boards connect TX to RX and RX to TX, and also share GND.
With UART, the TX and RX lines cross over and both devices share ground.

There’s no clock: both sides have to use the same speed (baud rate), for example 9600 or 115200, and the same format (usually 8N1: 8 data bits, no parity, 1 stop bit).

Pros: universal, ideal for debugging (the Arduino Serial Monitor runs over UART) and for modules such as GPS or Bluetooth. Cons: only two devices per port and, if the speeds don’t match, all you get is garbage.

Pins on the most common boards

BoardI2C (SDA / SCL)SPI (MOSI / MISO / SCK)UART (TX / RX)
Arduino Uno / Nano (ATmega328P)A4 / A5D11 / D12 / D13D1 / D0 (shared with USB)
ESP32 (DevKit)GPIO 21 / 22 by default, configurableGPIO 23 / 19 / 18 (VSPI) by defaultUART0 on GPIO 1 / 3 (USB); UART2 on GPIO 17 / 16
Raspberry Pi (40-pin)GPIO 2 / 3 (pins 3 and 5)GPIO 10 / 9 / 11 (pins 19, 21, 23)GPIO 14 / 15 (pins 8 and 10)

On the Raspberry Pi, I2C, SPI and the serial port are disabled by default. You enable them with sudo raspi-config → Interface Options.

Voltage levels: the mistake that fries boards

  • Arduino Uno runs at 5 V.
  • ESP32 and Raspberry Pi run at 3.3 V and are not 5 V tolerant on their pins.

If you connect a 5 V module to an ESP32 or a Raspberry Pi, use a logic level shifter (a bidirectional one for I2C) or a module that supports 3.3 V.

Common problems

  • I2C: the device doesn’t respond. Run an I2C scanner to see which addresses are on the bus: on Arduino, the i2c_scanner sketch; on Raspberry Pi, i2cdetect -y 1. If nothing shows up, check SDA/SCL (are they swapped?), power and the pull-ups.
  • I2C: two modules with the same address. Many modules let you change it with solder jumpers (A0, A1, A2). If not, use an I2C multiplexer such as the TCA9548A.
  • SPI: the display stays blank. Check the CS line and the DC/RS pin (TFT displays have an extra one), plus the SPI mode.
  • UART: weird characters. Different speed on each side, or TX/RX not crossed. And don’t forget the common GND.
  • Arduino Uno: I can’t upload the sketch. If you have something connected to D0/D1, it interferes with USB. Disconnect it while programming or use SoftwareSerial on other pins.

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor