ESP32 with BH1750 Ambient Light Sensor

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.

ESP32 with BH1750 Ambient Light Sensor Arduino IDE Core Programming

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 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 Ambient Light Sensor Breakout Boards

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 Mode4 lux precision16 ms measurement time
High Resolution Mode1 lux precision120 ms measurement time
High Resolution Mode 20.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

BH1750 Ambient Light Sensor

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

BH1750 Ambient Light Sensor Pinout

Here’s the BH1750 Pinout:

VCCPowers the sensor (3.3V or 5V)
GNDCommon GND
SCLSCL 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.

ESP32 with BH1750 Ambient Light Sensor

You can connect the BH1750 sensor to the ESP32 using the default’s I2C pins:

BH1750ESP32
SCLGPIO 22
SDAGPIO 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:

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.

ESP32 BH1750 Wiring Diagram Circuit

You can also follow the next table:

BH1750ESP32
VCC3.3V
GNDGND
SCLGPIO 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.

BHT1750 Library Arduino IDE

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);
}

View raw code

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.

Arduino 2.0 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.

ESP32 with BH1750 Ambient Light Sensor Arduino IDE Demonstration

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:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

6 thoughts on “ESP32 with BH1750 Ambient Light Sensor”

  1. 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

    Reply
  2. 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

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.