Go Down

Topic: I2C bus on multiple BH1750 sensor (Read 2 times) previous topic - next topic

YasserH

Hi all,
I'm currently working on BH1750, http://www.dfrobot.com/wiki/index.php/Light_Sensor_(SKU:SEN0097)
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):
Code: [Select]
#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

afremont

With the ADDR pin grounded, the address is as you have it 0x23, the other address is 0x5C (a simple bit inversion).
Experience, it's what you get when you were expecting something else.

YasserH

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:
Code: [Select]
#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?

Grumpy_Mike

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

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

YasserH

#4
Feb 11, 2013, 12:50 am Last Edit: Feb 11, 2013, 12:53 am by yaser Reason: 1
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



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();
}


ken_chuang

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"

YasserH

Hi Ken,
Thanks, I will check that..

Grumpy_Mike

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.

michinyon

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

Mohannad_Rawashdeh

this library support multiple device address

Library download on Github :

https://github.com/Genotronex/BH1750FVI_Master



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

no thing is impossible except that the state of your mind keeps it so...

Go Up