Humidity, IMU, display bridges — most sensors live here.
Sensors and I/OI2C sensorESP32 family
Official path: examples/peripherals/i2c/i2c_simple
01 Overview
This example demonstrates ESP32 as an I2C master reading and writing slave registers. I2C is the go-to bus for low-speed peripherals: temperature/humidity sensors, accelerometers, barometers, small OLEDs, EEPROMs, and IO expanders.
It solves the problem of connecting multiple devices with just two wires (SDA and SCL), using addresses to distinguish them, saving GPIOs and board space.
Use it when you need to talk to standard I2C sensors or small display modules. Skip it for high-rate imaging (use SPI) or bulk storage (use SDIO).
The example covers driver configuration, bus scanning, register read/write, and basic error handling.
It is a foundation for any I2C-based product firmware.
02 Hardware
Pull-ups on SDA/SCL: Typically 4.7kΩ to VCC, but adjust for bus capacitance and speed (2.2kΩ for 400kHz fast mode).
Voltage domain: All devices must share the same logic voltage; otherwise use a level shifter like PCA9306.
Address conflicts: Two identical sensors need different ADDR pin strapping; check the schematic.
Bus length: Keep traces short to avoid capacitance and signal integrity issues.
Wiring: Verify SDA and SCL are not swapped and not shared with other peripherals.
Power: Ensure clean supply and decoupling capacitors near sensor power pins.
03 Software flow
1. Configure I2C port, pins, and speed (e.g., 100kHz or 400kHz).
2. Install the driver with i2c_driver_install.
3. Use i2c_master_write_to_device to write registers and i2c_master_read_from_device to read data.
4. Optionally scan the bus to confirm all addresses.
5. Check return values: NACK means no device response; timeout means bus stuck.
6. In a FreeRTOS task, periodically sample the sensor with appropriate delays.
7. For low power, deinit the driver before sleep and reinit after wake.
04 Core points
No pull-ups, no I2C: The bus requires pull-ups to function.
Address conflicts are common: Plan addresses at schematic stage.