Arduino 連接 DHT11感測空氣中的溫濕度,以光敏電阻感測是否有人經過,再以LCD顯示目前空氣溫濕度。
Arduino:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
#define dht_dpin 2
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, NEGATIVE); // 設定 LCD I2C 位址
dht DHT;
int newPhotoValue, oldPhotoValue, diff;
bool flag=false;
void setup() {
lcd.begin(16, 2); // 初始化 LCD,一行 16 的字元,共 2 行,預設開啟背光
lcd.clear();
lcd.backlight();
lcd.noCursor();
delay(1000);
lcd.setCursor(0, 0); // 設定游標位置在第一行行首
lcd.print(“Hello, world!”);
lcd.setCursor(0, 1); // 設定游標位置在第二行行首
lcd.print(“Shrimp”);
delay(1000);
lcd.clear();
}
void loop() {
//lcd.clear();
newPhotoValue = analogRead(A0);
//lcd.print(newPhotoValue);
if(flag){
lcd.setCursor(0,0);
lcd.print(“Welcome to RS’s”);
lcd.setCursor(0,1);
lcd.print(“Weather Station”);
flag = false;
}
diff= oldPhotoValue-newPhotoValue;
if(abs(diff)>15){
unsigned long time = millis();
while(millis() – time < 1000){
lcd.clear();
DHT.read11(dht_dpin);
lcd.setCursor(0,0);
lcd.print(“Humi: “);
lcd.print(DHT.humidity);
lcd.print(“%”);
lcd.setCursor(0,1);
lcd.print(“temp: “);
lcd.print(DHT.temperature);
lcd.print(“oC”);
delay(500);
}
flag= true;
}
delay(1000);
oldPhotoValue= newPhotoValue;
}
library:
DHT11, LCD:
https://drive.google.com/folderview?id=0B9fmfEayrYvMRU0wejUyNUFGMkk&usp=sharing
參考:
1. 以arduino uno 燒錄 arduino pro mini:
http://sky4s.blogspot.tw/2014/04/arduino-unoarduino-pro-mini-use-arduino.html
補充: 燒錄時,板子記得選arduino pro mini
2. LCD I2C- G.T.WANG:
http://sky4s.blogspot.tw/2014/04/arduino-unoarduino-pro-mini-use-arduino.html
3. DHT11- 聰明人求知心切:
http://alex9ufoexploer.blogspot.tw/2013/04/arduino-humidity-sensor-dht11-tutorial_6.html