Ugenz Academy/IoT Tutorial

I2c Spi Uart And I2s

14 min read2,659 words

Reading progress

Keep learning

ESP32 — I2C, SPI, UART and I2S

ESP32 becomes much more powerful when it can communicate with other electronic devices.

For example:

ESP32 ├── LCD ├── OLED ├── RFID ├── GPS ├── SD Card ├── Sensors ├── Audio devices └── Other microcontrollers

Different devices use different communication protocols.

The four important protocols in this topic are:

I2C SPI UART I2S

1. What Is a Communication Protocol?

A communication protocol is a set of rules that tells devices:

How should we send and receive data?

For example:

ESP32 ─────── Sensor DATA

Both devices need to agree on things such as:

  • Which wire carries data
  • Which wire carries clock
  • How data is formatted
  • When data is sent
  • When data is received

That's what a protocol provides.


2. The Four Protocols

ProtocolMain Use
I2CSensors, LCD, OLED, RTC
SPIRFID, SD card, displays
UARTGPS, serial modules, ESP32 ↔ Arduino
I2SDigital audio

Easy memory:

I2C → Many devices with few wires SPI → Fast communication UART → Serial communication I2S → Audio

3. I2C

I2C = Inter-Integrated Circuit

It is one of the most commonly used protocols in ESP32 projects.

I2C generally uses only two signal wires:

SDA → Data SCL → Clock

Plus:

VCC GND

So:

ESP32 │ ├── SDA ───── Device │ ├── SCL ───── Device │ ├── VCC ───── Device │ └── GND ───── Device

4. Why Does I2C Need a Clock?

The clock synchronizes communication.

Think of it like two people talking:

Clock: "Now!" Device: "Here's the next bit." Clock: "Now!" Device: "Here's another bit."

The clock line is:

SCL

The data line is:

SDA

5. I2C Multiple Devices

One major advantage of I2C is that multiple devices can share the same SDA and SCL lines.

For example:

ESP32 │ ┌────────┴────────┐ │ │ SDA SCL │ │ ┌─────┼─────┬─────┬─────┼─────┐ │ │ │ │ │ │ OLED LCD RTC Sensor ...

Each device normally has an I2C address.

Example:

OLED → 0x3C RTC → 0x68 LCD → 0x27

So the ESP32 knows which device it is communicating with.


6. Typical ESP32 I2C Pins

On many classic ESP32 development boards, the commonly used default I2C pins are:

SDA → GPIO 21 SCL → GPIO 22

Example:

ESP32 GPIO 21 ─── SDA GPIO 22 ─── SCL

However, ESP32 allows I2C signals to be routed to other suitable GPIOs in software, and exact defaults can vary by board/core configuration.


7. I2C Example

For Arduino-ESP32:

#include <Wire.h> void setup() { Serial.begin(115200); Wire.begin(21, 22); } void loop() { }

Here:

21 → SDA 22 → SCL

8. I2C LCD

You have already worked with LCD I2C modules.

Typical wiring:

LCD I2C ESP32 ---------------------- VCC → 5V/appropriate supply GND → GND SDA → GPIO 21 SCL → GPIO 22

The exact LCD supply and logic compatibility should be checked for your particular module.

Example:

#include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); void setup() { Wire.begin(21, 22); lcd.init(); lcd.backlight(); lcd.setCursor(0, 0); lcd.print("Hello ESP32"); } void loop() { }

9. What Is 0x27?

This:

LiquidCrystal_I2C lcd(0x27, 16, 2);

contains:

0x27

which is the I2C address of the LCD backpack in many common modules.

But it isn't universal.

Some LCD modules use:

0x27

while others use:

0x3F

If your LCD doesn't respond, an I2C scanner is a useful debugging tool.


10. I2C Scanner

A simple scanner can detect devices connected to the I2C bus.

#include <Wire.h> void setup() { Serial.begin(115200); Wire.begin(21, 22); Serial.println("I2C Scanner"); for (byte address = 1; address < 127; address++) { Wire.beginTransmission(address); byte error = Wire.endTransmission(); if (error == 0) { Serial.print("Device found at 0x"); if (address < 16) { Serial.print("0"); } Serial.println(address, HEX); } } } void loop() { }

