Arduino从DHT11读取温湿度数据并显示在1602LCD
硬件清单
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); }