I2C wiring and an ESP32 address scanner
Check SDA, SCL and device addresses before debugging a sensor library.
Goal: locate a BH1750 sensor on a classic ESP32 DevKit I2C bus. Use an ESP32, matching data USB cable, BH1750 breakout and jumper wires. Disconnect USB before wiring. Connect sensor VCC to 3V3, GND to GND, SDA to GPIO21 and SCL to GPIO22. Other ESP32 variants may use different pins.
I2C uses SDA for data and SCL for clock. Devices share the lines and are addressed individually. The bus needs pull-up resistors; many breakout boards include them. Confirm the pull-up voltage is suitable for every connected device. In this example it must be 3.3V. Multiple breakout pull-ups in parallel can become too strong.
Upload this scanner with the esp32 board package installed. Place the following include directive on its own line:
#include <Wire.h>
void setup() { Serial.begin(115200); Wire.begin(21, 22, 100000); delay(500); int found = 0; for (int address = 8; address <= 119; address++) { Wire.beginTransmission(address); if (Wire.endTransmission() == 0) { Serial.print("Found address 0x"); if (address < 16) Serial.print("0"); Serial.println(address, HEX); found++; } } Serial.print("Devices found: "); Serial.println(found); } void loop() {}
Open Serial Monitor at 115200 baud and press reset. The BH1750 commonly appears at 0x23 with its address input LOW, or at 0x5C with it HIGH. If nothing appears, unplug power and check pin labels, the ground connection, sensor supply and swapped SDA/SCL.
An address response proves that a device acknowledged the bus, not that it is the intended model or that its measurements are correct. After scanning, upload the ESP32 Light Meter project and use the detected address. Devices with identical fixed addresses need a configurable address, a separate bus or a multiplexer.
References: https://docs.espressif.com/projects/arduino-esp32/en/latest/api/i2c.html and https://github.com/claws/BH1750