BeginnerIoT about 50 min

ESP32 Light Meter

Read room illuminance in lux with a BH1750 and compare lighting conditions in the Serial Monitor.

ESP32 Light Meter
Illustrative cover. Follow the written wiring steps.

Steps

  1. Prepare the board and sensor

    Use the catalog classic ESP32 DevKit with GPIO21 and GPIO22. Other ESP32 variants may need different pins. You need a data-capable USB cable matching your board connector; this is an extra item outside the listed parts. Power the BH1750 breakout from 3.3V so its I2C pull-ups remain compatible with ESP32 logic.

  2. Wire the I2C bus

    Unplug USB. Connect BH1750 VCC to ESP32 3V3, GND to GND, SDA to GPIO21 and SCL to GPIO22. If the board exposes ADD or ADDR, connect it to GND for address 0x23. If it is tied HIGH, use 0x5C instead. Confirm the breakout pin labels and voltage rating.

  3. Install board support and library

    Install the esp32 board package by Espressif Systems and the BH1750 library by Christopher Laws in Arduino IDE. Select the board and port. The sketch uses the library begin(), measurementReady() and readLightLevel() APIs, and a 100kHz I2C bus.

  4. Upload and compare readings

    Open Serial Monitor at 115200 baud. After initialization succeeds, a new lux value is printed about twice per second. Compare the same sensor position under a room light, near a window, and covered by your hand. Avoid interpreting readings as a certified lighting assessment.

  5. Troubleshoot the bus

    If initialization fails, disconnect power and check SDA/SCL, common ground, 3.3V power and address. Then use the I2C scanner in the companion lesson to locate 0x23 or 0x5C. A negative lux result is treated as a read error; it is not a valid light measurement.

Code

#include <Wire.h>
#include <BH1750.h>
#include <stdint.h>
const byte SENSOR_ADDRESS = 0x23;
BH1750 lightMeter;
bool sensorReady = false;
uint32_t lastSample = 0;

void setup() {
  Serial.begin(115200);
  if (!Wire.begin(21, 22, 100000)) {
    Serial.println("I2C initialization failed");
    return;
  }
  sensorReady = lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE,
                                SENSOR_ADDRESS, &Wire);
  Serial.println(sensorReady ? "Light meter ready" : "BH1750 not found: check wiring/address");
}

void loop() {
  if (!sensorReady) { delay(1000); return; }
  uint32_t now = millis();
  if ((uint32_t)(now - lastSample) < 500) return;
  if (!lightMeter.measurementReady()) return;
  lastSample = now;
  float lux = lightMeter.readLightLevel();
  if (lux < 0) { Serial.println("Sensor read error"); return; }
  Serial.print("Light: "); Serial.print(lux, 1); Serial.println(" lx");
}
Notes. Classic ESP32 DevKit target. Requires a matching data USB cable not included in the parts list. Keep sensor supply and I2C pull-ups at 3.3V. Educational readings, not calibrated certification. Cover is illustrative.