《Arduino 手册(思路与案例)》栏目介绍:
在电子制作与智能控制的应用领域:广泛涉及了Arduino BLDC、Arduino CNC、Arduino ESP32 SPP、Arduino FreeRTOS、Arduino FOC、Arduino GRBL、Arduino HTTP、Arduino HUB75、Arduino IoT Cloud、Arduino JSON、Arduino LCD、Arduino OLED、Arduino LVGL、Arduino PID 及 Arduino TFT 等方面的相关拓展思路和众多参考案例。本专栏目前博客近2300篇。
https://blog.csdn.net/weixin_41659040/category_12422453.html
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手试试多做实验,不管成功与否,都会记录下来——小小的进步或是搞不掂的问题,希望能够抛砖引玉。
**【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十八:DHT22 单总线数字温湿度传感器 AM2302电子积木模块 **
DHT22 是一款常用的单总线数字温湿度传感器,以下是关于它的详细介绍:
1、基本概述
DHT22 采用单总线通信协议,能够实时测量环境中的温度和湿度,并将测量结果以数字信号的形式输出。它具有体积小、功耗低、精度高、稳定性强等优点,广泛应用于智能家居、气象监测、农业温室等领域。
2、主要特性
高精度测量:温度测量范围为 - 40℃至 80℃,精度可达 ±0.5℃;湿度测量范围为 0% 至 100% RH,精度可达 ±2% RH。
单总线接口:仅需一个引脚即可与微控制器进行通信,简化了硬件连接,降低了成本。
数字输出:直接输出数字信号,无需额外的模数转换电路,方便微控制器进行读取和处理。
低功耗:工作电流通常在 0.5mA 至 2mA 之间,适合电池供电的设备使用。
响应速度快:能够在短时间内快速测量并更新温湿度数据,一般响应时间在 1 秒以内。
3、应用场景
智能家居:用于监测室内温湿度,实现自动调节空调、加湿器、除湿器等设备的运行,提供舒适的居住环境。
气象监测:可以作为小型气象站的温湿度传感器,实时监测环境温湿度变化,为气象预报和研究提供数据支持。
农业温室:帮助农民监测温室内的温湿度,及时采取措施调节环境,为农作物生长提供最佳条件,提高农作物产量和质量。
工业生产:在一些对温湿度要求较高的工业生产环境中,如电子制造、药品生产等,用于监测和控制生产环境的温湿度,确保产品质量。
实验模块接线示意图
【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十八:DHT22 单总线数字温湿度传感器 AM2302电子积木模块
项目之三:使用 DHT22 传感器读取温湿度数据并计算热指数
实验开源代码
/*
【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十八:DHT22 单总线数字温湿度传感器 AM2302电子积木模块
项目之三:使用 DHT22 传感器读取温湿度数据并计算热指数
*/
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
// Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}
代码解读
这段代码使用 DHT22 传感器 读取 温度和湿度数据,并计算 热指数,核心逻辑如下:
-
传感器连接
✅ #define DHTPIN 2 → 数据线连接 Arduino 的 D2 引脚
✅ #define DHTTYPE DHT22 → 指定传感器类型(DHT22,可更换为 DHT11 或 DHT21)
✅ 供电连接 → 5V 或 3.3V(根据开发板逻辑电压选择)
✅ 上拉电阻 → 使用 10kΩ 电阻连接数据线与电源,确保信号稳定 -
初始化
✅ Serial.begin(9600); → 启动串口通信
✅ dht.begin(); → 初始化 DHT22 传感器 -
读取温湿度
✅ float h = dht.readHumidity(); → 获取湿度
✅ float t = dht.readTemperature(); → 获取温度(摄氏度)
✅ float f = dht.readTemperature(true); → 获取温度(华氏度) -
数据处理
✅ 检测读取失败 → isnan(h) || isnan(t) || isnan(f) 确保数据有效
✅ 计算热指数(体感温度) → dht.computeHeatIndex(t, h, false); 以摄氏度计算
✅ dht.computeHeatIndex(f, h); 以华氏度计算 -
输出数据
✅ 湿度 → Serial.print(h); Serial.print(“%”);
✅ 温度(摄氏 & 华氏) → Serial.print(t); Serial.print("°C "); Serial.print(f); Serial.print(“°F”);
✅ 热指数 → Serial.print(hic); Serial.print("°C "); Serial.print(hif); Serial.println(“°F”); -
循环采集
✅ delay(2000); → 每 2 秒采集一次数据,适合定期监测
这段代码让 DHT22 传感器不断测量环境温湿度,并计算体感温度后输出到串口。
实验串口返回情况