Pages: [1]   Go Down
Author Topic: I2C bus on multiple BH1750 sensor  (Read 2083 times)
0 Members and 1 Guest are viewing this topic.
Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
Logged

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Experience, it's what you get when you were expecting something else.

Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33379
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Btw, I'm curious how to make that inversion?
By connecting pin 2 to either ground or leaving it unconnected.
Logged

Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: February 10, 2013, 06:53:49 pm by yaser » Logged

Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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"
Logged

Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Ken,
Thanks, I will check that..
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33379
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Faraday Member
**
Karma: 61
Posts: 2874
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Jordan
Offline Offline
Newbie
*
Karma: 0
Posts: 6
no thing is impossible except that the state of your mind keeps it so...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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/

Logged

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

Pages: [1]   Go Up
Jump to: