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 NodeMCU ESP8266 — I2C wiring and lux code

Wire a BH1750 (GY-302) light sensor to a NodeMCU ESP8266 on D1/D2 and read lux over I2C, keeping the single A0 analog pin free.

Arduino C++Verified — core esp8266 3.1.2, BH1750 1.3.0, 3 Nov 2026Compile-only

Community draft — report a mistake

A BH1750 tells a NodeMCU how bright it is in proper lux — ideal for automatic street-lights, plant grow lights or switching a display’s brightness.

It is an I2C sensor, so it goes on the ESP8266’s default bus: SCL to D1, SDA to D2, powered from 3V3. The bigger win on this board is that it leaves A0, the only analog input, free for something that really needs it.

Wiring

NodeMCU ESP8266Only the pins used are shown · 3.3 V logicBH1750 lux sensor (GY-302)GND3V3D1D2VCCGNDSCLSDAADDR (not connected)
Every connection
NodeMCU ESP8266BH1750 lux sensor (GY-302)WireNotes
3V3VCCred
GNDGNDblack
D1SCLyellowGPIO5 — default SCL.
D2SDAblueGPIO4 — default SDA.

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

  • Why not an LDR? The ESP8266 has only one analog input (A0). The BH1750 measures light over I2C in real lux, so A0 stays free for a soil or gas sensor.
  • ADDR open or to GND = 0x23; ADDR to 3V3 = 0x5C. Two BH1750s can share D1/D2 that way.
  • Do not move SDA/SCL to D3, D4 or D8: the GY-302’s pull-ups would hold D8 high at boot and the NodeMCU would not start.
  • High-resolution mode updates about every 120 ms. In direct Indian sunlight the reading tops out around 54,612 lx — it saturates rather than giving a wrong number.

Code

bh1750-nodemcu.inoArduino C++Open in PlaygroundOpen in PlaygroundFlash
// BH1750 (GY-302) light sensor on a NodeMCU (ESP8266).
// VCC → 3V3, GND → GND, SCL → D1 (GPIO5), SDA → D2 (GPIO4). ADDR open = address 0x23.
// Using I2C for light leaves A0 — the ESP8266's only analog input — free for another sensor.
#include <Wire.h>
#include <BH1750.h>

BH1750 lightMeter(0x23);

void setup() {
  Serial.begin(115200);
  Serial.println();
  Wire.begin();  // SDA = D2, SCL = D1
  if (!lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
    Serial.println("BH1750 not found - check wiring, or try address 0x5C (ADDR to 3V3)");
    while (true) delay(100);
  }
  Serial.println("BH1750 ready (0x23, 1 lx resolution)");
}

void loop() {
  if (lightMeter.measurementReady()) {
    float lux = lightMeter.readLightLevel();
    Serial.printf("Light: %.1f lx\n", lux);
  }
  delay(1000);
}

Library: BH1750 (Christopher Laws) — compiled with BH1750 1.3.0.

Expected serial output

Serial Monitor · 115200Output
BH1750 ready (0x23, 1 lx resolution)
Light: 312.5 lx
Light: 318.3 lx
Light: 4.2 lx

Common errors

“BH1750 not found”

Check SDA (D2) and SCL (D1), and try 0x5C if ADDR is tied high. The I2C scanner shows what answers.

Reading is always 0 or 54612

Covered or saturated sensor, or a 3.3 V supply missing. Point it at the room light and check VCC.

↑↓ to move · Enter to open