Optional ESP32 Implementation (BH1750)
Optional ESP32 Implementation (BH1750)
animation
sensors
NoteOptional ESP32 Implementation
This example reads the sensor and reports its output. Use it after checking the wiring, then compare the reported values with the physical test described above.
#include <Wire.h>
#include <BH1750.h>
BH1750 lightMeter;
void setup() {
Serial.begin(115200);
Wire.begin(); // Uses default ESP32 I2C pins: SDA=GPIO21, SCL=GPIO22
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println("BH1750 initialized");
} else {
Serial.println("Error initializing BH1750");
}
}
void loop() {
float lux = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.print(lux);
Serial.print(" lux");
// Classify light levels (approximate ranges)
String classification;
if (lux < 1) {
classification = "Darkness (moonlight)";
} else if (lux < 50) {
classification = "Very dim (street lighting)";
} else if (lux < 200) {
classification = "Dim (hallway)";
} else if (lux < 500) {
classification = "Normal (home/office)";
} else if (lux < 2000) {
classification = "Bright (retail/overcast)";
} else if (lux < 25000) {
classification = "Very bright (daylight)";
} else {
classification = "Extremely bright (direct sun)";
}
Serial.print(" - ");
Serial.println(classification);
delay(1000);
}