# DHT11溫溼度感測器模組

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-LoyhZ7TGT0i8ZxuGcbY%2F-LoyhbjdXSoZjM-xe0vu%2Flinkit7697_dht11_00.png?generation=1568715215833916\&alt=media)

## LinkIt 7697 專案說明

使用「LinkIt 7697 NANO Breakout」連接「DHT11溫溼度感測器模組」， 每1秒讀取DHT11溫溼度感測器模組溫度和濕度數值 。

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

### LinkIt 7697 電路圖

* [LinkIt 7697](https://www.robotkingdom.com.tw/product/linkit-7697/)
* LinkIt 7697 NANO Breakout
* DHT11溫溼度感測器模組

**DHT11溫溼度感測器模組**是**數位訊號**輸出， 可以接「D2」的 LinkIt 7697 NANO Breakout訊號端上。 本範例連接到「**D2**」。

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-LoyhZ7TGT0i8ZxuGcbY%2F-LoyhbjoOQNwL7ONPUWc%2Flinkit7697_dht11_01.png?generation=1568715211789597\&alt=media)

### BlocklyDuino 積木畫布

每秒鐘讀取DHT溫溼度感測器的溫度和濕度一次，並顯示在序列埠監控視窗上。

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-LoyhZ7TGT0i8ZxuGcbY%2F-Loyhbjx4phSYAeecwSg%2Flinkit7697_dht11_02.png?generation=1568715212161414\&alt=media)

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-LoyhZ7TGT0i8ZxuGcbY%2F-LoyhbjzLusJvXK5GDXL%2Flinkit7697_dht11_03.png?generation=1568715212277111\&alt=media)

### Arduino 程式

