2016年1月24日 星期日

Arduino的教學10- 利用DHT11抓取溫度和濕度,並顯示於2x16 I2C LCD上

一、學習目標:
學習使用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
  1.  
  2. #include <dht.h>
  3. #define dht_dpin 2 //定義訊號要從Pin 2 進來
  4. dht DHT;
  5. void setup(){
  6. Serial.begin(9600);
  7. delay(300); //Let system settle
  8. Serial.println("Humidity and temperature\n\n");
  9. delay(700); //Wait rest of 1000ms recommended delay before
  10. //accessing sensor
  11. }
  12. void loop(){
  13. DHT.read11(dht_dpin); //去library裡面找DHT.read11
  14. Serial.print("Humidity = ");
  15. Serial.print(DHT.humidity);
  16. Serial.print("% ");
  17. Serial.print("temperature = ");
  18. Serial.print(DHT.temperature);
  19. Serial.println("C ");
  20. delay(1000); //每1000ms更新一次
  21. }
  22.  
DHT11 + I2C LCD 程式碼
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
  4.  
  5. #include <dht.h>
  6. #define dht_dpin 2 //定義訊號要從Pin 2 進來
  7. dht DHT;
  8.  
  9. int no=0;//計數器
  10. void setup()
  11. {
  12. lcd.init(); // initialize the lcd
  13. Serial.begin(9600);
  14. // Print a message to the LCD.
  15. lcd.backlight();
  16. lcd.setCursor(0, 0);
  17. lcd.print("I2C+DHT11");
  18. Serial.println("Humidity and temperature\n\n");
  19. delay(1000);
  20. }
  21.  
  22. void loop()
  23. {
  24. no++;
  25. DHT.read11(dht_dpin); //去library裡面找DHT.read11
  26. //序列埠輸出
  27. Serial.print("Humidity = ");
  28. Serial.print(DHT.humidity);
  29. Serial.print("% ");
  30. Serial.print("temperature = ");
  31. Serial.print(DHT.temperature);
  32. Serial.println("C ");
  33.  
  34. //資料顯示在LCD
  35. lcd.setCursor(0, 0);
  36. lcd.print("No.");
  37. lcd.print(no);
  38. lcd.setCursor(8, 1);
  39. lcd.print("H=");
  40. lcd.print(DHT.humidity);
  41. lcd.setCursor(0, 1);
  42. lcd.print("T=");
  43. lcd.print(DHT.temperature);
  44. delay(500); //每500ms更新一次
  45. lcd.clear();
  46. }
五、參考資料: http://ming-shian.blogspot.tw/2014/05/arduino19dht11.html