Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.

BH1750 light sensor with ESP32 — lux readings over I2C

Wire a BH1750 ambient light sensor to an ESP32 DevKit V1 on GPIO21/22 and read calibrated light levels in lux in Arduino C++.

Arduino C++Verified — core esp32 3.3.12, BH1750 1.3.0, 3 Nov 2026Compile-only

Community draft — report a mistake

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 V1Only the pins used are shown · 3.3 V logicBH1750 lux sensor (GY-302)GNDD22D213V3VCCGNDSCLSDAADDR
Every connection
ESP32 DevKit V1BH1750 lux sensor (GY-302)WireNotes
3V3VCCred
GNDGNDblack
D22SCLyellow
D21SDAblue
GNDADDRblackADDR 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-esp32.inoArduino C++Open in PlaygroundOpen in PlaygroundFlash
// 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

Serial Monitor · 115200Output
BH1750 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.

↑↓ to move · Enter to open