Arduino从DHT11读取温湿度数据并显示在1602LCD
2023-03-07 09:41:13 时间
硬件清单
Arduino NANO
1602LCD + PCF8574T模块
YL-47 DHT11模块
连线
1. 连接LCD: PCF8574T模块4pin(Gnd, Vcc, SDA i2c数据, SCL i2c时钟) 连接至Arduino接口 Gnd -> Gnd, Vcc -> Vcc, SDA -> A4, SDL -> A5
2. 连接YL-47 DHT11: Gnd -> Gnd, Vcc -> Vcc, Data-> D4
Library
除了1602需要的库以外, 需要安装两个自带的库: DHT Sensor Library by Adafruit, Adafruit Unified Sensor
测试代码
#include <Wire.h> #include <LiquidCrystal_I2C.h> #include <DHT.h> #define DHTPIN 4 #define DHTTYPE DHT11 // I2C地址, 一般为0x3F, 0x20或0x27 LiquidCrystal_I2C lcd(0x27,16,2); // 初始化DHT DHT dht(DHTPIN, DHTTYPE); void setup() { lcd.init(); lcd.backlight(); // 打开背光 Serial.begin(9600); dht.begin(); lcd.setCursor(0,0); // line 0, pos 0 lcd.print("Good Day!"); lcd.setCursor(0,1); // line 1, pos 0 lcd.print("H: % T:"); delay(1000); } void loop() { // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; } // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t"); Serial.print("Heat index: "); Serial.print(hic); Serial.print(" *C "); Serial.print(hif); Serial.println(" *F"); lcd.setCursor(2,1); // line 1, pos 0 lcd.print(h); lcd.setCursor(11,1); // line 1, pos 0 lcd.print(t); delay(1000); }
代码说明
1. DHT11启动到读取数据需要等待1~2秒
2. 温湿度的精度都为1, 没有小数部分
3. DHT库里面带了计算热指数的方法 computeHeatIndex(), 用于生成综合温湿度计算得到的热指数值
改进拼接字符串
改进后的代码, 注意: arduino里的sprintf只能格式化整数, 不能格式化浮点
#include <Wire.h> #include <LiquidCrystal_I2C.h> #include <DHT.h> #include <DS3231.h> #define DHTPIN 4 #define DHTTYPE DHT11 // I2C地址, 一般为0x3F, 0x20或0x27 LiquidCrystal_I2C lcd(0x27,16,2); DHT dht(DHTPIN, DHTTYPE); DS3231 Clock; bool century=false; bool h12; bool PM; void setup() { lcd.init(); //lcd.backlight(); // 打开背光 Serial.begin(9600); dht.begin(); lcd.setCursor(0,0); // line 0, pos 0 lcd.print("Good Day Jessie~~"); lcd.setCursor(0,1); // line 1, pos 0 lcd.print("H: % T: T:"); delay(1000); } void loop() { char str[17]; sprintf( str, "%02d-%02d %02d:%02d:%02d ", Clock.getMonth(century), Clock.getDate(), Clock.getHour(h12, PM), Clock.getMinute(), Clock.getSecond()); lcd.setCursor(0,0); // line 0, pos 0 lcd.print(str); // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; } // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t"); Serial.print("Heat index: "); Serial.print(hic); Serial.print(" *C "); Serial.print(hif); Serial.println(" *F"); lcd.setCursor(2,1); // line 1, pos 0 lcd.print((int)h); lcd.setCursor(8,1); // line 1, pos 0 lcd.print((int)t); lcd.setCursor(13,1); lcd.print((int)(Clock.getTemperature()*10)); delay(1000); }
相关文章
- 数据孤岛是业务效率的无声杀手
- 2023展望:新的一年将给大数据分析领域带来什么?
- 阿里云ADB基于Hudi构建Lakehouse的实践
- 大数据在医疗保健领域的使用案例
- 微软增加说明:KB5021751 更新扫描已经 / 即将过时 Office 过程中不会触碰用户隐私
- 2022 Gartner全球云数据库管理系统魔力象限发布 腾讯云数据库入选
- 场景化、重实操,分享一个实时数仓实践案例
- Arctic的湖仓一体践行之路
- 分布式计算MapReduce究竟是怎么一回事?
- 淘系数据模型治理优秀实践
- 大数据分析对医疗保健的影响
- 当我们说大数据Hadoop,究竟在说什么?
- 2022年及以后大数据的五个发展趋势
- 网易严选离线数仓治理实践
- 2023 年数据治理趋势
- 一份“靠谱”的年度经营计划,你学会了吗?
- 漫谈对大数据的思考
- 测试一下,读懂数据的能力,你有吗?
- 用艺术的眼光探索数据之美
- 聊聊数据分析成果如何落地