###### tags: `Arduino` # DHT22 sensor project DHT22 library can be found in the Arduino libraries manager. Adafruit sensor library base: https://www.arduinolibraries.info/libraries/adafruit-unified-sensor LCD I2C libraries: https://www.arduinolibraries.info/libraries/liquid-crystal-i2-c ### code ```cpp= #include "DHT.h" #define DTpin 2 #define DHTTYPE DHT22 DHT dht(DTpin,DHTTYPE); #include<Wire.h> #include<LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27,16,2); const int ledr = 11, ledy = 10, ledg = 9; const int buz = 13; const int fan = 3; void setup(){ Serial.begin(9600); dht.begin(); for(int i=3;i<=13;i++){ pinMode(i,OUTPUT); } pinMode(2,INPUT); lcd.init(); lcd.backlight(); lcd.setCursor(0,0); lcd.print("start"); } void loop(){ analogWrite(fan,255); delay(1000); float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t) ) { Serial.println(F("感測器讀取失敗")); return; } Serial.print(F("濕度: ")); Serial.print(h); Serial.print(F("% 溫度: ")); Serial.print(t); Serial.println("°C"); lcd.clear(); lcd.setCursor(0,0); lcd.print("T:");lcd.print(t);lcd.print((char) 0); lcd.print("H:");lcd.print(h); if(t>=40){ //紅燈亮 digitalWrite(11,1); digitalWrite(10,0); digitalWrite(9,0); lcd.setCursor(1,0); lcd.print("Temp Alarming"); for(int i=0;i<20;i++){ //i上限是蜂鳴器次數 tone(13, 440); delay(200); noTone(13); // 停止播放 delay(200); } }else if(t>=35){ //黃燈亮 digitalWrite(11,0); digitalWrite(10,1); digitalWrite(9,0); }else{ //綠燈亮 digitalWrite(11,0); digitalWrite(10,0); digitalWrite(9,1); } } ``` ### wiring ![](https://i.imgur.com/Ol9UuS0.png) tmpIC => DHT22 Datapin is arduino pin2 RGB led => lcd monitor | Vin | GND | SCL | SDA | | --- | --- | --- | --- | | 5V | GND | A5 | A4 |