108个传感器之-光照传感器(53)
介绍
这是来自罗姆(Rohm)的BH1750 16位环境光传感器。由于光线对人类和大多数其他生物都至关重要,因此检测环境中的光照强度是学习使用微控制器和传感器时一个常见的入门项目。我们是否应该调高显示器的亮度,还是降低亮度以节省电能?机器人应该朝哪个方向移动才能停留在光线最充足的地方?现在是白天还是夜晚?所有这些问题都可以借助BH1750来解决。这是一款小巧、功能强大且价格低廉的光传感器,您可以将其集成到下一个项目中,以实现光线的检测与测量。
BH1750 提供16位lux光测量,用于测量光的SI单元使其易于与其他值 (如参考和来自其他传感器的测量) 进行比较。能够测量从 0 到 65k+ lux,bh1750。通过一些校准和先进的测量时间调整,它甚至可以说服测量高达100,000 lux。
)
传感器往往采用小型封装,BH1750也不例外。不比米粒大多少,这个方便的光感应朋友需要一些帮助,以便人们进行实验,而没有使用表面贴装零件的愿望或工具。我们是来帮忙的封装在我们的Stemma QT外形的PCB上,BH1750 集成了电压调节器和电平转换电路,使其可以与3.3v设备一起使用,如Raspberry Pi,或 5v 设备。
引脚介绍
SCL - I2C 时钟引脚,连接到您微控制器的 I2C 时钟线。该引脚具有电平转换功能,因此可以使用 3-5V 逻辑电平,并且该引脚上带有 10K 上拉电阻。
SDA - I2C 数据引脚,连接到您微控制器的 I2C 数据线。该引脚具有电平转换功能,因此可以使用 3-5V 逻辑电平,并且该引脚上带有 10K 上拉电阻。
ADDR/AD0 跳线 - I2C 地址选择引脚。将该引脚拉高或在背面焊接跳线短路,可以将 I2C 地址从 0x23 更改为 0x5C。
示例代码
// for help look at: https://github.com/Starmbi/hp_BH1750/wiki
// to speed up the measurements you can change the BH1750_MTREG_LOW = 31
// in the hp_BH1750.h file to a lower value
#include <Arduino.h>
#include <hp_BH1750.h> // include the library
//stores the first MAX_VAL samples in a array.
const unsigned int MAX_VAL = 150; // for Arduino (low memory)
// const unsigned int MAX_VAL = 1500; //for ESP8266
unsigned int val[MAX_VAL]; // adjust the value to your free memory on the board.
hp_BH1750 sens;
void setup()
{
// put your setup code here, to run once:
Wire.setClock(400000); // uncomment this line if you have problems with communication
Serial.begin(9600);
//Serial.begin(115200); // try this line for faster printing, uncomment the line above
sens.begin(BH1750_TO_GROUND); // change to (BH1750_TO_VCC) if address pin connected to VCC
sens.calibrateTiming(); // you need a little brightness for this
}
void loop()
{
// put your main code here, to run repeatedly:
Serial.println("***********");
unsigned int t = millis() + 1000;
unsigned int c = 0;
while (millis() <= t)
{
sens.start(BH1750_QUALITY_LOW, 1); //will be adjusted to lowest allowed MTreg (default = 31)
if (c < MAX_VAL)
{
val[c] = sens.getRaw();
}
else
{
sens.getRaw();
}
yield(); // feed the watchdog of ESP6682
c++;
}
unsigned int readVal = c;
if (readVal > MAX_VAL)
c = MAX_VAL;
// Serial.print(c);
// Serial.print(char(9));
// Serial.println(sens.getRaw());
for (unsigned int i = 0; i < c; i++)
{
//Serial.print(i);
Serial.print(val[i]);
Serial.print(char(9));
if ((i + 1) % 10 == 0)
Serial.println("");
yield(); // feed the watchdog of ESP6682
}
Serial.println("");
Serial.print(readVal);
Serial.println(" Samples per second");
Serial.println("");
delay(1000);
}
参考项目
108个传感器之-光照传感器(53)


