Sensors, displays and modules send and receive data to and from a microcontroller using agreed "languages" called protocols. The 3 you'll meet most are UART, I2C and SPI. If you know how each one works, you'll be able to wire any new module, and you'll know where to look when the module "doesn't respond".
UART (Serial)
- Two wires: TX (transmit) and RX (receive), plus ground.
- Crossed wiring: one device's TX goes to the other's RX, and vice versa.
- The speed (baud rate) must be the same on both ends: 9600 or 115200 are common.
- Only two devices on the same line.
- Examples: the Serial Monitor, GPS modules, GSM, and the HC-05 Bluetooth module.
How does it send?
There's no clock wire, so each end must know the speed in advance and keep time itself (which is why it's called asynchronous). Each byte is sent in a "frame": a start bit (LOW), then 8 data bits, then a stop bit (HIGH). That's 10 bits per byte, so at 115200 bits per second you send about 11,500 bytes per second, and at 9600 only about 960.
If the speed differs between the two ends, each one slices the bits in the wrong places, which is what produces the garbage characters in the Serial Monitor.
More than one UART
The Uno has only one UART, and it's the same one connected to the computer over USB (pins 0 and 1). If you connect a module to it, uploading and the Serial Monitor get confused. The solutions:
- SoftwareSerial: a library that creates a UART on any two pins in software. Good up to about 9600 or 38400, and it struggles at higher speeds.
- A board with more UARTs: the Mega has 4, and the ESP32 has 3 that you can map to many pins.
Common mistakes: TX wired to TX, a different speed (you'll see garbage characters), or a forgotten common ground.
I2C
- Two wires: SDA (data) and SCL (clock), plus ground and power.
- Many devices on the same two wires, each with an address (such as 0x3C for an OLED display and 0x27 for an LCD module).
- Needs pull-up resistors, usually already on the modules.
- Speed: 100 kHz (standard) or 400 kHz (fast).
- Examples: displays, temperature and humidity sensors, RTC clock chips, and external ADCs.
How does it send?
The microcontroller (the master) runs the whole conversation and generates the clock. Each message starts with a start condition, then the address (7 bits) and a bit saying read or write. Only the device with that address answers with an ACK (it pulls SDA low for a moment), and the rest stay silent. If there's no ACK, the device isn't there or the address is wrong.
I2C scanner
If the device doesn't respond, run this code first; it tells you which addresses are actually on the bus:
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
for (byte a = 1; a < 127; a++) {
Wire.beginTransmission(a);
if (Wire.endTransmission() == 0) {
Serial.print("Found a device at 0x");
Serial.println(a, HEX);
}
}
}
void loop() {}
If it finds nothing at all, the problem is in the wiring or power, not the code. The full troubleshooting steps are in the I2C troubleshooting article.
Two devices with the same address
Two sensors of the same type usually have the same address. The solutions, in order:
- An address-select pin: many modules have a pin (AD0 or ADDR) or a jumper that changes the address, like the MPU6050, which is either 0x68 or 0x69.
- Two I2C buses: the ESP32 has two, so each sensor gets its own bus.
- A TCA9548A multiplexer: a chip that creates 8 separate I2C buses from one.
SPI
- 4 wires: MOSI (master to device), MISO (device to master), SCK (clock) and CS (chip select).
- Much faster (megahertz or tens of megahertz).
- Each device needs its own CS wire.
- Examples: SD cards, colour TFT displays, flash memory and some fast ADCs.
How does it send?
The master pulls the CS of the device it wants LOW (so the others ignore the traffic), then with each clock pulse it sends a bit on MOSI and receives a bit on MISO at the same time. So sending and receiving happen together (full duplex), with no addresses and no ACK, which is why it's fast.
SPI modes: there are 4 modes (mode 0 to 3) that set whether the clock idles HIGH or LOW, and on which edge a bit is read. Most devices use mode 0, and libraries set it for you. If the data comes out consistently wrong, check the mode in the datasheet.
Quick comparison
| UART | I2C | SPI | |
|---|---|---|---|
| Wires | 2 | 2 | 4 (+1 per extra device) |
| Number of devices | 2 | many, by address | many, one CS each |
| Speed | medium | slow to medium | very fast |
| Distance | relatively longer | short (inside the device) | short |
| Confirms receipt? | no | yes (ACK) | no |
How to choose? If the module supports only one protocol, there's no choice. If it supports two (as many displays and sensors do): I2C if you want to save pins and have many devices, and SPI if you need speed (a colour display or an SD card).
Distance
I2C and SPI are made for short distances inside the same device: tens of centimetres, maybe a metre or two at lower speed with good wiring. The capacitance of a long wire slows the edges, so the signal breaks down. UART tolerates a bit more, and for truly long distances (tens or hundreds of metres) it's converted to RS-485 with a small module at each end.
Voltage: 3.3 or 5 V?
If the microcontroller is 3.3 V (like the ESP32) and the module is 5 V, the signals need a level shifter, or at least a voltage divider on the line going into the 3.3 V side. Many I2C modules work at both voltages, but check the datasheet. Details and methods are in the level shifting article.
Common ground
In every protocol: the ground must be shared between the devices. Without it, the signals have no reference and the data comes out wrong. Details are in the ground article.
The most common problems
| Problem | Protocol | Most likely |
|---|---|---|
| Garbage characters | UART | Different speed on each end |
| Nothing at all | UART | TX to TX, or no common ground |
| The scanner finds nothing | I2C | SDA and SCL swapped, power, or no pull-up |
| Works then stops every so often | I2C | Long wire, or too many pull-ups in parallel |
| Consistently wrong data | SPI | Wrong SPI mode, or MOSI and MISO swapped |
| One SPI device disrupts the other | SPI | One device's CS is unconnected or floating |
FAQ
Can I use I2C and SPI in the same project? Yes, no problem: each on its own pins, and the libraries don't conflict.
A module is labelled SDA and SCL but also has a CS pin? It probably supports both protocols, and that pin selects between them. Check the datasheet to wire it correctly for the protocol you'll use.