Arduino - 光敏電阻 + 溫溼度感測器 + LCD + LED(photocell + temperature and humidity sensor + LCD +LED)


目的


將光敏電阻、溫溼度感測器的值顯示於LCD(並讓LCD捲動顯示)
且當溫度感測器到達一個27度C,使一個LED閃爍(未達門檻不閃爍)。


成果:


Arduino Code:

  1. #include < Wire.h > #include < SPI.h > #include < dht11.h > // DHT11 感測器程式庫
  2. #include < LiquidCrystal.h >
  3.  
  4. const byte LED = 8;
  5. LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
  6. dht11 DHT11; // 宣告溫濕度檢測器程式物件
  7. const byte dataPin = 2;
  8. int LDR = 0; //光敏電阻輸入,接在anallog pin 0
  9. float readLDR = 0; //光敏電阻感應到的光源產生的電阻值
  10.  
  11. void setup() {
  12. pinMode(LED, OUTPUT);
  13. lcd.begin(16, 2); // 初始化 LCD
  14. lcd.setCursor(2, 0);
  15. lcd.print("Temp");
  16. lcd.setCursor(0, 1);
  17. lcd.print("Hu");
  18. Serial.begin(9600);
  19. }
  20. void loop() {
  21. lcd.setCursor(0, 1);
  22. readLDR = analogRead(LDR); //存取光敏電阻的電阻值
  23. int chk = DHT11.read(dataPin);
  24.  
  25. if (chk == 0) {
  26. lcd.setCursor(7, 0); // 顯示溫度
  27. lcd.print((float) DHT11.temperature, 2);
  28. Serial.println(DHT11.temperature);
  29. lcd.print((char) 0xDF);
  30. lcd.print("C");
  31. if (DHT11.temperature > 27) {
  32. digitalWrite(LED, HIGH);
  33. delay(500);
  34. digitalWrite(LED, LOW);
  35. }
  36.  
  37. lcd.setCursor(3, 1); // 顯示濕度
  38. Serial.println(DHT11.humidity);
  39. lcd.print((float) DHT11.humidity, 2);
  40. lcd.print("%");
  41.  
  42. lcd.setCursor(10, 1);
  43. float test = (readLDR * 100) / 1024; //  顯示光敏
  44. lcd.print(test);
  45. lcd.print("%");
  46. Serial.println(readLDR);
  47. }
  48. delay(1000);
  49. }

留言