---
# System prepended metadata

title: 溫濕度感測器 DHT11 加上 1602LCD 以及 Serial Monitor 組成電路

---

### 溫濕度感測器 DHT11 加上 1602LCD 以及 Serial Monitor 組成電路
撰寫程式並測量溫濕度
#### 電路圖
![446057547_940999264446809_3360984933980058529_n](https://hackmd.io/_uploads/HJdwSuNB0.jpg)
![445994918_1097914901311328_4753608580978493793_n](https://hackmd.io/_uploads/r1YtruNrR.jpg)
#### 程式碼
```cpp=
/*
  LiquidCrystal Library - Hello World

 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.

 This sketch prints "Hello World!" to the LCD
 and shows the time.

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 modified 7 Nov 2016
 by Arturo Guadalupi

 This example code is in the public domain.

 https://docs.arduino.cc/learn/electronics/lcd-displays

*/

// include the library code:
#include <LiquidCrystal.h>
#include "DHT.h"                  //匯入程式庫 DHT sensor library

#define DHTPIN 8                 // DHT pin 2 連接 Arduino pin 2
#define DHTTYPE DHT11            // 適用 DHT 11

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float h = dht.readHumidity();            //讀取濕度值 h
  float t = dht.readTemperature();         //讀取攝氏溫度值 t

  
  if (isnan(h) || isnan(t)) {                    //如果讀不到數值，印出讀取失敗訊息
    Serial.println(F("Failed to read"));
    lcd.print("Failed");
    delay(3000);
    return;
  }
                                        //在序列埠螢幕中印出濕度、攝氏溫度、和華氏溫度
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");

  lcd.setCursor(0, 0);
  lcd.print("Humidity:");
  lcd.print(h);
  lcd.setCursor(0, 1);
  lcd.print("Temperature:");
  lcd.print(t);
  //lcd.print(" *C ");

  delay(3000);
}
```
#### 感想
這次真的是特別難，而且還出現大狀況，這次我特別請吳守博（此方面很上手的人）幫我看線路還都沒問題，但是顯示器還是顯示不出來，所以我就想說可能是有線壞掉，我就先上網找顯示器的每個接的地方是做甚麼的，結果拆的第一條就找到了很幸運，這次雖然難，但有跟別人大量討論讓我收穫很多。