The BH1750 is a 16-bit ambient light sensor. In this guide, you’ll learn how to use the BH1750 ambient light sensor with the ESP32 board using Arduino IDE. The sensor communicates with a microcontroller using I2C communication protocol.
You’ll learn how to wire the sensor to the ESP32 board, install the required libraries and use a simple sketch to display the sensor readings in the Serial Monitor.
This tutorial covers the following topics:
- Introducing the BH1750 Ambient Light Sensor
- BH1750 Pinout
- BH1750 I2C Interface
- Example: BH1750: Read Ambient Light with ESP32
Introducing BH1750 Ambient Light Sensor
The BH1750 is a 16-bit ambient light sensor that communicates via I2C protocol. It outputs luminosity measurements in lux (SI-derived unit of illuminance). It can measure a minimum of 1 lux and a maximum of 65535 lux.
The sensor may come in different breakout board formats. See pictures below. Both images represent a BH1750 sensor.
BH1750 Features
Here’s a list of the BH1750 sensor features. For more information consult the BH1750 sensor datasheet.
- I2C bus Interface
- Spectral responsibility is approximately human eye response
- Illuminance to digital converter
- Range: 1 – 65535 lux
- Low current by power down function
- 50Hz / 60Hz Light noise reject-function
- It is possible to select 2 different I2 C slave addresses
- Small measurement variation (+/- 20%)
- The influence of infrared is very small
- Supports continuous measurement mode
- Supports one-time measurement mode
Measurement Modes
The sensor supports two different measurement modes: continuous measurement mode, and one-time measurement mode. Each mode supports three different resolution modes.
Low Resolution Mode | 4 lux precision | 16 ms measurement time |
High Resolution Mode | 1 lux precision | 120 ms measurement time |
High Resolution Mode 2 | 0.5 lux precision | 120 ms measurement time |
In continuous measurement mode, the sensor continuously measures ambient light values. In one-time measurement mode, the sensor measures the ambient light value once, and then it goes to power down mode.
Applications
The BH1750 is an ambient light sensor so it can be used in a wide variety of projects. For example:
- to detect if it is day or night;
- to adjust or turn on/off LED’s brightness accordingly to ambient light;
- to adjust LCDs and screen’s brightness;
- to detect if an LED is lit;
- …
BH1750 Pinout
Here’s the BH1750 Pinout:
VCC | Powers the sensor (3.3V or 5V) |
GND | Common GND |
SCL | SCL pin for I2C communication |
SDA (Data) | SDA pin for I2C communication |
ADD* | Selects address |
The ADD pin is used to set the I2C sensor address. If the voltage on that pin is less than 0.7VCC (pin is left floating or connected to GND), the I2C address is 0x23. But, if the voltage is higher than 0.7xVCC (pin is connected to VCC), the address is 0x5C. In summary:
- ADD pin floating or connected to GND → address: 0x23
- ADD pin connected to VCC → address: 0x5C
BH1750 I2C Interface
The BH1750 ambient light sensor supports I2C interface.
You can connect the BH1750 sensor to the ESP32 using the default’s I2C pins:
BH1750 | ESP32 |
SCL | GPIO 22 |
SDA | GPIO 21 |
GPIO 22 and GPIO 21 are the ESP32 default I2C pins. You can use other pins as long as you set them properly on code.
BH1750: Read Ambient Light with ESP32
Now that you are more familiar with the BH1750 sensor, let’s test it. In this section, we’ll build a simple project that reads the ambient light and displays it in the Arduino IDE Serial Monitor.
Parts Required
To complete this tutorial you need the following parts:
- BH1750 ambient light sensor
- ESP32 (read Best ESP32 development boards)
- Breadboard (optional)
- Jumper wires (optional)
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Schematic – ESP32 with BH1750
Wire the BH1750 sensor to the ESP32 I2C pins. You can follow the next schematic diagram.
You can also follow the next table:
BH1750 | ESP32 |
VCC | 3.3V |
GND | GND |
SCL | GPIO 22 |
SDA (Data) | GPIO 21 |
ADD* | Don’t connect |
By not connecting the ADD pin, we’re selecting 0x23 I2C address. Connect it to 3.3V to select 0x5C address instead.
Preparing Arduino IDE
We’ll program the ESP32 board using Arduino IDE. So, make sure you have the ESP32 add-on installed. Follow the next tutorial:
Installing the BH1750 Library
There are several libraries to read from the BH1750 sensor. We’ll use the BH1750 library by Christopher Laws. It is compatible with the ESP32, ESP8266, and Arduino.
Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries. The Library Manager should open.
Search for “BH1750” on the search box and install the BH1750 library by Christopher Laws.
Code – Reading BH1750 Ambient Light Sensor
Copy the following code to your Arduino IDE. This code simply reads ambient light in lux and displays the values on the Serial Monitor. It is the example code from the library called BH1750test (you can access it in File > Examples > BH1750 > BH1750test
/*
Example of BH1750 library usage. This example initialises the BH1750 object using the default high resolution continuous mode and then makes a light level reading every second.
*/
#include <Wire.h>
#include <BH1750.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);
}
The library also provides other examples worth exploring.
How the Code Works
We start by including the required libraries. The Wire.h library to use I2C communication protocol and the BH1750.h library to read from the sensor.
#include <Wire.h>
#include <BH1750.h>
Then, we create a BH1750 object called lightMeter.
BH1750 lightMeter;
In the setup(), initialize the Serial Monitor at a baud rate of 9600.
Serial.begin(9600);
Initialize I2C communication protocol. It will start an I2C communication on the microcontroller’s default I2C pins. If you want to use different I2C pins, pass them to the begin() method like this Wire.begin(SDA, SCL).
Wire.begin();
Initialize the sensor using the begin() method on the BH1750 object (lightMeter).
lightMeter.begin();
In the loop(), we create a variable called lux, that saves the luminance values. To get the value, you simply call the readLightLevel() function on the BH1750 object (lightMeter).
float lux = lightMeter.readLightLevel();
Finally, display the measurement on the Serial Monitor.
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
You get and print a new reading every second.
delay(1000);
Demonstration
Now, you can upload the code to your board. First, connect your board to your computer. Then, go to Tools > Board and select the ESP32 board you’re using. Go to Tools > Port and select the COM port your board is connected to. Finally, click on the upload button.
After successfully uploading the code, open the Serial Monitor at a baud rate of 9600 and press the ESP32 on-board RST button.
New luminance readings should be printed in the Serial Monitor.
Other Useful Functions
The library we’re using with the BH1750 sensor provides other examples that illustrate other useful functions and features. You can check all BH1750 library examples here.
Setting Measurement Mode
By default, the library uses the continuous high resolution measurement mode, but you can change it by passing the desired measurement mode to the begin() method when initializing the sensor. For example:
lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)
Here’s a list of all available modes:
- BH1750_CONTINUOUS_LOW_RES_MODE
- BH1750_CONTINUOUS_HIGH_RES_MODE (default)
- BH1750_CONTINUOUS_HIGH_RES_MODE_2
- BH1750_ONE_TIME_LOW_RES_MODE
- BH1750_ONE_TIME_HIGH_RES_MODE
- BH1750_ONE_TIME_HIGH_RES_MODE_2
See the properties of each mode in this previous section.
Wrapping Up
In this tutorial, you’ve learned how to use the BH1750 ambient light sensor with the ESP32. The sensor is very easy to use. It uses I2C communication protocol, which makes wiring simple, and the library provides methods to easily get the readings.
We hope you found this tutorial useful. Tell us in the comments below in which project would you use the BH1750 sensor.
We have tutorials for other sensors with the ESP32 board that you may like:
- ESP32 with BMP388: Altimeter Sensor
- ESP32 with DS18B20: Temperature Sensor
- ESP32 with BME680: Gas, Pressure, Humidity, and Temperature Sensor
- ESP32 with BME280: Temperature, Humidity, and Pressure Sensor
- ESP32 DHT11/DHT22: Temperature , and Humidity Sensor
- ESP32 HC-SR04: Ultrasonic Distance Sensor
- ESP32 PIR: Motion Sensor
- ESP32 BMP180: Pressure Sensor
Thanks for reading.
Please make a video on the lilly go TTGO esp32 with the sim7600. Posting data to the cloud the sim800 one only supports 2g network
Hello all, I want to ask, I have a problem when I use the sensor module bh1750 and ESP32. Where on the Arduino IDE serial monitor there is a message like the following “Device is not configured! Light: -2.00 lx”. When I searched for the solution to the problem, it was at the following link https://github.com/claws/BH1750/issues/36 and other links. With NodeMCU, the bh1750 sensor module can measure light intensity, whereas when I use the ESP32, the BH1750 sensor module can’t measure. Can anyone give a solution? #ask
With the NodeMCU, I get “Device not Configured”. How do I fix that, please?
Stupid question perhaps.
But is IIC the same as I2c ?
Greetings bert
Hi.
Yes. It is the same.
Regards,
Sara
Thank you Sara
Example code does not compile, gives out of range error when you try to implement the HIGH_RES_CONTINUOUS_MODE_2. Works correctly with default value for resolution.
i.e. modifying the lightMeter.begin() line to
lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE_2). (tried it with and without the :: and using the underscore _ instead, as shown in the table of commands. Neither would compile and produced an out of range error.
Not very sensitive when using the “light ball” version. Apparently the white ball (plastic hemisphere) attenuates the ambient light severely. I thought it would be much more transparent, and thus much more sensitive to ambient light.
Direct, very bright Braun white multi LED flashlight only a few inches away, (far brighter than sunlight), only gives about 600 lux, thus not using most of the dynamic range of the sensor, and reduced accuracy as a result, for solar light monitoring.