[王老师玩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 接线图

    <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);
}

猜你喜欢

转载自www.cnblogs.com/ezezwyj/p/9485522.html