I2C bus on multiple BH1750 sensor

Hi all,
I'm currently working on BH1750, Light_Sensor__SKU_SEN0097_-DFRobot
I don't understand, how to using it in multiple I2C address/sensor.
Please give me some input on code reference to changes the address?

Here the current code I used for 1 sensor (from DFrobot):

#include <Wire.h> //BH1750 IIC Mode 
#include <math.h> 
int BH1750address = 0x23; //setting i2c address
 
byte buff[2];
void setup()
{
  Wire.begin();
  Serial.begin(57600);//init Serail band rate
}
 
void loop()
{
  int i;
  uint16_t val=0;
  BH1750_Init(BH1750address);
  delay(200);
 
  if(2==BH1750_Read(BH1750address))
  {
    val=((buff[0]<<8)|buff[1])/1.2;
    Serial.print(val,DEC);     
    Serial.println("[lx]"); 
  }
  delay(150);
}
 
int BH1750_Read(int address) //
{
  int i=0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while(Wire.available()) //
  {
    buff[i] = Wire.receive();  // receive one byte
    i++;
  }
  Wire.endTransmission();  
  return i;
}
 
void BH1750_Init(int address) 
{
  Wire.beginTransmission(address);
  Wire.send(0x10);//1lx reolution 120ms
  Wire.endTransmission();
}

Thanks before

With the ADDR pin grounded, the address is as you have it 0x23, the other address is 0x5C (a simple bit inversion).

Hi afremont,
Thanks for respond, still can't works. I think the problem is in my code.
Sorry for my poor writing skills, but I will try to make it work.

This my present code:

#include <Wire.h> //BH1750 IIC Mode 
#include <math.h> 
int Sen1 = 0x23; //setting i2c address
int Sen2 = 0x5C;

byte buff[2];

void setup()
{
  Wire.begin();
  Serial.begin(57600);//init Serail band rate
}

void loop()
{
  int i;
  uint16_t val1=0, val2=0;
  
  BH1750_Init(Sen1);
  delay(200);

  if(2==BH1750_Read(Sen1))
  {
    val1=((buff[0]<<8)|buff[1])/1.2;
    Serial.print(val1,DEC);     
    Serial.println("[lx]"); 
  }
  delay(200);
  
  if(2==BH1750_Read(Sen2))
  {
    val2=((buff[0]<<8)|buff[1])/1.2;
    Serial.print(val2,DEC);     
    Serial.println("[lx]"); 
  }
  delay(200);
}

int BH1750_Read(int address) //
{
  int i=0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while(Wire.available()) //
  {
    buff[i] = Wire.read();  // receive one byte
    i++;
  }
  Wire.endTransmission();  
  return i;
}

void BH1750_Init(int address) 
{
  Wire.beginTransmission(address);
  Wire.write(0x10);//1lx reolution 120ms
  Wire.endTransmission();
}

Btw, I'm curious how to make that inversion?
ADDR = ‘H’ ( ADDR ? 0.7VCC ) ? “1011100“ --> 5C
ADDR = 'L' ( ADDR ? 0.3VCC ) ? “0100011“ --> 23

Isn't?

Btw, I'm curious how to make that inversion?

By connecting pin 2 to either ground or leaving it unconnected.

Thanks Grumpy_Mike,
I run the code above and nothing happen in serial monitor. I used this connection:
Vcc - 5v
Gnd - Gnd
SCL - 10K - Pin 5
SDA - 10K - Pin 4
ADD - 100 ohm - Gnd

yaser:
This my present code:
#include <Wire.h> //BH1750 IIC Mode
#include <math.h>
int Sen1 = 0x23; //setting i2c address
int Sen2 = 0x5C;

byte buff[2];

void setup()
{
Wire.begin();
Serial.begin(57600);//init Serial baud rate
}

void loop()
{
uint16_t val1=0, val2=0;

BH1750_Init(Sen1);
delay(200);

if(2==BH1750_Read(Sen1))
{
val1=((buff[0]<<8 )|buff[1])/1.2;
Serial.print(val1,DEC);
Serial.println("[lx]");
}
delay(200);

BH1750_Init(Sen2);
delay(200);

if(2==BH1750_Read(Sen2))
{
val2=((buff[0]<<8 )|buff[1])/1.2;
Serial.print(val2,DEC);
Serial.println("[lx]");
}
delay(200);
}

int BH1750_Read(int address) //
{
int i=0;
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
while(Wire.available()) //
{
buff = Wire.read(); // receive one byte

  • i++;*
  • }*
  • Wire.endTransmission(); *
  • return i;*
    }
    void BH1750_Init(int address)
    {
  • Wire.beginTransmission(address);*
  • Wire.write(0x10);//1 lux resolution 120ms*
  • Wire.endTransmission();*
    }
    [/quote]

I am using the following connection for Arduino Mega 2560

VCC ------ 5V
GND ------ GND
SCL ------ SCL
SDA ------ SDA
ADD ------ GND

Most of the sample codes from chinese websites worked for me with Arduino Mega 2560 with code change to "wire.read" and "wire.write"

Hi Ken,
Thanks, I will check that..

As that address pin can be left unconnected there must be an internal pull up, so don' connect the pin through a 100R but direct.

Are you getting any response at all ? Get the "I2C scanner" program, which will identify if the
device is responding to any I2C communication.

this library support multiple device address

Library download on Github :

GitHub - Genotronex/BH1750FVI_Master: Digital Light Sensor BH1750

http://www.instructables.com/id/BH1750-Digital-Light-Sensor/

This is an old thread and probably not visited anymore, but in case there is someone wanting to use 2 light sensors at the same time (which I suppose was the issue of this thread) I would like to summarize:

The ADDR pin on the BH1750 (GY-302) is determining if the chip will have the address 0x23 (pin LOW) or 0x5C (pin HIGH) and is actually pulled DOWN !!! according to the schematics.
It is always a good idea to verify the addresses with I2C scanner.

Mohannad in his article #9 refers to http://www.instructables.com/id/BH1750-Digital-Light-Sensor/ but this instructable actually never shows 2 sensors connected to Arduino. He instructs to connect the ADDR to pin A3, which to me does not make sense especially if you have 2 sensors.

I am using Christopher Laws library BH1750.h at GitHub - claws/BH1750: An Arduino library for the digital light sensor breakout boards containing the BH1750FVI IC

One of my sensors has hardwired ADDR to GND and the other sensor has hardwired ADDR to Vcc. (Probably the most correct way should be to connect ADDR to Vcc with 2k resistor)

The modified code for 2 sensors is:

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

// create two instances (one for each sensor)
BH1750 lightMeter1(0x23); 
BH1750 lightMeter2(0x5C); 

void setup(){
 Serial.begin(9600);
 Wire.begin();
 lightMeter1.begin();
 lightMeter2.begin();
 Serial.println(F("BH1750 Test 2 sensors"));
}

void loop() {
 float lux1 = lightMeter1.readLightLevel(true);
 float lux2 = lightMeter2.readLightLevel(true);
 Serial.print("Light: ");
 Serial.print(lux1);
 Serial.print(" ");
 Serial.print(lux2);
 Serial.println(" lx");
 delay(2000);
}

With this method, you do not have to initiate the sensor (and lose time) each time you read it.

By the way - Yaser in article #2: you never initiate the Sen2, which might be your actual problem.

a mi si me sirvio pero tengo un porblema, solo me lee el sensor con la direccion 0x23, es decir que me da dos datos pero son exactamente iguales y solo varian cuaando el sensor con esa direccion varia, el otro sensor no lo lee