The BH1750 is a digital light intensity sensor capable of measuring light in lux (lumens per square meter). Its high resolution and ease of use make it an excellent choice for projects that require accurate light measurement. In this tutorial, we will explore how the BH1750 works, how to calculate lux values, and how to interface the sensor with an ESP32 microcontroller.
How the BH1750 Works
The BH1750 uses a photodiode to measure the intensity of light. The sensor outputs the light intensity in lux, which represents the amount of light falling on a square meter of surface. The BH1750 has two operating modes:
- Continuously High-Resolution Mode: Provides measurements at a 1-lux resolution with a typical measurement time of 120 ms.
- Continuously Low-Resolution Mode: Provides measurements at a 4-lux resolution with a typical measurement time of 16 ms.
The device communicates with a microcontroller via the I2C protocol and outputs 16-bit digital data representing the measured light intensity. The lux value is calculated using a specific formula based on the sensor’s output.
Connecting BH1750 to ESP32
The BH1750 operates over the I2C protocol, requiring only two pins for communication: SCL (clock) and SDA (data). Here’s how to connect the BH1750 to an ESP32:
BH1750 Pin | ESP32 Pin |
---|---|
VCC | 3.3V |
GND | GND |
SDA | GPIO21 |
SCL | GPIO22 |
The GY-30 BH1750 module includes an ADD pin which is used to set the I2C address of the sensor. If this pin is connected to VCC (3.3V in this case), the sensor address will be 0x5C. Conversely, if the ADD pin is left floating or connected to GND, the I2C address will be 0x23.
Ensure pull-up resistors (typically 4.7 kΩ) are connected to the SDA and SCL lines if not already integrated into your module.
Required Components
- ESP32 Development Board
- BH1750 Sensor Module
- Jumper Wires
- Breadboard (optional)
Setting Up the Arduino IDE
- Install the Arduino IDE if you haven’t already.
- Install the ESP32 board package by navigating to File > Preferences, and add the following URL to the “Additional Board Manager URLs” field:https://dl.espressif.com/dl/package_esp32_index.json
Then, go to Tools > Board > Board Manager and install the ESP32 package.
- Install the BH1750 library by going to Sketch > Include Library > Manage Libraries, searching for “BH1750”, and installing the library by Christopher Laws.
Example Code
Here is an example sketch for interfacing the BH1750 with an ESP32:
#include <BH1750.h>
#include <Wire.h>
BH1750 lightMeter;
void setup() {
Serial.begin(9600);
// Initialize the I2C bus (BH1750 library doesn't do this automatically)
Wire.begin();
// On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
// For Wemos / Lolin D1 Mini Pro and the Ambient Light shield use
// Wire.begin(D2, D1);
lightMeter.begin();
Serial.println(F("BH1750 Test begin"));
}
void loop() {
float lux = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
delay(1000);
}
Explanation of the Code
- Initialization: The BH1750 object lightMeter is created, and the I2C bus is initialized with Wire.begin().
- Sensor Setup: The lightMeter.begin() function initializes the sensor.
- Reading Lux Values: The lightMeter.readLightLevel() function reads the current light intensity in lux.
- Output: The lux value is printed to the Serial Monitor every second.
Testing the Setup
- Connect the ESP32 and BH1750 as described.
- Upload the example code to your ESP32 using the Arduino IDE.
- Open the Serial Monitor (Ctrl+Shift+M) and set the baud rate to 115200.
- Observe the light intensity readings in lux.
Applications of BH1750
- Automatic lighting systems
- Greenhouse monitoring
- Photography exposure settings
- Solar panel efficiency monitoring
Conclusion
In this tutorial, we covered how the BH1750 sensor works, calculated lux values, and interfaced it with an ESP32. The BH1750’s simplicity and accuracy make it a powerful tool for projects involving light measurement. Experiment with different lighting conditions to observe how the sensor reacts and integrate it into your next project!