BH1750传感器,用于检测环境光光照强度。
BH1750FVI是日本罗姆(ROHM)半导体生产的数字式环境光传感IC。其主要特性有:
|
- GND 电源地
- ADD I2C地址引脚
- SDA I2C总线数据引脚
- SCL I2C总线时钟引脚
- VCC 电源 电压3-5V
#include <Wire.h>
#include <math.h>
#include <MsTimer2.h>
int BH1750address = 0x23;//BH1750 I2C地址 //ADDR="L" for this module
byte buff[2];
int flag = 0;//定时中断标志
void timer()//定时中断函数
{
flag = 1;
}
void setup()
{
Wire.begin();
Serial.begin(9600);
MsTimer2::set(2000, timer); //定时器设置,每2秒触发一次timer函数操作
MsTimer2::start();
}
void loop()
{
if( flag )//
{
Serial.print( BH1750() );
Serial.println("[lux]");
flag = 0;//归零,等着定时中断重新赋值
}
}
double BH1750() //BH1750设备操作
{
int i=0;
double val=0;
//开始I2C读写操作
Wire.beginTransmission(BH1750address);
Wire.send(0x10);//1lx reolution 120ms//发送命令
Wire.endTransmission();
delay(200);
//读取数据
Wire.beginTransmission(BH1750address);
Wire.requestFrom(BH1750address, 2);
while(Wire.available()) //
{
buff[i] = Wire.receive(); // receive one byte
i++;
}
Wire.endTransmission();
if(2==i)
{
val=((buff[0]<<8)|buff[1])/1.2;
}
return val;
}
光亮度数据参考
晚上: 0.001-0.02;
月夜: 0.02-0.3;
多云室内: 5-50;
多云室外: 50-500;
晴天室内: 100-1000;
夏天中午光照下: 大约10*6能量;
阅读书籍时的照明度:50-60;
家庭录像标准照明度:1400
/*
https://www.geek-workshop.com/thread-1610-1-1.html
https://www.arduino.cn/thread-92182-2-13.html
*/
/*
【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[2];
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[0]<<8)|buff[1])/1.2;
Serial.print(val,DEC);
Serial.println("[lx]");
}
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();
}
测量程序步骤:
4、指令集合结构:
5、测量模式说明: