Capstone · #16 of 48
Sensor Selection + I²C Wiring
SHT31/BME280 Integration
Why it matters
The capstone uses an I²C environmental sensor (temperature, humidity, pressure). Proper wiring and address scanning are critical.
The idea
Sensor Options
Common I²C environmental sensors:
- SHT31: Temperature + humidity, 0x44 address
- BME280: Temperature + humidity + pressure, 0x76 or 0x77
- SHT40: Temperature + humidity, improved accuracy
I²C Wiring
Standard I²C connections:
- VCC → 3.3V (not 5V!)
- GND → GND
- SDA → GPIO21 (or configured pin)
- SCL → GPIO22 (or configured pin)
- Pull-ups: 10kΩ resistors on SDA and SCL to 3.3V
Address Scanning
First step: verify sensor is connected:
- Scan I²C bus for all addresses (0x08–0x77)
- Look for ACK responses
- Verify expected address matches datasheet
Common Issues
- No device found: Check wiring, pull-ups, power
- Wrong address: Some sensors have address-select pins
- NACK errors: Sensor not ready, check timing
Demo
Sensor wiring is physical, not visual. Review this before connecting your sensor.
Key takeaways
- Use 3.3V sensors (not 5V) for direct ESP32 connection
- I²C requires pull-up resistors (10kΩ) on SDA and SCL
- Always scan I²C bus to verify sensor address
- Check datasheet for address-select pins if address doesn’t match
Going deeper
SHT31 has excellent accuracy (±2% RH, ±0.3°C) and low power consumption (~2.7mA active, <1µA sleep). BME280 adds pressure sensing but uses more power. For battery-powered devices, SHT31 is often the better choice. Always check sensor power consumption in datasheet.
Math details
I²C pull-up calculation:
V_CC = 3.3V
I_max = 3mA (I²C spec)
R_min = V_CC / I_max = 3.3V / 0.003A = 1100Ω
Use 10kΩ (standard value, provides ~330µA current)
Capacitance limit:
C_bus_max = 400pF (I²C spec)
With 10kΩ pull-ups: works up to ~1m cable length
Sensor power:
SHT31: 2.7mA active, <1µA sleep
BME280: 3.6µA sleep, 338µA active
Implementation
LLM Prompt: I²C Address Scanner
Write Rust code for ESP32 I²C address scanner using esp-hal.
Scan addresses 0x08–0x77, attempt read/write, report which addresses
respond with ACK. Include timeout handling and error reporting.
Useful for debugging I²C connections.
Lab Exercise
- Connect SHT31: VCC→3.3V, GND→GND, SDA→GPIO21, SCL→GPIO22
- Add 10kΩ pull-ups on SDA and SCL to 3.3V
- Run I²C address scanner — verify sensor at 0x44
- Read temperature register — verify reasonable values
- Read humidity register — verify 0–100% range
Mastery