Connect arduino to BH1750

Hello everyone, I use 2 BH1750 sensors connected to Arduino with the following connection: Sensor 1 (addr = vcc) and sensor 2 (addr = gnd) are both connected to A5 and A4 of Arduino, but when I get data from the sensor, the value of sensor 1 depends on sensor 2. That is, when I cover sensor 1, the value of sensor 1 does not change, but when I cover sensor 2, the value of both sensors changes. I don't know how to install it or if my program is wrong.
`#include <Wire.h>
#include <BH1750.h>

BH1750 lightSensor1(0x23); // ADDR nối GND
BH1750 lightSensor2(0x5C); // ADDR nối VCC

void setup() {
Serial.begin(9600);
Wire.begin();

lightSensor1.begin(BH1750::CONTINUOUS_HIGH_RES_MODE_2);
lightSensor2.begin(BH1750::CONTINUOUS_HIGH_RES_MODE_2);

Serial.println(F("\n---------------- LOADING ----------------"));
Serial.println(F("| Sensor 1 (0x23) | Sensor 2 (0x5C) |"));
}

void loop() {
float lux1 = lightSensor1.readLightLevel();
float lux2 = lightSensor2.readLightLevel();

Serial.print("| ");
Serial.print(lux1, 2);
Serial.print(" lx");

if (lux1 < 10) Serial.print(" ");
else if (lux1 < 100) Serial.print(" ");
else Serial.print(" ");

Serial.print("| ");
Serial.print(lux2, 2);
Serial.println(" lx |");

delay(500);
}
`

Wrap your code in a code block.

You must also post a wiring diagram

Try running the i2c scanner sketch to check that two sensors with the two addresses are visible on the bus.

Try swapping the address pins of the sensors (sensor 1 addr = ground etc).

Try other BH1750 libraries (like Adafruit).

I can't see an error in your code which would cause what you described.

I have never used 2x BH1750 in the same circuit. I have used them with other i2c sensors (e.g. SHT31) with no problems.

Your language in the comments indicates you are connecting the sensors to VCC and GND rather than SCL and SDA.

The "two sensors" example indicates only one address is used, but you must configure Wire.h twice, one for each sensor...

#include <Wire.h>
#include <BH1750.h>

BH1750 lightSensor1(0x23); // ADDR nối GND
BH1750 lightSensor2(0x5C); // ADDR nối VCC

void setup() {
  Serial.begin(9600);
  Wire.begin();

  lightSensor1.begin(BH1750::CONTINUOUS_HIGH_RES_MODE_2);
  lightSensor2.begin(BH1750::CONTINUOUS_HIGH_RES_MODE_2);

  Serial.println(F("\n---------------- LOADING ----------------"));
  Serial.println(F("| Sensor 1 (0x23) | Sensor 2 (0x5C) |"));
}

void loop() {
  float lux1 = lightSensor1.readLightLevel();
  float lux2 = lightSensor2.readLightLevel();

  Serial.print("|   ");
  Serial.print(" lx");

  if (lux1 < 10) Serial.print("     ");
  else if (lux1 < 100) Serial.print("    ");
  else Serial.print("   ");

  Serial.print("|   ");
  Serial.print(lux2, 2);
  Serial.println(" lx   |");

  delay(500);
}

Here is my connection diagram and the program, but it doesn't work as I want it to

I used the i2c scan program, the result is received two addresses 0x5c(addr=vcc) and 0x23(addr=gnd). But when receiving the light intensity value, the above error occurs. This is my scan program, I don't know if it works correctly. `

#include <Wire.h>

void setup() {
  Wire.begin();        
  Serial.begin(9600);  
  Serial.println("Scan_device_I2C...");
}

void loop() {
  Serial.println("\nLoading...");

  byte count = 0;

  for (byte address = 1; address < 127; address++) {
    Wire.beginTransmission(address);
    if (Wire.endTransmission() == 0) {
      Serial.print("Find_device 0x");
      if (address < 16) Serial.print("0");
      Serial.println(address, HEX);
      count++;
    }
    delay(5);
  }

  if (count == 0) {
    Serial.println("❌Device not found.");
  } else {
    Serial.print("âś…Total devices found: ");
    Serial.println(count);
  }

  delay(2000);
}

Ok, good. You see 2 addresses, as expected.

Now what happens when you try my other suggestions?

(Please never say "it does not work". Give a detailed description of what actually happens, compared to what you wanted to happen. Even if it is still not working correctly, has anything changed?)

I tried changing the pins of the sensor, but the result is still the same. The sensor whose pin addr = gnd affects the reading value of the 2 sensors. When I cover the sensor(addr=gnd) the value of both sensors changes, if I cover the sensor(addr = vcc) the value of the sensor does not change

Ok, thanks for the more detailed explanation.

But it is not the same as before, if you followed my suggestion correctly and I understood your description correctly. Before, sensor 1 was ignored. Now, sensor 2 is ignored. Am I right?

This is important because it seems to indicate that neither of the sensors is faulty. This is also good.

You said that whichever sensor has it's ADDR pin connected to ground is ignored. I agree this is what seems to be happening. I have a theory this may be caused by a bug in the BH1750 library you are using. That's why I also suggested you try other libraries for BH1750.

Hi
What voltage are you using to feed the BH1750?
Datasheet says:

Are you using a module for this sensor? Also, as suggested above, try another library.
And what about the DVI pin in your schematic (and the pull-up resistors)? Do you leave it floating? Please refer to page 8

I am using claws library on github. and the library you mentioned i don't know where it is. can you provide me the link or file


this is the bh1750 i use, my sensor does not have dvi

I think you can use the library manager in the IDE. Type BH1750 into the search. You will probably find the Adafruit library there.

I used another library from Starmbi(hp_BH1750) and I also used their sample program. And it worked. Thank you

1 Like

I have raised an issue on the claws repo. I don't hold out much hope that it will ever be fixed, because the library has no commits for years.