1. 介紹
用溫度傳感器 DHT11 檢測溫度2. 硬件
2.1 ESP32 開發板
DHT11 溫度傳感器
2.2 接線
[ESP32 IO14 - DHT22 DATA]
[ESP32 GND - DHT22 GND]
[ESP32 GND - DHT22 GND]
2.3 接線圖
<ignore_js_op style='color: rgb(68, 68, 68); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: "Microsoft YaHei", SimHei, Verdana, Arial, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0px; white-space: normal; -ms-word-wrap: break-word; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'>
3.軟件
#include "DHT.h" //here we use pin IO14 of ESP32 to read data #define DHTPIN 14 //our sensor is DHT22 type #define DHTTYPE DHT22 //create an instance of DHT sensor DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(115200); Serial.println("DHT22 sensor!"); //call begin to start sensor dht.begin(); } void loop() { //use the functions which are supplied by library. float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } // print the result to Terminal Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.println(" *C "); //we delay a little bit for next read delay(2000); }