DHT22 温湿度在 Arduino RJT43 的 7 段显示器上
该项目演示了如何使用Arduino构建一个简单而有效的系统,以在7段显示器上显示温度和湿度读数。该设置适用于需要监测环境条件的各种应用。该项目将DHT22温湿度传感器与TM1637 7段显示器结合在一起,提供清晰且易于读取的输出。


项目创意:
- 监测家庭环境以实现最佳舒适度。
- 在温室或生态瓶中跟踪条件。
- 搭建一个基本的气象站。
- 在数据记录项目中显示温度和湿度。
硬件/组件
要构建此项目,您需要以下组件:
- Arduino Uno(或兼容板)
- DHT22 温湿度传感器
- TM1637 七段显示器
- 连接电线
- 面包板(可选,但推荐使用)
布线指南



接线很简单。请参考视频(在视频中的 01:51)以获取视觉指导。主要连接为:
- TM1637 显示器:VCC连接到5V,GND连接到GND,CLK连接到Arduino引脚2,DIO连接到Arduino引脚3(视频中的时间是02:00)。
- DHT22传感器:VCC 接到 5V,GND 接到 GND,DATA 接到 Arduino 引脚 9(视频中的时间是 02:20)。请注意,视频中使用 Arduino 引脚 8 为传感器提供 5V(视频中的时间是 02:41)。
代码解释
Arduino代码使用了两个库:TM1637Display用于七段显示器,DHT用于DHT22传感器。代码的可配置参数主要位于开头:
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
#define DHTPIN 9 // DHT22 data pin
#define DHTTYPE DHT22 // DHT sensor type
这些行定义了连接到显示屏和DHT22传感器的Arduino引脚。如果您使用不同的引脚,您可能需要调整这些设置。getTemp()在视频的07:03处,函数是关键。通过传递字符串参数,它使您能够从DHT22传感器检索不同的值:
float getTemp(String req) {
// ... (Sensor reading code) ...
if(req =="c"){ return t; } // Celsius
else if(req =="f"){ return f; } // Fahrenheit
// ... (Other options for Kelvin, humidity, heat index) ...
}
此功能简化了从传感器读取不同数据(摄氏度、华氏度、湿度、热指数)。主循环使用此功能获取数据并在七段显示器上显示,将浮点值四舍五入为整数以便于显示(视频中在07:14)。
现场项目/演示
视频(在00:32的录像中)展示了该项目的现场演示。7段显示器清楚地显示了华氏温度。视频还演示了如何修改代码以显示摄氏度、开尔文、湿度和热指数值(在08:21的录像中)。
章节
- [00:06] 引言和项目概述
- [00:53] 入门和组件概述
- [01:51] 连接TM1637显示屏
- [02:20] 接线 DHT22 传感器
- [03:14] 代码说明:TM1637 设置
- [04:00] 代码说明:DHT22 设置
- [05:54] 代码说明:主循环和显示功能
- [07:03] 代码说明:getTemp() 函数
- [08:21] 演示和不同的输出选项
56-Arduino code and video for a DHT12 Temperature and Humidity Sensor with TM1637 Display
语言: C++
/*
* Original code from TM1637 https://github.com/avishorp/TM1637
* Original code and library for DHT22 https://github.com/adafruit/DHT-sensor-library
* Modified for Robojax video on January 7, 2018
* by Ahmad Shamshiri, in Ajax, Ontario, Canada
* Watch the video for this code https://youtu.be/z_FvRm6Te78
* Other Arduino library and videos https://robojax.com
* Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
all in one place.
If you found this tutorial helpful, please support me so I can continue creating
content like this.
or make a donation using PayPal http://robojax.com/L/?id=64
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// ****** Start of TM1637 Display code
#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 1000
TM1637Display display(CLK, DIO);
// ****** end of TM1637 Display code
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// ****** Start of DHT code
#include "DHT.h"
#define DHTPIN 9 // what digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
// ********** end of DHT22 code
void setup()
{
Serial.begin(9600);
Serial.println("DHT22 Robojax Test with Display");
pinMode(8,OUTPUT);
digitalWrite(8,HIGH);// gives 5v for DHT22
dht.begin();
}
void loop()
{
delay(TEST_DELAY);// wait
// **** TM1637 code start
display.setBrightness(0x0f);// set brightness
uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 };// clear display values
display.setSegments(data);//clear display
// **** TM1637 code end
// Robojax.com test video
Serial.println(getTemp("h"));
int temp = round(getTemp("h"));
display.showNumberDec(temp, false, 3,1);
}// loop end
/*
* getTemp(String req)
* returns the temperature related parameters
* req is string request
* This code can display temperature in:
* getTemp("c") is used to get Celsius
* getTemp("f") is used to get Fahrenheit
* getTemp("k") is used for Kelvin
* getTemp("hif") is used to get Fahrenheit
* getTemp("hic") is used to get Celsius
* getTemp("h") is used to get humidity
* written by Ahmad Shamshiri for Robojax.com on January 7, 2018
* in Ajax, Ontario, Canada
*/
float getTemp(String req)
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's 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);
// 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);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return 0.0; //return 0.0 instead of nothing.
}
// Compute heat index in Kelvin
float k = t + 273.15;
if(req =="c"){
return t;//return Celsius
}else if(req =="f"){
return f;// return Fahrenheit
}else if(req =="h"){
return h;// return humidity
}else if(req =="hif"){
return hif;// return heat index in Fahrenheit
}else if(req =="hic"){
return hic;// return heat index in Celsius
}else if(req =="k"){
return k;// return temperature in Kelvin
}else{
return 0.000;// if no request found, return 0.000
}
}
资源与参考
尚无可用资源。
文件📁
Arduino 库(zip 格式)
-
DHT22 PCB module red
DHT22-module-red.fzpz0.01 MB
Fritzing 文件
-
DHT22 Humidity and Temperature Sensor
DHT22 Humidity and Temperature Sensor.fzpz0.01 MB -
DHT22 PCB module red
DHT22-module-red.fzpz0.01 MB -
DHT22 module white
DHT22-module-white.fzpz0.01 MB -
TM1637 Seven Segment module
TM1637.fzpz0.01 MB -
DHT11 Humitidy and Temperature Sensor (3 pins)
DHT11 Humitidy and Temperature Sensor (3 pins).fzpz0.20 MB
用户手册
-
DHT22 Temperature and Humidity sensor user's manual
robojax-DHT22_manual.pdf0.36 MB