Blog
ESP32 I2C: Pins, Addresses, Scanner
Two wires, dozens of devices, one bus. This covers why should you use i2c for the esp32 board, how to find i2c port esp32, running both hardware buses, and the scanner sketch that solves most address mysteries.
Why Should You Use I2C for the ESP32 Board?
Because two wires — SDA and SCL — talk to nearly every sensor, display, and clock chip you can buy. Each device answers at its own address, so a dozen parts share one bus without burning extra pins. That matters on the ESP32, where you have roughly two dozen usable GPIOs and a Wi-Fi stack competing for them.
Which pins does the ESP32 use for I2C by default?
Classic ESP32 boards default to GPIO21 (SDA) and GPIO22 (SCL), but newer S3 and C3 modules move them — my ESP32-S3 DevKitC-1 wants GPIO8 and GPIO9. Nothing is wired until your code calls Wire.begin(sda, scl), so check your board’s pinout instead of trusting the classic default. Depends on your board, in other words.
How to Find I2C Port ESP32?
Run the I2C scanner. It is a 20-line sketch that pings every 7-bit address and prints which ones answer, and it ships as an example with the Wire library. If nothing replies, the port is almost never the problem — wiring and pull-ups are.
How to Do Two I2C on ESP32?
Create a second TwoWire object on free pins: TwoWire i2cTwo(1); i2cTwo.begin(18, 19);. The ESP32 has two hardware I2C controllers, so this is real hardware, not bit-banging. Mostly works with any free GPIO pair, and you can test both buses in the free ESP32 simulator before soldering anything.
How to Use Both I2C Ports on ESP32?
Instantiate TwoWire i2cOne(0) and TwoWire i2cTwo(1), call begin() on each with its own SDA/SCL pair, then hand the object to sensor libraries that accept a TwoWire: Adafruit_BME280 bme(&i2cTwo);. Each bus is independent, so two devices with the same address can live on different ports — I keep my display on bus 0 and the sensors on bus 1.
How to Use Multiple I2C Devices ESP32?
Give every device a unique 7-bit address and the rest is automatic. Two identical boards on one bus — two AHT20 modules, say — collide at the same address; check for address jumpers or an AD0-style pin on the module. Mixed vendors share fine: an OLED at 0x3C sits happily next to an MPU6050 at 0x68. More in the FAQ index under sensors.
My scanner finds nothing — what do I check?
Wiring first: SDA to SDA, SCL to SCL, a shared ground. Then pull-ups: 4.7 kΩ to 3.3V, one per line. The chip’s internal pull-ups are too weak at any real bus speed, and the official docs are vague about it — this one costs everyone an evening. The wiring KB entry walks through it with photos.
Do I need level shifting for 5V I2C sensors?
Yes, unless the module already converts levels on board. Classic ESP32 GPIOs are not 5V tolerant — the S3 mostly is — and a 5V LCD backpack on the same bus as a 3.3V sensor can lock the whole bus up. A two-dollar bidirectional level shifter fixes it.
How fast can I2C run on the ESP32?
400 kHz is the practical ceiling for most breakout boards; 100 kHz is the safe default. The hardware can clock faster, but long dupont wires and breadboard capacitance make anything above 400 kHz flaky. If a sensor glitches at 400, drop to 100 before blaming the code.
Can I use any GPIO for I2C?
Nearly any free pin works, but steer clear of strapping pins — GPIO0, GPIO2, and GPIO12 on the classic ESP32 — because they change boot behavior at power-on. I once chased a sensor that only failed after reset, and it turned out to be a strapping pin. The under-the-hood deep dive lists the full set.
Scan a Real I2C Bus — In Your Browser
The single most useful I2C tool is five lines of code, and it runs free in our simulator:
- I2C bus scanner — finds every device on the virtual bus:
===========================
I2C Bus Scanner (TWI)
===========================
Scanning...
Device found at 0x27 (PCF8574 LCD backpack)
Add or remove parts from the canvas and rescan — that is exactly how you chase a miswired bus on a real bench. Then pair it with a consumer:
- SSD1306 OLED on ESP32 — whose Serial Monitor prints the two sentences you will meet most in I2C life:
SSD1306 not found!(wrong address or SDA/SCL swapped) andOLED ready(bus sane, draw away).