學習使用DHT11溫溼度感應器來測量溫度,並顯示於 I2C 2X16
二、重點說明
DHT11功能說明:
1、濕度測量範圍:20~90%RH;
2、濕度測量精度:±5%RH;
3、溫度測量範圍:0~50℃
4、溫度測量精度:±2℃
5、電源供應範圍: 3~5V
6、頻率不可超過:0.5Hz (每2秒一次)
三、接線圖
"+" vcc 極接5V,v
"-" Gnd 極接GND,
data pin接上要輸入的Pin腳=>接在2
四、程式碼
DHT11的程式碼1
#include <dht.h> #define dht_dpin 2 //定義訊號要從Pin 2 進來 dht DHT; void setup(){ Serial.begin(9600); delay(300); //Let system settle Serial.println("Humidity and temperature\n\n"); delay(700); //Wait rest of 1000ms recommended delay before //accessing sensor } void loop(){ DHT.read11(dht_dpin); //去library裡面找DHT.read11 Serial.print("Humidity = "); Serial.print(DHT.humidity); Serial.print("% "); Serial.print("temperature = "); Serial.print(DHT.temperature); Serial.println("C "); delay(1000); //每1000ms更新一次 }DHT11 + I2C LCD 程式碼
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#include <dht.h>
#define dht_dpin 2 //定義訊號要從Pin 2 進來
dht DHT;
int no=0;//計數器
void setup()
{
lcd.init(); // initialize the lcd
Serial.begin(9600);
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("I2C+DHT11");
Serial.println("Humidity and temperature\n\n");
delay(1000);
}
void loop()
{
no++;
DHT.read11(dht_dpin); //去library裡面找DHT.read11
//序列埠輸出
Serial.print("Humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
//資料顯示在LCD
lcd.setCursor(0, 0);
lcd.print("No.");
lcd.print(no);
lcd.setCursor(8, 1);
lcd.print("H=");
lcd.print(DHT.humidity);
lcd.setCursor(0, 1);
lcd.print("T=");
lcd.print(DHT.temperature);
delay(500); //每500ms更新一次
lcd.clear();
}
五、參考資料:
http://ming-shian.blogspot.tw/2014/05/arduino19dht11.html