eagler8 发表于 2019-9-27 19:49:57

eagler8 发表于 2019-9-27 19:51:20

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百二十九:GY-302 数字量光强度检测 光照传感器 BH1750FVI 光线检测模块
项目:依据光亮度控制LED
Module      UNO
5 VCC   ——   5V
6 GND——   GND
7 SCL    ——   A5
8 SDA   ——   A4
9 ADD——   NC
*/

#include <Wire.h> //IIC库

#include <math.h>

int BH1750address = 0x23;//芯片地址为16位23

byte buff;

void setup()

{
pinMode(13,OUTPUT);
Wire.begin();

Serial.begin(9600);

}




void loop()

{

int i;

uint16_t val=0;

BH1750_Init(BH1750address);

delay(100);

if(2==BH1750_Read(BH1750address))

{

   val=((buff<<8)|buff)/1.2;

   Serial.print(val,DEC);   

   Serial.println("");

}

delay(150);
   if (val<100)
{
    digitalWrite(13,HIGH);
}
else
{
    digitalWrite(13,LOW);
}
}




int BH1750_Read(int address) //

{

int i=0;

Wire.beginTransmission(address);

Wire.requestFrom(address, 2);

while(Wire.available()) //

{

    buff = Wire.read();// read one byte

    i++;

}

Wire.endTransmission();

return i;

}




void BH1750_Init(int address)

{

Wire.beginTransmission(address);

Wire.write(0x10);//1lx reolution 120ms

Wire.endTransmission();

}

eagler8 发表于 2019-9-27 20:07:12

页: 1 [2]
查看完整版本: 【Arduino】108种传感器模块系列实验(129)---BH1750光照传感器