Hi Sara … I want to assign different i2c pins for use with a BH1750 light sensor. You have a very nice tutorial that does this very thing for the BME280 sensor, and when I implement your code, it works fine … but when I try to do it for the BH1750 sensor, it doesn’t. An issue is that the BME code uses the Adafruit_BME280 library, but there isn’t an Adafruit_BH1750, so the code doesn’t translate. I tried the following code, but it won’t work:
BH1750 lightMeter;
#define I2C_SDA 1
#define I2C_SCL 2
bool status;
TwoWire I2CBH1750 = TwoWire(0);
//*****************************************************************************
//********************** setup ************************************************
//*****************************************************************************
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println(F(“BH1750 test”));
I2CBH1750.begin(I2C_SDA, I2C_SCL, 100000);
lightMeter.begin();
Serial.println(F(“BH1750 Test begin”));
/*
status = lightMeter.begin(0x23, &I2CBH1750);
if (!status) {
Serial.println(“Could not find a valid BH1750 sensor, check wiring!”);
while (1);
}
*/
I’ve commented out the lightmeter.begin code since it won’t accept an address … the BH1750 object apparently doesn’t have the same thing as the Adafruit_BME280 object does.
Do you have any solution to this? Thanks!
Hi
Which library are you using to read from the light meter? Can you share a link to the library you’re using?
Regards,
Sara
P.S. I’m currently out of the office, so I might take longer to respond.
HI Sara,
It turned out that the .begin statement had to include the mode for this device (For the BME280, it only needs the address and the wire object pointer). So, the following code works fine:
#include “BH1750.h”
#include “Wire.h”
BH1750 bh1750;
void setup() {
Serial.begin(115200);
Wire.begin(1, 2);
bh1750.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire);
}
void loop() {
float lightLevel;
if (bh1750.measurementReady()) {
lightLevel = bh1750.readLightLevel();
Serial.print(“lux = “); Serial.print(lightLevel);
delay(1000); }
}
Thanks for your help (I didn’t get a message that you had responded, which I usually get).
Great!
I’m glad the issue is solved.
I’ll mark this as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara
Hi Again (I know this has been resolved, but ..). It is not clear to me why the statement bh1750.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire); had to be used. I tried another program which also works fine, and basically uses Wire.begin(SDA_pin, SCK_pin) (where those pins are integers defined elsewhere).
Here is a simpler code that works fine:
#include <Wire.h>
#include <BH1750.h>
BH1750 lightMeter;
void setup(){
Serial.begin(115200);
delay(1000);
Wire.begin(1,2); // Wire.begin(SDA_pin, SCL_pin)
//Wire.begin(); // use this if not assigning pins, but using default ones
lightMeter.begin(); // uses the default setting for this chip
Serial.println(“BH1750 initialized”);
}
void loop() {
float lux = lightMeter.readLightLevel();
Serial.print(“Light: “); Serial.print(lux); Serial.println(” lx”);
delay(1000);
}
That statement, will probably do the same thing as wire.begin(), but you are passing more parameters, probably related to the way the measurements are taken.
Usually, on those libraries, if you don’t pass those parameters, they initialize the sensor with the default settings.
Regards,
Sara