If your LCD is detected, you might see:

Device found at 0x27

11. I2C Pull-Up Resistors

I2C lines typically require pull-up resistors on:

SDA SCL

Many breakout modules already include them.

For example:

SDA ───┬──────── devices │ Pull-up │ VCC

and similarly for SCL.

If several modules are connected, you should be aware that multiple onboard pull-ups can combine and produce too-strong pull-up resistance.


12. SPI

SPI = Serial Peripheral Interface

SPI is commonly used when higher communication speed is useful.

Examples:

RFID SD card TFT display Flash memory Fast sensors

SPI generally uses these signals:

SCK MOSI MISO CS/SS

13. Meaning of SPI Pins

SCK

Serial Clock

The controller generates the clock.

MOSI

Master Out, Slave In

Data from controller → peripheral.

MISO

Master In, Slave Out

Data from peripheral → controller.

CS / SS

Chip Select / Slave Select

Selects which peripheral should communicate.


14. SPI Diagram

ESP32 │ ┌──────────────┼──────────────┐ │ │ │ │ │ SCK MOSI MISO CS │ │ │ │ └──────┴───────┴───────┘ │ RFID

For multiple SPI devices:

SCK ────────────────┐ MOSI ──────────────┐│ MISO ─────────────┐││ │││ ┌─────────────┐ ┌─┴┴┴────────┐ │ RFID │ │ SD Card │ │ CS1 │ │ CS2 │ └─────────────┘ └────────────┘

SCK/MOSI/MISO can be shared, while each device generally gets its own CS line.


15. Common SPI Pins on Classic ESP32

A commonly used VSPI mapping on classic ESP32 boards is:

SCK → GPIO 18 MISO → GPIO 19 MOSI → GPIO 23 CS → GPIO 5

This is a common default arrangement, not a universal requirement.

ESP32's flexible GPIO matrix allows peripheral signals to be routed differently where supported.


16. SPI Example

Arduino-ESP32 provides the SPI library.

#include <SPI.h> #define CS_PIN 5 void setup() { Serial.begin(115200); SPI.begin(18, 19, 23, CS_PIN); pinMode(CS_PIN, OUTPUT); digitalWrite(CS_PIN, HIGH); } void loop() { }

The parameters correspond to:

SCK MISO MOSI CS

17. RFID + SPI

A common example is the MFRC522 RFID module.

Typical connections:

MFRC522 ESP32 ----------------------- SDA/SS → GPIO 5 SCK → GPIO 18 MOSI → GPIO 23 MISO → GPIO 19 RST → GPIO 27 GND → GND 3.3V → 3.3V

The exact pin labels and library configuration can vary.

Important:

The MFRC522 is a 3.3V device. Don't assume a 5V supply is safe for it.


18. SPI vs I2C

FeatureI2CSPI
Typical signal wires23 + CS
SpeedUsually lowerUsually higher
Multiple devicesAddressesSeparate CS
WiringSimpleMore wires
Common useLCD, sensorsRFID, SD, TFT

Easy memory:

I2C → fewer wires SPI → more wires, generally faster

19. UART

UART = Universal Asynchronous Receiver/Transmitter

UART is another extremely important serial communication protocol.

It generally uses:

TX RX GND

20. UART Wiring

If ESP32 communicates with another device:

ESP32 Device TX ────────────→ RX RX ←──────────── TX GND ───────────── GND

Notice:

TX → RX RX → TX

They cross.


21. Why Is It Called Asynchronous?

UART normally doesn't use a shared clock wire.

Instead, both devices agree on communication parameters such as:

Baud rate Data bits Parity Stop bits

For example:

115200 baud 8 data bits No parity 1 stop bit

Often written as:

115200 8N1

22. UART Example — ESP32 to Serial Device

ESP32 TX ─────→ Device RX ESP32 RX ←───── Device TX ESP32 GND ───── Device GND

