Sensors and I/O

I2C simple read/write

Humidity, IMU, display bridges — most sensors live here.

Sensors and I/O I2C sensor ESP32 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.
  • Never spin forever: Use timeouts to avoid hangs.
  • Custom work: Sensor choice, schematic, sample task, error handling strategy.
  • Higher speed is not always better: It demands better layout and may be unstable.

中文

传感器与外设

I2C 简单读写

温湿度、加速度计、屏幕桥,大多数传感器走这里。

传感器与外设 I2C 传感器 ESP32 family

官方路径: examples/peripherals/i2c/i2c_simple

01 项目概述

这是一个 ESP32 作为 I2C 主机读写从机寄存器的示例。I2C 是低速外设最常用的总线,适合温湿度、加速度计、气压计、OLED 屏幕等。

解决的问题:用两根线(SDA/SCL)挂多个设备,通过地址区分,节省 GPIO 和布线。

适用场景:标准 I2C 传感器、小尺寸显示模块、EEPROM、IO 扩展芯片。

不适用场景:高速图像传输(如摄像头)、大数据量存储(用 SPI 或 SDIO)。

本示例演示了如何配置 I2C 驱动、扫描总线、读写寄存器,并处理常见的错误。

02 项目硬件描述

  • SDA/SCL 上拉: 通常 4.7kΩ 到 VCC,但具体值取决于总线电容和速率。快速模式(400kHz)可能需要 2.2kΩ。
  • 电压域: 所有设备必须在同一电压域,否则需要电平转换(如 PCA9306)。
  • 地址冲突: 两个相同传感器必须通过 ADDR 引脚设置不同地址,否则总线冲突。
  • 总线长度: 保持短线,避免高电容导致信号变形。
  • 接线: 确认 SDA 和 SCL 没有接反,且没有与其他外设复用。
  • 电源: 确保传感器供电稳定,去耦电容靠近电源引脚。

03 项目软件流程描述

1. 配置 I2C 端口、引脚和速率(例如 100kHz 或 400kHz)。

2. 调用 i2c_driver_install 初始化驱动。

3. 使用 i2c_master_write_to_device 写寄存器,i2c_master_read_from_device 读数据。

4. 可选:扫描总线,确认所有设备地址正确。

5. 处理错误:检查返回值,NACK 表示设备无响应,超时表示总线卡住。

6. 在 FreeRTOS 任务中周期读取传感器,注意任务优先级和延时。

7. 低功耗场景:在休眠前关闭 I2C 驱动,唤醒后重新初始化。

04 项目核心点

  • 没上拉就没 I2C: 必须接上拉电阻,否则总线无法工作。
  • 地址冲突是常见问题: 设计原理图时就要规划地址。
  • 不要死等: 使用超时机制,避免系统卡死。
  • 可定制: 传感器选型、原理图、采集任务、错误处理策略。
  • 速率不是越高越好: 高速率对布线要求更高,可能不稳定。