อยากสอบถามเรื่องการต่อตัว bh1750 2 ตัว ในบอร์ด arduino ค่ะ
BH1750 มีอินเตอร์เฟสติดต่อแบบ I2C
อันดับแรก ลองต่อให้ออก 1 อันผมว่าน่าจะได้แล้ว
http://www.instructables.com/id/BH1750-Digital-Light-Sensor/
กรณีที่ต่อ 2 ตัว สามารถต่อทับกันได้เลย แล้วอ้างอิงผ่าน address i2c ให้ถูกตัว
ถ้าต่อปกติเป็น address 0x5C ถ้าต่อขา ADDR กับกราวร์จะได้ Address 0x23
With the ADDR pin grounded, the address is as you have it 0x23, the other address is 0x5C (a simple bit inversion).
ลองตามตัวอย่างในลิงค์นี้ครับ
http://forum.arduino.cc/index.php?topic=148007.0
ลองทำตาม ได้แล้วค่ะ แต่ไม่เข้าใจ 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();
}
ต้องลองศึกษาดู ไล่ตามโคด เป็นการใช้งานไลบารี Wire สำหรับรับส่งข้อมูลสั่งงาน BH1750 ครับ
คือcodeนี้ถ้าใช้กับบอร์ด STM32 nจะต้องเปลี่ยนตรงไหนไหมค่ะ
ใช้กับ pic16f887 ได้ไหมครับ