// Simple Version with BH1750.h
//////////////////////////////////////////////////////////////////
/////////////////////////////////////
/////////////////////
///////////
//Quelle: https://randomnerdtutorials.com/arduino-bh1750-ambient-light-sensor/
#include <BH1750.h>
#include <Wire.h>
BH1750 lightMeter;
void setup() {
Serial.begin(9600);
// Initialize the I2C bus (BH1750 library doesn't do this automatically)
Wire.begin();
// On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
// For Wemos / Lolin D1 Mini Pro and the Ambient Light shield use
// Wire.begin(D2, D1);
lightMeter.begin();
Serial.println(F("BH1750 Test begin"));
}
void loop() {
float lux = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
delay(1000);
}
// Function (readLightLevel)
//////////////////////////////////////////////////////////////////
/////////////////////////////////////
/////////////////////
///////////
// Quelle: https://codebender.cc/library/BH1750#BH1750.cpp
uint16_t BH1750::readLightLevel(void) {
uint16_t level;
Wire.beginTransmission(BH1750_I2CADDR);
Wire.requestFrom(BH1750_I2CADDR, 2);
#if (ARDUINO >= 100)
level = Wire.read();
level <<= 8;
level |= Wire.read();
#else
level = Wire.receive();
level <<= 8;
level |= Wire.receive();
#endif
Wire.endTransmission();
#if BH1750_DEBUG == 1
Serial.print("Raw light level: ");
Serial.println(level);
#endif
level = level/1.2; // convert to lux
#if BH1750_DEBUG == 1
Serial.print("Light level: ");
Serial.println(level);
#endif
return level;
}
///////////// THIS ///////////// THIS ///////////// THIS /////////////
#include <Wire.h>
void setup() {
Serial.begin(9600); // Start serial for output
Wire.begin(); // Join I2C bus (address is optional for controller device)
}
void loop() {
float lux = Get_B();
print("Light: ");
print(lux);
println(" lx");
delay(500);
}
float Get_B (void) {
float temp, msb, lsb;
Wire.beginTransmission(0b0100011); // Transmit to device address (0b0100011) **
Wire.requestFrom(0b0100011, 6); // From peripheral device(address) request 6 bytes **
// Peripheral may send less than requested
while (Wire.available()) {
byte val = Wire.read(); // Receive a byte as character
}
Serial.println(val); // Print byte
Wire.endTransmission(); // Stop transmitting
temp = (msb * 256 + lsb ) / 1.2;
return temp;
}