Arduino用DHT11模块与LCD1602实现温湿度测量及显示

本文主要利用Arduino开发板实现温湿度测量及显示

废话不多说。先上实物图:
实物图
主要使用到的模块包括 Arduino开发板、1602液晶显示屏、DHT11温湿度模块、两个10k电阻、5V电源

电路图

在这里插入图片描述

代码

#include <LiquidCrystal.h>
#include <DHT.h>
#define DHTPIN 8 //定义DHT11输入管脚为8管脚
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);

const int rs = 12, en = 11, d4 = 2, d5 = 3, d6 = 4, d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() { 
 Serial.begin(9600); 
 lcd.begin(16, 2);
 dht.begin();
 
}

void loop() {
 delay(2000);
float h = dht.readHumidity();//读湿度
float t = dht.readTemperature();//读温度,默认为摄氏度
 
  lcd.setCursor(0,1);
  lcd.print("Hum: ");//湿度
  lcd.print((int)h);
  lcd.print(" %");
  
  
  lcd.setCursor(0,0);
  lcd.print("Tem: ");//温度
  lcd.print((int)t);
  lcd.print(" C");
}
 /// 代码部分感谢CSDN的各位大佬

关于DHT.h以及LiquidCrystal.h模块的导入

在Arduino IDE界面选择管理库
在这里插入图片描述
搜索想要的库安装即可

这是一款带有DHT11温度/湿度传感器的Arduino Uno,并带有由电源供电的LCD屏幕。 这个项目需要以下内容: 所有零件都可以在sparkfun或adafruit购买。或者你可以像我一样做,并尽可能地从旧设备中拯救。 Arduino(我使用了UNO R3,但任何5V都可以工作) 面包板 DHT11温湿度传感器 10k欧姆电位器 16x2液晶屏幕 触觉按钮 USB AB电缆 充电宝 跳线 现在是把所有电线连接到设备的时候了。请参阅Fritzing原理图(请注意,我在面包板上的两个电源导轨之间没有跳线,如果同时使用这两个电源导轨,您将需要它们): 我使用的LCD显示器是从一个旧的火灾报警器面板中恢复的。引脚15和16位于引脚1之前,而引脚16不是Gnd,实际上是5v,引脚15是Gnd。请仔细检查你自己的显示器,并确保你的引脚是正确的。由于我的显示器引脚排列几乎与其他人不同,所以我使用“标准”显示屏制作了Fritzing原理图,而不是我的确切引脚。 Gnd - >面包板上的负电源 5v - >面包板上的正轨 DTH11 Pin1 - > 5v和10k欧姆电阻 Pin2 - > Arduino Pin8和10k欧姆电阻 Pin3 - >无连接 Pin4 - > Gnd 16x2液晶屏幕 Pin1 - > Gnd Pin2 - > 5v Pin3 - > 10k欧姆电位器刮水器针(中间针,POT上的另外两个针变为5v和Gnd) Pin4 - > Arduino Pin12 Pin5 - > Gnd Pin6 - > Arduino Pin11 Pin7 - >没有连接 Pin8 - >没有连接 Pin9 - >没有连接 Pin10 - >没有连接 Pin11 - > Arduino Pin5 Pin12 - > Arduino Pin4 Pin13 - > Arduino Pin3 Pin14 - > Arduino Pin2 Pin15 - > 5v Pin16 - >触觉按钮(另一侧的按钮去Gnd) 完成所有接线后,将电源插入Arduino。 您的液晶显示器和DHT11应该启动。按下圆润按钮,LCD背光应该点亮。 现在你有一个带有实时显示的便携式温度和湿度传感器。 这帮助我确定了我家中最有野味的窗户,以及如何最好地设置房屋通风。
以下是使用ArduinoDHT11模块LCD1602显示器来显示温度和湿度的基本代码。 首先,我们需要使用DHT11库读取传感器的值。在Arduino IDE中,打开“示例” -> “DHT sensor library” -> “DHTtester”并将以下代码复制到新的Arduino文件中: #include <DHT.h> #define DHTPIN 2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); } void loop() { delay(2000); float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.println(" *C"); } 现在,我们可以在串口监视器中看到传感器的读数。接下来,我们将使用LCD1602显示器将读数显示在屏幕上。连接LCD1602显示器的引脚如下: LCD1602 引脚 Arduino 引脚 -------------------------------- VSS GND VDD 5V VO 10K 滑动电位器 RS 12 RW GND EN 11 D4 5 D5 4 D6 3 D7 2 A 5V K GND 使用LiquidCrystal库控制LCD1602显示器。在Arduino IDE中,打开“示例” -> “LiquidCrystal” -> “HelloWorld”并将以下代码复制到新的Arduino文件中: #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); lcd.print("Hello, world!"); } void loop() { lcd.setCursor(0, 1); lcd.print("Arduino rocks!"); delay(1000); lcd.clear(); delay(1000); } 现在,我们可以将这两个示例合并到一个程序中,并使用lcd.print()将温度和湿度显示LCD1602上。以下是完整的代码: #include <DHT.h> #include <LiquidCrystal.h> #define DHTPIN 2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { Serial.begin(9600); dht.begin(); lcd.begin(16, 2); lcd.print("Temperature:"); } void loop() { delay(2000); float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } lcd.setCursor(0, 1); lcd.print("Temp: "); lcd.print(t); lcd.print(" C Humidity: "); lcd.print(h); lcd.print(" %"); } 现在,我们已经成功地将温度和湿度显示LCD1602上。可以通过调整代码来改变显示的格式或添加其他功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值