[王老师玩ESP32系列]第三课,DHT11温度检测

1. 介绍
用温度传感器 DHT11 检测温度2. 硬件
    2.1      ESP32 开发板
                DHT11 温度传感器
    2.2      接线

     [ESP32 IO14 - DHT22 DATA]
     [ESP32 GND - DHT22 GND]
     [ESP32 GND - DHT22 GND]
  2.3 接线图

    [王老师玩ESP32系列]第三课,DHT11温度检测_第1张图片 
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);
}

 

转载于:https://www.cnblogs.com/ezezwyj/p/9485522.html

你可能感兴趣的:([王老师玩ESP32系列]第三课,DHT11温度检测)