Back to Tutorial
esp32-bh1750-light-sensor-lux-meter-iotwebplanet

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

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

SensorBH1750 ambient-light sensor
ModuleGY-302
MeasurementLight intensity
OutputDigital, in lux (lx)
CommunicationI²C
ESP32 compatibleYes
Best suited forAmbient-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

BH1750ESP32 30-Pin
VCC3.3V
GNDGND
SDAGPIO 21
SCLGPIO 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

  1. Smart street light
  2. Automatic room lighting
  3. Smart classroom lighting monitor
  4. ESP32 IoT weather station
  5. Solar panel monitoring system
  6. Smart greenhouse
  7. Plant growth monitoring system
  8. Automatic OLED/TFT brightness controller
  9. Smart-home lighting automation
  10. Portable IoT lux meter
  11. Solar tracker monitoring
  12. Museum lighting monitor
  13. Library reading-light monitor
  14. Smart agriculture environmental station
  15. 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.

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial