Home

Awesome

BH1750 digital light sensor library for Arduino

Build Status

This is a 16-bit BH1750 digital ambient light sensor on a GY-302 breakout PCB:

BH1750

Arduino library features

BH1750 sensor specifications

GY-302 breakout specifications

Hardware

Schematic BH1750 and Arduino UNO

Connection Arduino UNO board - BH1750

Pins board - BH1750VCCGNDSDASCL
Arduino UNO (ATMega328 boards)5VGNDA4A5
Arduino Mega25605VGNDD20D21
Arduino Leonardo5VGNDD2D3
Arduino DUE (ATSAM3X8E)3V3GND2021
ESP82663V3GNDGPIO4 (D2)GPIO5 (D1)
ESP323V3GNDGPIO21GPIO22

Note: Tested ESP8266 / ESP32 boards:

Other unlisted MCU's may work, but are not tested.

WeMos LOLIN32 with OLED display

Change the following Wire initialization to:

// WeMos LOLIN32 with OLED support
Wire.begin(5, 4);

I2C address

Note: ADDR pin may be floating (open) which is the same as LOW.

Examples

Examples | Erriez BH1750:

Documentation

Example continues conversion high resolution

#include <Wire.h>
#include <ErriezBH1750.h>

// ADDR line LOW/open:  I2C address 0x23 (0x46 including R/W bit) [default]
// ADDR line HIGH:      I2C address 0x5C (0xB8 including R/W bit)
BH1750 sensor(LOW);

void setup()
{
  Serial.begin(115200);
  Serial.println(F("BH1750 continues measurement high resolution example"));

  // Initialize I2C bus
  Wire.begin();

  // Initialize sensor in continues mode, high 0.5 lx resolution
  sensor.begin(ModeContinuous, ResolutionHigh);

  // Start conversion
  sensor.startConversion();
}

void loop()
{
  uint16_t lux;

  // Wait for completion (blocking busy-wait delay)
  if (sensor.isConversionCompleted()) {
    // Read light
    lux = sensor.read();

    // Print light
    Serial.print(F("Light: "));
    Serial.print(lux / 2);
    Serial.print(F("."));
    Serial.print(lux % 10);
    Serial.println(F(" LUX"));
  }
}

Output

BH1750 continues measurement high resolution example
Light: 15.0 LUX
Light: 31.2 LUX
Light: 385.0 LUX
Light: 575.1 LUX
Light: 667.5 LUX

Usage

Initialization

#include <Wire.h>
#include <ErriezBH1750.h>

// ADDR line LOW/open:  I2C address 0x23 (0x46 including R/W bit) [default]
// ADDR line HIGH:      I2C address 0x5C (0xB8 including R/W bit)
BH1750 sensor(LOW);

void setup()
{
  	// Initialize I2C bus
  	Wire.begin();
    
    // Initialize sensor with a mode and resolution:
    //   Modes:
    //     ModeContinuous
    //     ModeOneTime
    //   Resolutions:
    //     ResolutionLow (4 lx resolution)
    //     ResolutionMid (1 lx resolution)
    //     ResolutionHigh (0.5 lx resolution)
    sensor.begin(mode, resolution);
}

Start conversion

Wire.begin(); and sensor.begin(); must be called before starting the conversion:

sensor.startConversion();

Wait for completion asynchronous (non-blocking)

The sensor conversion completion status can be checked asynchronously before reading the light value:

bool completed = sensor.isConversionCompleted();

Wait for completion synchronous (blocking)

The sensor conversion completion status can be checked synchronously before reading the light value:

// Wait for completion
// completed = false: Timeout or device in power-down
bool completed = sensor.waitForCompletion();

Read light value in LUX

One-time mode: The application must wait or check for a completed conversion, otherwise the sensor may return an invalid value. Continues mode: The application can call this function without checking completion, but is not recommended when accurate values are required.

Read sensor light value:

// lux = 0: No light or not initialized
uint16_t lux = sensor.read();

For 4 lx low and 1 lx high resolutions:

// Print low and medium resolutions
Serial.print(F("Light: "));
Serial.print(lux);
Serial.println(F(" LUX"));

For 0.5 lx high resolution:

// Print high resolution
Serial.print(F("Light: "));
Serial.print(lux / 2);
Serial.print(F("."));
Serial.print(lux % 10);
Serial.println(F(" LUX"));

Power down

The device enters power down automatically after a one-time conversion.

A manual power-down in continues mode can be generated by calling:

sensor.powerDown();

Library dependencies

Library installation

Please refer to the Wiki page.

Other Arduino Libraries and Sketches from Erriez