If both devices support compatible voltage levels and UART settings, they can communicate.


23. ESP32 Hardware Serial

ESP32 has multiple hardware UART controllers, though the exact UART count and pin availability can vary across ESP32 family members.

On many classic ESP32 boards, you may see:

UART0 UART1 UART2

Arduino code can create a hardware serial interface.

Example:

HardwareSerial mySerial(2); void setup() { Serial.begin(115200); mySerial.begin( 9600, SERIAL_8N1, 16, 17 ); }

Here:

RX → GPIO 16 TX → GPIO 17

assuming your board/chip supports that routing.


24. UART GPS Example

GPS modules commonly communicate using UART.

Conceptually:

GPS │ ├── TX ───→ ESP32 RX │ └── RX ←─── ESP32 TX

GPS continuously sends data such as:

Latitude Longitude Altitude Time Satellites

The ESP32 receives this serial data and processes it.


25. ESP32 ↔ Arduino

UART can also connect:

ESP32 ↔ Arduino

For example:

ESP32 TX ───── Arduino RX ESP32 RX ───── Arduino TX GND ───── Arduino GND

But you must consider logic voltage compatibility.

ESP32 GPIO logic is 3.3V, while many classic Arduino boards use 5V logic.

A direct 5V signal into an ESP32 GPIO can damage it.

Use appropriate level shifting where needed.


26. UART vs Serial Monitor

When you write:

Serial.begin(115200);

on many ESP32 boards, you're using one UART/USB-serial path for communication with your computer.

But you can also create another UART:

HardwareSerial mySerial(2);

Then conceptually:

ESP32 / \ / \ USB Serial UART2 │ │ ↓ ↓ Computer GPS

This is extremely useful in IoT projects.


27. I2S

Now we reach a specialized protocol:

I2S = Inter-IC Sound

It is mainly designed for digital audio.

Examples:

Digital microphone Audio DAC Audio amplifier I2S speaker Audio codec

28. I2S Signals

Common I2S signals include:

BCLK WS / LRCLK DIN DOUT

Depending on the device, not every signal is used in exactly the same way.

BCLK

Bit clock.

WS / LRCLK

Word-select / left-right clock.

DIN

Digital audio data input to a device.

DOUT

Digital audio data output from a device.


29. I2S Concept

ESP32 │ ┌──────────┼──────────┐ │ │ │ BCLK WS DATA │ │ │ └──────────┴──────────┘ │ Audio Device

Unlike normal UART, I2S is specifically structured for digital audio streams.


30. Digital Microphone

An I2S microphone can send digital audio directly to ESP32.

I2S Microphone │ ↓ ESP32 │ ↓ Process audio

Possible applications:

Voice recording Sound level analysis Voice-controlled IoT Audio streaming Sound detection

31. I2S Speaker / DAC

The reverse direction is also possible:

ESP32 ↓ I2S audio data ↓ I2S DAC / amplifier ↓ Speaker

This can be used for:

Music Voice Notifications Alarm sounds Text-to-speech systems

32. I2S Is Different from PWM Audio

You may already know PWM can produce tones using a buzzer.

For example:

PWM ↓ Buzzer ↓ Tone

I2S is designed for digital audio data streams and is much more suitable for higher-quality digital audio applications.


33. Comparing All Four

ESP32 COMMUNICATION │ ┌─────────────────┼─────────────────┐ │ │ │ I2C SPI UART │ │ │ Sensors/LCD RFID/SD GPS/Modules OLED/RTC TFT/Flash MCU-to-MCU │ │ └─────────────────┬─────────────────┘ │ I2S │ Audio

34. Quick Comparison Table

ProtocolMain SignalsMain UseSpeed
I2CSDA, SCLSensors/LCD/OLEDModerate
SPISCK, MOSI, MISO, CSRFID/SD/TFTHigh
UARTTX, RXGPS/serial devicesModerate
I2SBCLK, WS, DATADigital audioAudio-oriented

35. When Should You Use I2C?

Choose I2C when you have:

LCD OLED RTC Temperature sensor Accelerometer Gyroscope Multiple sensors

