Measuring illuminance with light intensity sensor module (BH1750FVI) and ESP32 | そう備忘録

# Measuring illuminance with light intensity sensor module (BH1750FVI) and ESP32

Contents

## BH1750FVI

Article on the measurement of illuminance with the ESP-WROOM-32 module using the GY-302, a modularized illuminance sensor (BH1750FVI) from ROHM Co.

The GY-302 has the following features

• Interface: I2C
• I2C Address: 0x23 or 0x5C
• Illuminance: 0.11 to 100,000 lux
• Voltage: 3.0 to 5.0 V (3.3 V was used in this case)

### Measurement Mode

There are three modes of measurement as follows

In addition to the above modes, there are two other modes, Continuously and One Time, so there are 3 x 2 = 6 possible combinations.

### MTReg

The sensor sensitivity can be adjusted with the register that adjusts the measurement time.

The default setting is 69, with a range of 31 to 254.

A higher value (254) will increase the measurement time and allow for finer sensitivity, but will lower the maximum measurable illuminance.

In H-Resolution Mode, the maximum illuminance is 14,836 lux, and in Mode 2, the maximum illuminance is 7,418 lux, which is half of the maximum, making it unsuitable for outdoor measurements.

On the other hand, indoors, there was no problem with the default setting of 69.

#### Calculation Formula

The following is a description of the calculation formulas performed inside the library.

In H-Resolution Mode and Mode2, the sensitivity is calculated by the following formula.

``````H-reslution mode : Illuminance per 1 count ( lx / count ) = 1 / 1.2 *( 69 / X )
H-reslution mode2 : Illuminance per 1 count ( lx / count ) = 1 / 1.2 *( 69 / X ) / 2``````

X : MTreg value

Ex.)

When X (MTreg) is 254 in H-Resolution Mode

``````1 / 1.2 *( 69 / 254 ) = 0.2263・・・ ≒ 0.23
``````

Measured in 0.23 lux units. (Half of that when Mode 2 is selected)

The same formula is used to calculate the unit of 1.85 lux when MTreg is 31.

The value measured by the sensor is returned in High Byte and Low Byte as shown below (quoted from BH1750FVI datasheet).

The powers of 2 where the bit is 1 are added together and divided by 1.2 (fixed).

The above ex) is a calculation example when MTreg is set to the default value of 69, but if all bits are set to 1 and MTreg is set to 254, the calculation formula for H-Resolution Mode is as follows

``(2^15 + 2^14 + 2^13 + 2^12 + 2^11 + 2^10 + 2^9 + 2^8 + 2^7 + 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0) / 1.2 *( 69 / 254 ) = 14835.67913・・・ ≒ 14,836``

The above formula indicates that a maximum of 14,836 lux can be measured.

In Mode 2, the maximum measurable illuminance is about 7,400 lux, which is half of the maximum measurable illuminance. Therefore, care should be taken when measuring illuminance in bright places with high sensitivity.

### Connectors

The following five connectors are available.

• VCC: 3 to 5V
• GND: GND
• SCL: I2C connection
• SDA: Same as above

When ADDR is connected LOW (0.3V or lower), the value is 0x23; when ADDR is connected HIGH (0.7V or higher), the value is 0x5C.

## Other Parts

Other major parts prepared are as follows.

### ESP32 module

Various ESP32 modules are available on the market, but the ESP-WROOM-32 module we used this time is from the following product.

The pin layout is the pin layout of ESP32 DEVKIT V1 (30 pins).

### Silicon Wire

Recently purchased silicon wire (24 AWG)

Silicon is soft and very comfortable to use.

The temperature range of -60 to +200 C is also very nice.

## Preliminary Preparation

Arduino IDE (Ver 1.8.19) was used as the development environment.

Install the necessary libraries in advance.

### BH1750 Library

Start the Arduino IDE and launch the Library Manager by going to Sketch->Include Library->Manage Libraries.

Type “BH1750” in the search field to search and installed the library that appears at the top of the list.

Github is here.

This library does the calculations I just described internally, so all I need to do from the sketch is to call the function that reads the MTreg set and the illuminance.

## Wiring Diagram

The wiring diagram is as follows.

## Sketch

The following is a sketch of the illuminance measured and displayed on the console approximately every 10 seconds.

### bh1750.ino

``````/*
* BH1750 test
*
*/
#include <Wire.h>
#include <BH1750.h>

// I2C Pin
constexpr short int SDA_PIN = 21;
constexpr short int SCL_PIN = 22;

BH1750 lightMeter(0x23);

float lux;

void setup() {
Serial.begin(115200);
Wire.begin(SDA_PIN, SCL_PIN); // I2C

/*
* 6 Mode
*   CONTINUOUS_HIGH_RES_MODE
*   CONTINUOUS_HIGH_RES_MODE_2
*   CONTINUOUS_LOW_RES_MODE
*   ONE_TIME_HIGH_RES_MODE
*   ONE_TIME_HIGH_RES_MODE_2
*   ONE_TIME_LOW_RES_MODE
*
*/
lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE_2);
}

void loop() {
if (lightMeter.setMTreg(69)) { // set MTreg(Measurement Time)
Serial.println("MTreg Normal Setting(69)");
} else {
Serial.println("MTreg setting error");
}
delay(500);

Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
} else {
Serial.println(F("--------------------------------------"));
}
delay(10000);
}``````

### supplementary explanation

#### setup()

Initial processing of I2C connection and initial processing of BH1750FVI.

The following six modes can be set.

• CONTINUOUS_HIGH_RES_MODE
• CONTINUOUS_HIGH_RES_MODE_2
• CONTINUOUS_LOW_RES_MODE
• ONE_TIME_HIGH_RES_MODE
• ONE_TIME_HIGH_RES_MODE_2
• ONE_TIME_LOW_RES_MODE

#### lightMeter.setMTreg(69)

MTreg is set to the standard 69.

The above sketch is an example of a single measurement with standard settings, but if you want to measure as accurately as possible in high-resolution mode from very bright to very dark locations, you can use the

1. Set MTreg to 69 and measure in high resolution mode 2
2. If the measurement result is less than 10 lux, go to 4.
3. If the result is more than 5,000 lux, go to 5. Otherwise, go to 6.
4. If MTreg is set to 254, go to 7.
5. Set MTreg to 32 and go to 7.
6. Set MTreg to 69 and go to 7.
7. Measure again in High Resolution 2 mode

The measurement may be performed in two steps, such as

## Execution Result

The results are as follows.

The measurement results are displayed on the console, and a high illuminance was measured when the sensor was illuminated by an LED light. 