ESP32 with BH1750 Light Sensor – Measure Light Intensity in Lux
The BH1750 GY-302 Light Intensity Sensor is a digital ambient-light sensor that works very well with ESP32. Unlike a basic LDR, the BH1750 provides a digital light measurement directly in lux (lx), making it useful for smart lighting, weather stations, automatic brightness control, solar monitoring, greenhouse automation and IoT projects. Buy the GY-302 BH1750 Light Intensity Module from IoTWebPlanet.
Components Required
- ESP32 Development Board
- GY-302 BH1750 Light Intensity Module – Buy from IoTWebPlanet
- Jumper wires
- Breadboard (optional)
- USB cable
What is the BH1750 Sensor?
The BH1750 measures visible ambient light and reports the result in lux. It communicates with the ESP32 using I²C, so only SDA and SCL communication lines are needed.
Key Features
| Sensor | BH1750 ambient-light sensor |
| Module | GY-302 |
| Measurement | Light intensity |
| Output | Digital, in lux (lx) |
| Communication | I²C |
| ESP32 compatible | Yes |
| Best suited for | Ambient-light measurement and automation |
Why Use BH1750 Instead of an LDR?
An LDR is useful for simple bright/dark detection. The BH1750 is better when a project needs a meaningful digital light measurement. Instead of an arbitrary ADC value, the ESP32 can report a reading such as 328.5 lux.
ESP32 to BH1750 Wiring
| BH1750 | ESP32 30-Pin |
|---|---|
| VCC | 3.3V |
| GND | GND |
| SDA | GPIO 21 |
| SCL | GPIO 22 |
Circuit / Wiring Diagram
ESP32 3.3V ───────── BH1750 VCC
ESP32 GND ───────── BH1750 GND
ESP32 GPIO21 ───────── BH1750 SDA
ESP32 GPIO22 ───────── BH1750 SCL
ESP32 30-Pin GY-302 BH1750
┌──────────────┐ ┌──────────────┐
3.3V │ ●────────────┼─────────────►│ VCC │
GND │ ●────────────┼─────────────►│ GND │
21 │ ●────────────┼─────────────►│ SDA │
22 │ ●────────────┼─────────────►│ SCL │
└──────────────┘ └──────────────┘Arduino IDE Library
Open Arduino IDE → Library Manager, search for BH1750, and install the BH1750 library by Christopher Laws. The ESP32 Arduino core already provides Wire.h for I²C communication.
Complete ESP32 BH1750 Code
#include <Wire.h>
#include <BH1750.h>
#define SDA_PIN 21
#define SCL_PIN 22
BH1750 lightMeter;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("IoTWebPlanet - ESP32 + BH1750");
Wire.begin(SDA_PIN, SCL_PIN);
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println("BH1750 initialized successfully.");
} else {
Serial.println("Error initializing BH1750. Check wiring.");
}
}
void loop() {
if (lightMeter.measurementReady()) {
float lux = lightMeter.readLightLevel();
Serial.print("Light Intensity: ");
Serial.print(lux);
Serial.println(" lux");
if (lux < 10) Serial.println("Condition: Very Dark");
else if (lux < 100) Serial.println("Condition: Dim");
else if (lux < 500) Serial.println("Condition: Normal Indoor Light");
else if (lux < 10000) Serial.println("Condition: Bright");
else Serial.println("Condition: Very Bright / Outdoor");
}
delay(1000);
}How the Code Works
Wire.h handles I²C communication while the BH1750 library initializes the sensor and converts its measurement into lux. The ESP32 starts I²C on GPIO 21 (SDA) and GPIO 22 (SCL). The program reads the lux value every second and also provides a simple descriptive lighting condition.
Expected Serial Monitor Output
BH1750 initialized successfully. Light Intensity: 342.50 lux Condition: Normal Indoor Light Light Intensity: 825.83 lux Condition: Bright
Troubleshooting
- Sensor not detected: Check VCC, GND, SDA and SCL and run an I²C scanner.
- Reading does not change: Move the sensor between dark and bright locations and ensure the sensing area is unobstructed.
- BH1750.h not found: Install the BH1750 library from Arduino IDE Library Manager.
- ESP32 does not boot: Disconnect the module and check for shorts, especially between 3.3V and GND.
15 ESP32 + BH1750 Project Ideas
- Smart street light
- Automatic room lighting
- Smart classroom lighting monitor
- ESP32 IoT weather station
- Solar panel monitoring system
- Smart greenhouse
- Plant growth monitoring system
- Automatic OLED/TFT brightness controller
- Smart-home lighting automation
- Portable IoT lux meter
- Solar tracker monitoring
- Museum lighting monitor
- Library reading-light monitor
- Smart agriculture environmental station
- Cloud-based light data logger
Frequently Asked Questions
Can BH1750 work with ESP32?
Yes. The BH1750 uses I²C and interfaces easily with ESP32.
What does BH1750 measure?
It measures ambient light intensity and reports the measurement in lux (lx).
Which ESP32 pins should I use?
For a typical ESP32 DevKit, GPIO 21 is commonly used for SDA and GPIO 22 for SCL. ESP32 can also configure other suitable GPIOs for I²C.
Is BH1750 better than an LDR?
For simple light/dark detection an LDR is inexpensive and effective. When you need a digital measurement directly in lux, BH1750 is more convenient.
Conclusion
The GY-302 BH1750 is a useful ESP32 sensor for projects that require quantitative ambient-light measurement. Its simple I²C interface makes it beginner-friendly, while ESP32 Wi-Fi makes it easy to extend the project into smart lighting, agriculture, energy monitoring and cloud-connected IoT applications.

Leave a Reply
You must be logged in to post a comment.