```c
#include <DHT.h>
DHT dht11_p2(2, DHT11);

void setup()
{
  Serial.begin(9600);
  dht11_p2.begin();
}

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

## micro:bit 專案說明

使用「micro:bit搭配科易KEYES micro:bit 感測器擴充板 V2」連接「DHT11溫溼度感應器模組」, 每1秒讀取DHT11溫溼度應器模組溫度和濕度數值 。

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

### micro:bit電路圖

* [BBC micro:bit 微控制板V1.5  ](https://www.robotkingdom.com.tw/product/bbc-microbit-1/)
* [科易KEYES micro:bit 感測器擴充板 V2  ](https://www.robotkingdom.com.tw/product/keyes-microbit-sensor-breakout-v2/)
* DHT11溫溼度感應器模組

**DHT11溫溼度感應器模組**是**數位訊號**輸出， 可以接「0、 1、 2、 8、 12、13、14、 15、 16、 19、20」的 micro:bit訊號端上，建議使用的是0、1、2腳位。本範例連接到「**P0**」。

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-MflacbINBFSy_cg0cUq%2F-MfleI2oqg01pTMBorJU%2F07.DHT11MICEOBITV1.5.png?alt=media\&token=6dfaf88a-0861-4158-8d6a-baad4a0840cd)

### Microsoft MakeCode積木畫布 (適用microbit v1.5)

{% hint style="info" %}
注意:\
測試時發現電壓如果切換到5V時會發生無法正常顯示溫、濕度的問題、請記得把V1切到3V3當中(如上圖紅框處)\
以下程式及函式庫目前測試不適用於micro:bit v2，如需再micro:bit v2使用請使用下方另一個函式庫的程式。
{% endhint %}

在使用DHT11溫溼度感應器之前，需要擴展DHT11溫溼度感應器的指令庫。打開makecode程式設計平臺，滾動至指令區最底部，依序點擊進階----擴展，並使用以下網址安裝套件。

DHT11溫溼度感應器套件安裝網址：[https://github.com/lioujj/pxt-sensors](https://github.com/lioujj/pxt-sensors)

![新增擴展套件位置](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-M-NGenNSBx5AyGivu9s%2F-M-NHaFIP9Jwi0T2mjPt%2F02.jpg?alt=media\&token=4c605695-6cc3-4d0e-b075-c035e5957b09)

![使用網址安裝sensors的套件](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-M-NGenNSBx5AyGivu9s%2F-M-NHf54KwAUC7Rr8Oy5%2F03.jpg?alt=media\&token=170d7e5d-8537-46e0-8bc6-7544f45df129)

![安裝完成後即可在指令區看到感應器選項裡面及有DHT11的套件](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-M-NGenNSBx5AyGivu9s%2F-M-NHjte6uymJi5Ltmn7%2F04.jpg?alt=media\&token=bf980b32-04e2-4b62-9860-e55b90784429)

每按下A或B按鈕會讀取並顯示DHT11溫溼度感應器的溫度和濕度一次，並可透過micro:bit顯示指示燈看到DHT11溫溼度感應器的溫度和濕度的狀態。

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-M-NGenNSBx5AyGivu9s%2F-M-NHqCa-Gq9HszHyCTq%2F05.JPG?alt=media\&token=b8380359-e4ad-4f8d-8259-6a98f2f3ce1e)

### JavaScript 程式

```javascript
let temp = 0
let humi = 0
input.onButtonPressed(Button.A, function () {
    temp = sensors.get_DHT11_value(
    DigitalPin.P0,
    sensors.Dht11Result.Celsius
    )
    basic.showString("TEMP:")
    basic.showNumber(temp)
})
input.onButtonPressed(Button.B, function () {
    humi = sensors.get_DHT11_value(
    DigitalPin.P0,
    sensors.Dht11Result.humidity
    )
    basic.showString("HUMI:")
    basic.showNumber(humi)
})
basic.forever(function () {
    
})
```

## DHT11更新Micro:bitv2(可使用函式庫)

### micro:bit電路圖

* [BBC micro:bit 微控制板V2  ](https://www.robotkingdom.com.tw/product/bbc-microbit-1/)
* [科易KEYES micro:bit 感測器擴充板 V2  ](https://www.robotkingdom.com.tw/product/keyes-microbit-sensor-breakout-v2/)
* DHT11溫溼度感應器模組

**DHT11溫溼度感應器模組**是**數位訊號**輸出， 可以接「0、 1、 2、 8、 12、13、14、 15、 16、 19、20」的 micro:bit訊號端上，建議使用的是0、1、2腳位。本範例連接到「**P0**」。

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-MflgKy5bdsvOgvd-qHB%2F-MflhK0GACSrFD69ig56%2F07.DHT11MICEOBITV2.png?alt=media\&token=3437c3f7-0cd5-46bc-8bd0-64408f634fa3)

{% hint style="info" %}
**注意: 測試時發現輸入電壓如果切換到5V時會發生無法正常顯示溫、濕度的問題、請記得把V1切到3V3當中(如上圖紅框處)。**

**以下函式庫測試後適用於micro:bit v2，發現一開始如果溫、濕度顯示-999.0為正常，請等程式驗證後即可正常顯示溫、濕度，如還是無法正常顯示可以嘗試將MICRO USB供電改為擴充版供電、重新插拔感測器的3pin排線或換一條線或是按板子背後的RESET鈕重置程式讓他重新讀取數值。**

**如無法正常顯示溫濕度請先檢查上述問題，有無正確測試過。**<br>
{% endhint %}

在使用DHT11溫溼度感應器之前，需要擴展DHT11溫溼度感應器的指令庫。打開makecode程式設計平臺，滾動至指令區最底部，依序點擊進階----擴展，並使用以下網址安裝套件。

DHT11溫溼度感應器套件安裝網址：<https://github.com/alankrantas/pxt-DHT11_DHT22>

![進階；擴展套件的位置](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-MfljYz43W2bu_4CvOx2%2F-MfllhIzlBfDmMKLPLjg%2Fimage.png?alt=media\&token=be320ffd-0702-463c-8f27-65212e110d4d)

![使用網址安裝sensors的套件](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-MfljYz43W2bu_4CvOx2%2F-MflkSlb7eM44nhhfsV3%2Fimage.png?alt=media\&token=0bf57583-9cfe-4dcd-a418-e41f03f6c1e6)

![使用搜尋DHT11安裝sensors的套件](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-MfljYz43W2bu_4CvOx2%2F-Mflkwcb8qb-qbvcQUZB%2Fimage.png?alt=media\&token=528e162f-3dbb-4ebf-bdef-51c55f27d3b1)

新增後在上方可以看見增加的DHT11/DHT22套件

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-MfljYz43W2bu_4CvOx2%2F-MflmNf-kmwoobAMAreP%2Fimage.png?alt=media\&token=9e9a78c2-397a-4fad-b33c-752f58305ae7)

### Microsoft MakeCode積木畫布 (適用microbit v2)

使用pxt-DHT11\_DHT22此函式庫時(完整程式如下)，需先宣告你要使用攝氏溫度還是華氏溫度

並且要設定此感測器Query為DHT11

設定Data pin為P0

Pin pull up為true

Serial output為false

Wait 2 sec after query為true

![](https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LaZQFBYOS3O0ksiEmR1%2F-MflmsJk04Nq0DAbMSwj%2F-MflnEipQLQ7RGSHuX2c%2F07.DHT11MICEOBITV2CODE.PNG?alt=media\&token=1bb5b1ba-a84e-4a8c-b2ab-c13dc0c43e6d)

可透過micro:bit顯示指示燈看到DHT11溫溼度感應器的溫度和濕度的狀態。會四捨五入小數點輪流顯示T:目前溫度H:目前濕度。

### JavaScript 程式

```javascript
basic.forever(function () {
    basic.pause(100)
    dht11_dht22.selectTempType(tempType.celsius)
    dht11_dht22.queryData(
    DHTtype.DHT11,
    DigitalPin.P0,
    true,
    false,
    true
    )
    basic.showString("T:")
    basic.showString("" + (Math.round(dht11_dht22.readData(dataType.temperature))))
    basic.showString("H:")
    basic.showString("" + (Math.round(dht11_dht22.readData(dataType.humidity))))
})
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cavedu.gitbook.io/cavedu/rk001/module_ui/linkit7697_dht11.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
