bh1750arduino
时间: 2023-12-20 08:05:16 浏览: 40
BH1750是一种数字式环境光强度传感器,可以通过I2C接口与Arduino等微控制器连接。下面是使用Arduino和BH1750环境光传感器制作DIY光照强度计的步骤:
1.准备材料:Arduino UNO、BH1750环境光传感器、LCD显示屏、无焊面包板、跳线等。
2.将BH1750传感器连接到Arduino UNO的I2C总线上。将传感器的VCC引脚连接到Arduino的5V引脚,将传感器的GND引脚连接到Arduino的GND引脚,将传感器的SDA引脚连接到Arduino的SDA引脚,将传感器的SCL引脚连接到Arduino的SCL引脚。
3.将LCD显示屏连接到Arduino UNO上。将LCD的VSS引脚连接到Arduino的GND引脚,将LCD的VDD引脚连接到Arduino的5V引脚,将LCD的VO引脚连接到一个10K电位器的中间引脚,将LCD的RS引脚连接到Arduino的数字引脚12,将LCD的RW引脚连接到Arduino的GND引脚,将LCD的E引脚连接到Arduino的数字引脚11,将LCD的D4-D7引脚分别连接到Arduino的数字引脚5-8。
4.编写Arduino代码。代码中需要使用Wire库和LiquidCrystal库。首先初始化BH1750传感器,然后读取传感器的光照强度值,并将其显示在LCD屏幕上。以下是示例代码:
```arduino
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 6, 7, 8); // 初始化LCD
#define BH1750_ADDRESS 0x23 // BH1750地址
#define BH1750_MODE 0x10 // BH1750模式
void setup() {
Wire.begin(); // 初始化I2C总线
lcd.begin(16, 2); // 初始化LCD
lcd.print("Light Intensity"); // 在LCD上显示标题
}
void loop() {
uint16_t lux; // 光照强度值
Wire.beginTransmission(BH1750_ADDRESS); // 开始传输
Wire.write(BH1750_MODE); // 发送模式指令
Wire.endTransmission(); // 结束传输
delay(200); // 等待传感器响应
Wire.requestFrom(BH1750_ADDRESS, 2); // 请求2个字节的数据
lux = Wire.read(); // 读取高字节
lux <<= 8; // 移位
lux |= Wire.read(); // 读取低字节
lux /= 1.2; // 转换为lx
lcd.setCursor(0, 1); // 设置光标位置
lcd.print(lux); // 在LCD上显示光照强度值
lcd.print(" lx"); // 在LCD上显示单位
delay(1000); // 延时1秒
}
```
5.将代码上传到Arduino UNO上,并将电路连接好。当电路通电后,LCD屏幕将显示“Light Intensity”标题和当前的光照强度值。
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)