Arduino - 光敏電阻 + 溫溼度感測器 + LCD + LED(photocell + temperature and humidity sensor + LCD +LED)
目的:
將光敏電阻、溫溼度感測器的值顯示於LCD(並讓LCD捲動顯示)
且當溫度感測器到達一個27度C,使一個LED閃爍(未達門檻不閃爍)。
成果:
Arduino Code:
#include < Wire.h > #include < SPI.h > #include < dht11.h > // DHT11 感測器程式庫
#include < LiquidCrystal.h >
const byte LED = 8;
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
dht11 DHT11; // 宣告溫濕度檢測器程式物件
const byte dataPin = 2;
int LDR = 0; //光敏電阻輸入,接在anallog pin 0
float readLDR = 0; //光敏電阻感應到的光源產生的電阻值
void setup() {
pinMode(LED, OUTPUT);
lcd.begin(16, 2); // 初始化 LCD
lcd.setCursor(2, 0);
lcd.print("Temp");
lcd.setCursor(0, 1);
lcd.print("Hu");
Serial.begin(9600);
}
void loop() {
lcd.setCursor(0, 1);
readLDR = analogRead(LDR); //存取光敏電阻的電阻值
int chk = DHT11.read(dataPin);
if (chk == 0) {
lcd.setCursor(7, 0); // 顯示溫度
lcd.print((float) DHT11.temperature, 2);
Serial.println(DHT11.temperature);
lcd.print((char) 0xDF);
lcd.print("C");
if (DHT11.temperature > 27) {
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
}
lcd.setCursor(3, 1); // 顯示濕度
Serial.println(DHT11.humidity);
lcd.print((float) DHT11.humidity, 2);
lcd.print("%");
lcd.setCursor(10, 1);
float test = (readLDR * 100) / 1024; // 顯示光敏
lcd.print(test);
lcd.print("%");
Serial.println(readLDR);
}
delay(1000);
}
留言
張貼留言