硬件清單
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); }