Especially when you want to connect multiple devices using relatively few wires.


36. When Should You Use SPI?

Choose SPI when you need:

RFID SD card TFT display Fast sensor Flash memory

Especially when communication speed is important.


37. When Should You Use UART?

Choose UART when connecting:

GPS GSM module Bluetooth serial module Another microcontroller Serial sensor Serial communication device

38. When Should You Use I2S?

Choose I2S for:

Digital microphone Audio amplifier Audio DAC Speaker Audio codec Digital audio processing

39. One ESP32 Can Use Multiple Protocols

This is where ESP32 becomes really powerful.

Imagine your smart-home project:

ESP32 │ ┌───────────────┼─────────────────┐ │ │ │ I2C SPI UART │ │ │ ↓ ↓ ↓ LCD RFID GPS │ ├── DHT/other I2C sensor │ └── RTC │ I2S │ Speaker

All these communication systems can coexist when configured correctly and when their GPIO/peripheral resources don't conflict.


40. Practical Smart-Home Architecture

For your type of ESP32 project, you might have:

ESP32 │ ┌─────────────┼──────────────┐ │ │ │ I2C SPI GPIO │ │ │ ↓ ↓ ↓ LCD RFID PIR OLED SD Relay DHT TFT Servo RTC Buzzer

And if audio is added:

ESP32 │ I2S │ Digital Audio Device

41. Important Wiring Rule — Common GND

When connecting separate modules, you normally need a common reference:

ESP32 GND ───── Module GND

For example:

ESP32 │ ├── SDA ├── SCL └── GND │ ↓ LCD

Without a proper common ground/reference, communication may fail.

For externally powered modules, connect grounds appropriately unless the circuit is intentionally isolated.


42. Important Voltage Rule

Always check the voltage requirements of the module.

ESP32 GPIO logic is generally 3.3V.

Do not blindly connect a 5V signal to an ESP32 GPIO.

For example:

5V Device TX │ ↓ Level Shifter │ ↓ ESP32 RX

A level shifter or suitable voltage-divider/interface may be required depending on the signal and direction.


43. Common Mistake — TX to TX

Wrong:

ESP32 TX ─── TX ESP32 RX ─── RX

Usually correct for UART:

ESP32 TX ─── RX ESP32 RX ─── TX

Remember:

TX → RX RX ← TX

44. Common Mistake — SPI CS

With SPI, you may have:

SCK MOSI MISO

shared between devices.

But each device normally needs its own:

CS

For example:

ESP32 │ ├── SCK ─────┬── RFID │ └── SD │ ├── MOSI ────┬── RFID │ └── SD │ ├── MISO ────┬── RFID │ └── SD │ ├── CS1 ─────── RFID │ └── CS2 ─────── SD

Only one CS should normally be active for a particular transaction.


45. Common Mistake — I2C Address

If an I2C device isn't working:

Check: ↓ SDA SCL GND Power I2C address Pull-ups Library

An I2C scanner is often the fastest first test.


46. Common Mistake — Wrong UART Settings

Both UART devices need compatible settings.

For example:

ESP32: 9600 baud GPS: 9600 baud

If one uses:

115200

and the other:

9600

communication will not work correctly.

Also check:

Data bits Parity Stop bits

47. Protocol Selection Cheat Sheet

When you see:

LCD/OLED

Think:

I2C

RFID MFRC522

Think:

SPI

GPS

Think:

UART

SD card

Often:

SPI

Digital microphone

Think:

I2S

ESP32 ↔ Arduino serial communication

Think:

UART

48. The Big Picture

ESP32 │ ┌────────────────┼────────────────┐ │ │ │ I2C SPI UART │ │ │ ↓ ↓ ↓ Sensors RFID GPS LCD SD Card GSM OLED TFT MCU RTC Flash │ │ └────────────────┐ ↓ I2S ↓ Digital Audio

Remember:

I2C → SDA + SCL SPI → SCK + MOSI + MISO + CS UART → TX + RX I2S → Audio data + clock signals

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy