Home

Awesome

BH1750

Build Status<br>

This package contains an Arduino library for digital light sensor breakout boards containing the BH1750FVI IC.

The BH1750 board uses I2C for communication which requires two pins to communicate with the device. Configuring the I2C bus must be done in user code (not library code). This approach has been adopted so it can be done once and will better support the various options for different platforms.

A common module containing the BH1750 component is the GY-30 shown below.

GY-30 Module image

Overview

The BH1750 has six different measurement modes which are divided in two groups; continuous and one-time measurements. In continuous mode the sensor continuously measures lightness value. In one-time mode, the sensor makes only one measurement and then goes into Power Down mode.

Each mode has three different precisions:

By default, this library uses Continuous High Resolution Mode, but you can change this to a different mode by passing the mode argument to BH1750.begin().

When the One-Time mode is used your sensor will go into Power Down mode when it completes the measurement and you've read it. When the sensor is powered up again it returns to the default mode which means it needs to be reconfigured back into One-Time mode. This library has been implemented to automatically reconfigure the sensor when you next attempt a measurement so you should not have to worry about such low level details.

Usually you will get an integer value which represent the lux equivalent.

The sensor itself returns a 16 bit unsigned integer. Therefore the maximum value is limited in general. The standard conversion between the so called 'counts' to lux is 1/1.2, that means you get a smaller value. As we use float, if an error occurs you will get a negative value.

As the sensor counts impact of light in a specific time frame you could change this time frame. This is needed if you use an overlay window or compensate for environmental influence like darkness. This time frame is defined by a register which is called MTreg. Therefore you could choose a value between 32 and 254. The default value is 69; keep in mind that the measurement time is changed accordingly.

The datasheet for the BH1750 chip can be obtained here[2011.11 Rev.D]

Installation arduino-library-badge

The following YouTube video (specifically from 7:20 onwards) provides a good overview of manually installing this library and loading an example using the Arduino IDE.

BH1750 Video Tutorial

Information about the library installation process - https://www.arduino.cc/en/Guide/Libraries

Example

An example using the BH1750 library in conjunction with the GY-30 board (which contains the BH1750 component) is presented below. The example code uses the BH1750 library in the default continuous high precision mode when making light measurements.

Wiring

Connections:

The ADD pin is used to set the sensor I2C address. By default (if ADD voltage less than 0.7 * VCC) the sensor address will be 0x23. If it has voltage greater or equal to 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be 0x5C.

Wiring up the GY-30 sensor board to an Arduino is shown in the diagram below.

Example wiring diagram image

The image above was created using Fritzing and the GY-30 module was obtained from here.

Code

Upload the BH1750 test code to your Arduino.

#include <Wire.h>
#include <BH1750.h>

BH1750 lightMeter;

void setup(){

  Serial.begin(9600);

  // Initialize the I2C bus (BH1750 library doesn't do this automatically)
  // On esp8266 devices you can select SCL and SDA pins using Wire.begin(D4, D3);
  Wire.begin();

  lightMeter.begin();
  Serial.println(F("BH1750 Test"));

}

void loop() {

  float lux = lightMeter.readLightLevel();
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lx");
  delay(1000);

}

Output

Moving the sensor to face more light results in the lux measurements increasing.

BH1750 Test
Light: 70.0 lx
Light: 70.0 lx
Light: 59.0 lx
Light: 328.0 lx
Light: 333.0 lx
Light: 335.0 lx
Light: 332.0 lx

More Examples

The examples directory contains more advanced use cases such as using different modes, I2C addresses and multiple Wire instances.

Developers

The following information is for developers of this library.

Code Format

The code in this project is formatted using clang-format tool.

Good instructions for installing clang-format can be found here

Once the clang-format tool has been install you can then run the convenience script (ci/code-format.bash) to check or apply the code formatting. The script should be run from the repo's top level directory.

$ ./ci/code-format.bash

This script is also run as part of the project's continuous integration checks.

If you make changes to code files then the code format can be applied by simply passing apply as an argument to the script.

$ ./ci/code-format.bash apply

Code Linting

The code in this project is linted using arduino-lint. The tool can be installed using the instructions here.

To run the linter over the project use the command below.

$ arduino-lint --library-manager update --compliance strict

The same command is run as part of the project's continuous integration checks.

If errors or warnings are reported then fix them and re-run the script until they are resolved.

Build Locally

The code in this project can be built locally using the arduino-cli tool. The tool can be installed using the instructions here. Once you have the tool installed you can compile the examples scripts using the convenience script (ci/compile-examples.bash).

$ ./ci/compile-examples.bash

This script performs the same actions as the project's continuous integration compile checks.

Release Process