arduino读取BEC光照传感器数据

近期一个项目要用到光照传感器测量光照度,第一根项目是测量大棚里面的光照度,用的BH1750量程65535Lx,第二个项目是采集室外的光照度,实际测试发现晴天的时候在早上8点左右传感器就已经满量程,之后传感器的度数一直是0。室外用BH1750不行,在网上买了个BCE的光照传感器,光感芯片是MAX44009。回来发现网上并没有在arduino上使用BCE光照传感器的程序,现在贡献出来,下面是资料和代码。

#include <Wire.h> 
#include <math.h>
int BECaddress = 0x4A;
void setup()
{
   Serial.begin(9600);
   Serial.println("Starting up");
   Wire.begin();
   delay(50);
}
void loop()
{
    float lx;
    lx = BEC_Read();
    Serial.println(lx);
    delay(200);
}
float BEC_Read() 
{
  int buff_low,buff_high;
  char data_bin_high[10];
  int data_bin_low[10];
  float lx=0;
  long int t=0,k=0,j=0;
  int i=0;
  //读取传感器数值
  BEC_Init(0x03,BECaddress);
  delay(400);
  Wire.beginTransmission(BECaddress);
  Wire.requestFrom(BECaddress,1);
  while(Wire.available()) 
  {
     buff_high = Wire.read();  //高字节
  }
  Wire.endTransmission();  
  delay(100);
  BEC_Init(0x04,BECaddress);
  delay(400);
  Wire.beginTransmission(BECaddress);
  Wire.requestFrom(BECaddress,1);
  while(Wire.available()) 
  {
    buff_low = Wire.read();  // 低字节
  }
  Wire.endTransmission();
    //流明算法
    #ifdef BCE_TEST
    Serial.print("Buff high =");
    Serial.println(buff_high);
    Serial.print("Buff low =");
    Serial.println(buff_low);
    #endif
    if(buff_high==0)
    {
       data_bin_high[7]=0;
       data_bin_high[6]=0;
       data_bin_high[5]=0;
       data_bin_high[4]=0;
       data_bin_high[3]=0;
       data_bin_high[2]=0;
       data_bin_high[1]=0;
       data_bin_high[0]=0;
       i=8;
    }
   while (i<8)
    {
       data_bin_high[i]=(int)(buff_high%2);
       i=i+1;
       buff_high=buff_high/2;
    }
    #ifdef BCE_TEST
    for(i--;i>=0;i--)
        Serial.print((int)data_bin_high[i]);
    Serial.println("  ");
    #endif
    i=0;
        if(buff_low==0)
    {
       data_bin_low[3]=0;
       data_bin_low[2]=0;
       data_bin_low[1]=0;
       data_bin_low[0]=0;
       i=4;
    }
   while (i<8)
    {
       data_bin_low[i]=(int)(buff_low%2);
       i=i+1;
       buff_low=buff_low/2;
    }
    #ifdef BCE_TEST
    for(i--;i>=0;i--)
        Serial.print(data_bin_low[i]);
    Serial.println("  ");
    #endif
    t=(int)data_bin_high[3]*128+(int)data_bin_high[2]*64+(int)data_bin_high[1]*32+(int)data_bin_high[0]*16+(int)data_bin_low[3]*8+(int)data_bin_low[2]*4+(int)data_bin_low[1]*2+(int)data_bin_low[0];
    j=(int)data_bin_high[7]*8+(int)data_bin_high[6]*4+(int)data_bin_high[5]*2+(int)data_bin_high[4];
    k=pow(2,j);
    lx = k*t*0.045;
    #ifdef BCE_TEST
    Serial.print("The Lx is ");
    Serial.println(lx);
    #endif
    return lx;
}
void BEC_Init(int buf, int address) 
{
  Wire.beginTransmission(address);
  Wire.write(buf);//高字节流明寄存器 0x03,低字节流明寄存器0x04
  Wire.endTransmission();
  }


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Louistinda

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值