# DHT11溫溼度模組

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-Lx00XGnqAn7JxlS80YM%2F-Lx00rWF6rAzn_73w1sN%2F01.png?alt=media\&token=ee416eb4-3913-4a58-8cef-e9cdaaf670f4)

## 專案說明

使用「科易KEYES Arduino UNO R3 開發板」連接「DHT11溫溼度模組」,讀取DHT11溫溼度模組所測到的溫度、溼度。

此**DHT11溫溼度模組**包含於「[洞洞兩教學材料包 Education Kit 002](https://www.robotkingdom.com.tw/product/rk-education-kit-002/)」內。

## KEYES Arduino UNO R3電路圖

* [KEYES Arduino UNO R3&#x20;  ](https://www.robotkingdom.com.tw/product/keyes-uno-r3/)
* 通用型彩色Sensor shield v5.0感測器擴充板
* DHT11溫溼度模組

**DHT11溫溼度模組**是**數位訊號**輸出， 可以接「D0 \~ D13」的KEYES Arduino UNO R3訊號端上。 本範例連接到「**D3**」

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-Lx00XGnqAn7JxlS80YM%2F-Lx01wY1UkUoHIG9xq3C%2F02.png?alt=media\&token=81ddb4a5-3661-4653-9904-ecaa32c01650)

## Arduino 程式

由於DHT11溫溼度模組是函式庫型模組，所以需要先在Arduino上下載函式庫，下載方法如下。

在Arduino上選擇工具，選擇管理程式庫，在管理程式庫搜尋DHT sensor選擇DHT sensor library by adafruit版本1.3.6並安裝。

{% hint style="info" %}
注意：只下載此函式庫的話，編譯範例程式會出現錯誤，

需下載以下函式庫並放入Arduino函式庫中(Arduino資料夾中libraries資料夾裡面)
{% endhint %}

[https://github.com/adafruit/Adafruit\_Sensor](https://github.com/adafruit/Adafruit_Sensor)

函式庫名稱為 Adafruit\_Sensor-master

以上函式庫也是官方版1.3.0以上函式庫所額外需要的函式庫，下載之後放到Arduino函式庫目錄中，就能解決1.3.0版以上編譯錯誤的問題。

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-Lx00XGnqAn7JxlS80YM%2F-Lx02JO4n5BerH1PJGB-%2F03.png?alt=media\&token=60c45863-ec11-4dc2-a3b0-466c5cc4a9d7)

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-Lx00XGnqAn7JxlS80YM%2F-Lx02NbNfRUpqez7Dal7%2F04.png?alt=media\&token=92d546d6-34df-44b0-b1fb-220c533c83fa)

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-Lx00XGnqAn7JxlS80YM%2F-Lx02SeZf1dxtFGYZcr6%2F05.png?alt=media\&token=760f4905-0967-4321-969e-082b8a7bdf5b)

按右上角Clone or download選擇Download ZIP

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-Lx00XGnqAn7JxlS80YM%2F-Lx02XT9Mguo3UzwAmlD%2F06.png?alt=media\&token=7528a15c-0b6d-4873-bcb3-f57a50ae7700)

接著把下載好的ZIP檔解壓縮到Arduino資料夾中libraries資料夾

每1秒鐘會讀取一次DHT11溫溼度模組所測得的溫度、溼度 。

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-Lx00XGnqAn7JxlS80YM%2F-Lx02dxK1TEDKwqUPypt%2F07.png?alt=media\&token=8b324672-d954-41fd-87fd-eddfd383890a)

產生出的 Arduino 程式如下

```c
#include <DHT.h>

DHT dht11_p3(3, DHT11);

void setup()
{

  Serial.begin(9600);

  dht11_p3.begin();
}


void loop()
{
  Serial.print("溫度：");
  Serial.println(dht11_p3.readTemperature());
  Serial.print("濕度：");
  Serial.println(dht11_p3.readHumidity());
  delay(1000);
}

```
