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 ESP8266 | BH1750 lux sensor (GY-302) | Wire | Notes |
|---|---|---|---|
| 3V3 | VCC | red | |
| GND | GND | black | |
| D1 | SCL | yellow | GPIO5 — default SCL. |
| D2 | SDA | blue | GPIO4 — 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 (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);
}// 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
BH1750 ready (0x23, 1 lx resolution)
Light: 312.5 lx
Light: 318.3 lx
Light: 4.2 lxBH1750 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.