To use the library, include Wire.h and BH1750.h. You must initialize the I2C bus with Wire.begin() before calling lightMeter.begin().
By default, the library uses Continuous High Resolution Mode. You can change the measurement mode by passing an argument to lightMeter.begin(mode).
Error Codes:
readLightLevel() returns a float. If an error occurs, it returns:
-1: No valid data was transmitted from the sensor.-2: Device is not configured.
#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();
lightMeter.begin();
Serial.println(F("BH1750 Test"));
}
void loop() {
float lux = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
delay(1000);
}