Minimal bus
i2c:
sda: GPIO21
scl: GPIO22
scan: true
| ESP32 default | ESP8266 default | |
|---|---|---|
| SDA | GPIO21 | GPIO4 |
| SCL | GPIO22 | GPIO5 |
scan is on by default, and the frequency defaults to 50 kHz. Many sensors work faster (e.g. frequency: 100kHz or 400kHz) if the wiring is short.
Step 1: read the scan
After flashing, open the logs (esphome logs device.yaml). At startup the i2c component logs every address that answered the scan.
- Nothing found: SDA/SCL swapped, wrong pins in YAML, no power or ground to the sensor, or missing pull-ups.
- Found, but the sensor component fails: wrong
address:in the sensor config (many boards can be strapped to two addresses, e.g. BME280 at 0x76 or 0x77), or a different chip than the label says.
Step 2: pull-ups and wiring
- ESPHome enables the internal pull-ups (on ESP32 with the Arduino framework they're on by default, on ESP8266 always), but they're weak. For longer wires or several devices, add external pull-up resistors, or use breakout boards that have them.
- Keep I2C wires short. It's a bus meant for circuit boards, not for tens of meters of cable.
Step 3: two sensors with the same address
Either put them on separate buses:
i2c:
- id: bus_a
sda: GPIO21
scl: GPIO22
- id: bus_b
sda: GPIO25
scl: GPIO26
sensor:
- platform: bme280_i2c
i2c_id: bus_b
address: 0x76
temperature:
name: "Outside temperature"
Or use a TCA9548A multiplexer, which splits one bus into several channels.
Before choosing extra pins on ESP32, check the list of pins to avoid (strapping and flash pins).