The BH1750 measures light in lux, which makes it the right sensor for automatic street lights, curtain openers and grow-light timers.
It sits on the ESP32’s I2C pins at 3.3 V. The sketch runs it in continuous high-resolution mode (1 lx steps) and prints a reading every second.
Wiring
| ESP32 DevKit V1 | BH1750 lux sensor (GY-302) | Wire | Notes |
|---|---|---|---|
| 3V3 | VCC | red | |
| GND | GND | black | |
| D22 | SCL | yellow | |
| D21 | SDA | blue | |
| GND | ADDR | black | ADDR low → address 0x23. Tie it to 3V3 for 0x5C. |
3–5 V (on-board regulator) part, 3.3 V board. The GY-302 has a 3.3 V regulator and accepts 3–5 V on VCC; its pull-ups suit both 3.3 V and 5 V boards.
Board quirks
- Unlike an LDR, the BH1750 reports real lux values (1–65535 lx) — no calibration needed.
- It shares the I2C bus happily with a BME280 or an OLED on the same two pins.
- Keep the sensor window clear of the board’s LEDs, or the reading includes your own lights.
Code
// BH1750 ambient light sensor on an ESP32 DevKit V1.
// VCC → 3V3, GND → GND, SDA → D21, SCL → D22, ADDR → GND (address 0x23).
#include <Wire.h>
#include <BH1750.h>
BH1750 lightMeter(0x23);
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA, SCL
if (!lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println("BH1750 not found at 0x23 — if ADDR is tied high, use 0x5C");
while (true) delay(1000);
}
Serial.println("BH1750 ready");
}
void loop() {
if (lightMeter.measurementReady()) {
Serial.printf("Light: %.1f lx\n", lightMeter.readLightLevel());
}
delay(1000);
}// BH1750 ambient light sensor on an ESP32 DevKit V1.
// VCC → 3V3, GND → GND, SDA → D21, SCL → D22, ADDR → GND (address 0x23).
#include <Wire.h>
#include <BH1750.h>
BH1750 lightMeter(0x23);
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA, SCL
if (!lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println("BH1750 not found at 0x23 — if ADDR is tied high, use 0x5C");
while (true) delay(1000);
}
Serial.println("BH1750 ready");
}
void loop() {
if (lightMeter.measurementReady()) {
Serial.printf("Light: %.1f lx\n", lightMeter.readLightLevel());
}
delay(1000);
}Library: BH1750 (Christopher Laws) — compiled with BH1750 1.3.0.
Expected serial output
BH1750 ready
Light: 312.5 lx
Light: 315.0 lx
Light: 4.2 lxBH1750 ready Light: 312.5 lx Light: 315.0 lx Light: 4.2 lx
Common errors
“BH1750 not found at 0x23”
Check ADDR: if it is tied high, the address is 0x5C. Then run the I2C scanner.
Always reads 54612 lx
The sensor is saturated in direct sunlight on high-resolution mode; switch to a lower-resolution mode outdoors.