Jump to content
43oh

Trouble with BH1750 (Tiva C-Lm4f123gxl)


Recommended Posts

#include <Wire.h> //BH1750 IIC Mode 
#include <math.h> 
int BH1750address = 0x23; //setting i2c address
uint16_t val=0;


byte buff[2];
void setup()
{
  Wire.begin();
  Serial.begin(9600);//init Serail band rate
}

void loop()
{
  
  int i;
  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]"); 
  }

}

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

SCL >> PA6

SDA >> PA7

 

but  no result ,pz help me?

Link to post
Share on other sites

@@asus0909

 

Here is a data sheet for the bh1750: http://www.dfrobot.com/image/data/SEN0097/BH1750FVI.pdf

 

The iic address = 0x23 when ADDR = LOW.

The iic address = 0x3C when ADDR = HIGH.

 

I do not see where you configure the light sensor for a specific sample resolution. It may be in reset mode until you configure it. Check out page 7 of the datasheet to see an example transaction to get a reading out of the device.

 

Also, do you have pull-up resistors on SCL and SCK?

Link to post
Share on other sites

@@zeke

 

 

The iic address = 0x23 when ADDR = LOW.

this mean ADD >> GND ?

 

 

I do not see where you configure the light sensor for a specific sample resolution.
void BH1750_Init(int address) 
{
  Wire.beginTransmission(address);
  Wire.write(0x10);//1lx reolution 120ms
  Wire.endTransmission();
}

Wire.write(0x10);//Continuous_H_resolution_Mode  0x10

 

Can you help me code sample?

 

p/s:my english is not good

Link to post
Share on other sites

Hi @@asus0909,

 

Don't worry about your english skills. We can speak code and we will understand each other  :biggrin:

 

 

this mean ADD >> GND ?

 

Yes, that is correct. Connect the ADDR pin to ground and then the I2C address will be 0x23.

 

 

 

Do you have a logic analyzer?

 

If so, connect it up to the SDA and SCL lines and capture some waveforms. Those waveforms will let us know what is and is not working properly.

 

 

Link to post
Share on other sites
  • 4 months later...
I tested BH1750FVI board for arduino and it works. But there is few things I have to do before It worked. On the board is 3.3V voltage stabilizer, so you must remove it or use 5V from USB and common ground with microprocessor! 
I make few changes in code. Energia 0014 and Launchpad 1.4 (cross RX TX) with MSP430G2553.
Here is my working code:
// SDA to PIN 1.7
//SCL to PIN 1.6
//The iic address = 0x23 when ADDR = LOW; address = 0x3C when ADDR = HIGH

#include <Wire.h> //BH1750 I2C Mode slave
uint16_t Lux=0;
void setup()
{ 
  Wire.begin();
  Serial.begin(9600);
}

void loop()
{
    Lux=0;
    Read_BH1750();
    Serial.print(Lux,DEC);     
    Serial.println(" LUX"); 

 delay(400);

}

void Read_BH1750() 
{
  byte buff[2];
  int i=0;
  Wire.beginTransmission(0x23);
  Wire.write(0x10);//1 Lux resolution time 120ms  
  Wire.requestFrom(0x23, 2 );
  while(Wire.available()){buff[i] = Wire.read(); i++;}    // receive one byte
  Wire.endTransmission(); 
  if(i==2){Lux=((buff[0]<<8)|buff[1])/(12/10);}
}
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...