esp32-i2c-bus
/esp32-i2c-bus
Use when adding I2C devices to an ESP32, when a sensor is not detected, or when the bus works alone and fails with a second device. Covers pull-ups, address conflicts, clock stretching and stuck-bus recovery.
- Author
- Esmail-sarhadi ↗
- Repository
- esp32-agent-skills ↗
- Licence
- MIT
- Last repository change
- 11.11.2026
Assessment and security
- Files in this package
- 2
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 11.11.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
What it does and when to use it
Use when adding I2C devices to an ESP32, when a sensor is not detected, or when the bus works alone and fails with a second device. Covers pull-ups, address conflicts, clock stretching and stuck-bus recovery.
Original author description
Use when adding I2C devices to an ESP32, when a sensor is not detected, or when the bus works alone and fails with a second device. Covers pull-ups, address conflicts, clock stretching and stuck-bus recovery.
How it works
- Wiring, before any code
- Address conflicts
- The three failure modes
- Rules for the agent
SKILL.md preview
---
name: esp32-i2c-bus
description: Use when adding I2C devices to an ESP32, when a sensor is not detected, or when the bus works alone and fails with a second device. Covers pull-ups, address conflicts, clock stretching and stuck-bus recovery.
license: MIT
---
# I2C on ESP32
I2C fails in ways that look like a broken sensor. Almost always it is the bus.
## Wiring, before any code
- Default pins on the Arduino core are `SDA=21`, `SCL=22`. Any pin can be used
with `Wire.begin(sda, scl)`; changing them is not the problem, changing them
to an input-only pin (`GPIO34`-`GPIO39`) is.
- **The internal pull-ups are not enough.** They are in the tens of kilo-ohms,
which is fine for a slow single device and marginal for everything else.
Add external resistors: roughly 4.7 kΩ for 100 kHz, 2.2 kΩ for 400 kHz. These
are rules of thumb, not spec values.
- The I2C specification caps total bus capacitance at 400 pF. Long wires,
dupont cables and many devices add up fast; when a bus works on a breadboard
and fails on a 30 cm cable, this is why.
- A 5 V device on a 3.3 V bus needs a level shifter. A bare connection sometimes
"works" and then damages the ESP32 later.
## Address conflicts
Most cheap sensors ship at a fixed address, and the popular ones share it:
two SSD1306 displays, or an BMP280 next to a BME280, both at `0x76`/`0x77`.
Scan first, then look for two devices answering the same address — the scan
will show one, and one of them will simply never respond again.
```cpp
for (uint8_t address = 1; address < 127; address++) {
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0) {
Serial.printf("found 0x%02X\n", address);
}
}
```
## The three failure modes
1. **Nothing responds.** Pull-ups missing, `SDA`/`SCL` swapped, or the device is
not powered. Measure the idle level: both lines must sit at 3.3 V.
2. **One device works, two do not.** Address conflict, or the pull-up is now
too weak for the added capacitance.
3. **It works until the bus hangs.** A device held the line low mid-transfer.
The bus is stuck: the master cannot generate a start condition because `SDA`
is low. Recovery is to clock `SCL` manually nine times, then issue a stop.
```cpp
void i2cBusRecover(uint8_t sclPin) {
pinMode(sclPin, OUTPUT);
for (int i = 0; i < 9; i++) {
digitalWrite(sclPin, LOW);
delayMicroseconds(5);
digitalWrite(sclPin, HIGH);
delayMicroseconds(5);
}
pinMode(sclPin, INPUT); // hand the line back to the peripheral
}
```
## Rules for the agent
- Never assume a sensor's address. Print it from a scan and put the real value
in the code, with the scan output as a comment.
- If the user says "the sensor is not detected", ask for the scan output before
changing any code. The scan distinguishes wiring from software in one step.
- Do not add `Wire.setClock(400000)` to fix slowness on a long cable. Slower is
more reliable; the fix for a marginal bus is shorter wires and stronger
pull-ups.
- Do not put I2C transactions in an ISR. `Wire` blocks and the ISR will hang the
core.
Files in this package (2)
- LICENSE.txt 1 KB
- SKILL.md 3 KB
Versions
The database has no other skills named esp32-i2c-bus.
Database snapshot: 13.11.2026 · package version 7b32ed3ae08c (SHA-256 digest of the entire package)
Downloads in the last 30 days: 1 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 95esp32-spi-and-devices
Use when an ESP32 talks to SPI peripherals - the second device on the bus fails, the bus is initialised twice, transfers are slow or corrupted, or the clock rate is a guess. Covers bus versus device, DMA and chip select. - 85esp32-can-twai
Use when an ESP32 joins a CAN bus - no frames arrive, the bus goes off after a while, the bitrate will not match, or the transceiver is missing. Covers TWAI setup, termination, bitrate and bus-off recovery. - 90azure-service-bus
Expert coverage of Azure Service Bus managed messaging service: queues, topics, subscriptions, sessions, dead-letter queues, duplicate detection, transactions, Premium tier, geo-disaster recovery, and SDK patterns. Use for \"Azure Service Bus\", \"Service Bus\", \"Service Bus… - 90i2c-bringup-diagnostician
Use when users need to diagnose I2C bring-up failures on Arduino or ESP32-class boards, including no devices found, wrong readings, address conflicts, scanner-detects-but-library-fails cases, pull-up problems, or board-family-specific bus quirks. - 100embedded-driver-design
Use when developing or reviewing peripheral drivers for embedded firmware (STM32, GD32, ESP32, FreeRTOS, drone flight controllers, robotics). Enforces layered driver architecture with bus abstraction, device context objects, register encapsulation, and strict separation between… - 95scaffold_driver
Scaffold a device driver for one external peripheral chip (header, source and register map) on top of the project's HAL, with every register, address and bus setting taken from the part's datasheet and the board's schematic, and record it in the twin. Use when the user asks to…