Arduino从DHT11读取温湿度数据并显示在1602LCD
硬件清单
Arduino NANO
1602LCD + PCF8574T模块
YL-47 DHT11模块
连线
1. 连接LCD: PCF8574T模块4pin(Gnd, Vcc, SDA i2c数据, SCL i2c时钟) 连接至Arduino接口 Gnd -> Gnd, Vcc -> Vcc, SDA -> A4, SDL -> A5
2. 连接YL-47 DHT11: Gnd -> Gnd, Vcc -> Vcc, Data-> D4
Library
除了1602需要的库以外, 需要安装两个自带的库: DHT Sensor Library by Adafruit, Adafruit Unified Sensor
测试代码
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h> #define DHTPIN 4
#define DHTTYPE DHT11 // I2C地址, 一般为0x3F, 0x20或0x27
LiquidCrystal_I2C lcd(0x27,,);
// 初始化DHT
DHT dht(DHTPIN, DHTTYPE); void setup() {
lcd.init();
lcd.backlight(); // 打开背光
Serial.begin();
dht.begin();
lcd.setCursor(,); // line 0, pos 0
lcd.print("Good Day!");
lcd.setCursor(,); // line 1, pos 0
lcd.print("H: % T:");
delay();
} void loop() {
// 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("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("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F"); lcd.setCursor(,); // line 1, pos 0
lcd.print(h);
lcd.setCursor(,); // line 1, pos 0
lcd.print(t); delay();
}
代码说明
1. DHT11启动到读取数据需要等待1~2秒
2. 温湿度的精度都为1, 没有小数部分
3. DHT库里面带了计算热指数的方法 computeHeatIndex(), 用于生成综合温湿度计算得到的热指数值
改进拼接字符串
改进后的代码, 注意: arduino里的sprintf只能格式化整数, 不能格式化浮点
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <DS3231.h> #define DHTPIN 4
#define DHTTYPE DHT11 // I2C地址, 一般为0x3F, 0x20或0x27
LiquidCrystal_I2C lcd(0x27,,);
DHT dht(DHTPIN, DHTTYPE);
DS3231 Clock;
bool century=false;
bool h12;
bool PM; void setup() {
lcd.init();
//lcd.backlight(); // 打开背光
Serial.begin();
dht.begin();
lcd.setCursor(,); // line 0, pos 0
lcd.print("Good Day Jessie~~");
lcd.setCursor(,); // line 1, pos 0
lcd.print("H: % T: T:");
delay();
} void loop() {
char str[];
sprintf(
str,
"%02d-%02d %02d:%02d:%02d ",
Clock.getMonth(century),
Clock.getDate(),
Clock.getHour(h12, PM),
Clock.getMinute(),
Clock.getSecond()); lcd.setCursor(,); // line 0, pos 0
lcd.print(str); // 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("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("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F"); lcd.setCursor(,); // line 1, pos 0
lcd.print((int)h);
lcd.setCursor(,); // line 1, pos 0
lcd.print((int)t);
lcd.setCursor(,);
lcd.print((int)(Clock.getTemperature()*)); delay();
}
Arduino从DHT11读取温湿度数据并显示在1602LCD的更多相关文章
- Javascript实例技巧精选(6)—滚动鼠标中键读取Json数据分页显示网页内容
>>点击这里下载完整html源码<< 截图如下: 滚动鼠标中键读取Json数据分页显示网页内容,关键的Javascript如下: <script type="t ...
- Django读取Mysql数据并显示在前端
一.首先按添加网页的步骤添加网页,我的网页名为table.html, app名为web table.html放到相应目录下, froms文件提前写好 修改views.py ? 1 2 3 4 5 6 ...
- Qt监控Arduino开关状态(读取串口数据)
setup.ini配置文件内容 [General] #游戏所在主机IP GameIp1=192.168.1.151 GameIp2=192.168.1.152 GameIp3=192.168.1.15 ...
- 使用Arduino Wire Library读取温湿度传感器AM2321
AM2321是采用I2C总线或单总线通讯的国产温湿度传感器.在AM2321手册中,当采用I2C通讯时,手册指定了多处需要主机等待的时间间隔,包括: (1)唤醒传感器时,从机不回复ACK,但主机主要等待 ...
- C#SerialPort如何读取串口数据并显示在TextBox上
SerialPort中串口数据的读取与写入有较大的不同.由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取.一.线程实时读串口:二.事件触发方式实现. 由于线程实时读串口的效率不是十分高 ...
- SerialPort如何读取串口数据并显示在TextBox上,多线程委托
namespace SerialPort { public partial class Form3 : Form { delegate void UpdateTextEventHandler(stri ...
- php分页例子实现读取mysql数据分页显示
以下代码是PHP分页案例,测试通过,主要是PHP+mysql实现分页,代码来处百度空间,有兴趣看的话可以了解一下PHP是如何分页的? <?php $link = mysql_connect(&q ...
- Android 读取后台数据并显示。模拟小区车辆管理系统
帮别人做的演示系统,只具有基本的增删查改功能. 核心是android端和后台通过http传输数据 后台是asp.net,数据库是ms sql 2008 android端 private void ge ...
- ARDUINO MEGA2560 经过ESP8266 WIFI模块上传温湿度数据到 OneNet 服务器
简述 原来写了一个C++的wifi库但是发现用c++ arduino这小身板有点扛不住,代码比较大,使用String类型数据处理速度慢,而且很容易无缘无故跑飞.而且封装成库后使用还需要修改arduin ...
随机推荐
- springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定
springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 标签: springmvc springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需 ...
- GFW实现原理
GFW的工作机制主要包括: 1. IP黑名单 2. 内容审查 3. DNS劫持 参考链接:http://www.doc88.com/p-8435599803718.html
- 百度统计数据的UV和IP为什么不一样?
相信网站站长们在每天查看百度统计数据时会发现网站的IP和UV数据时大时小,有时候IP比UV大,有时候UV比IP大,站长们可能对这些情况感到奇怪.今天就和大家分享一下UV和IP的知识,帮助大家更好地做好 ...
- 第三章 消息摘要算法--MD5
注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第6章“验证数据完整性--消息摘要算法” 3.1.消息摘要算法:防止消息在传递过程中被篡改. 原理:任何消息经过消息摘要算法后 ...
- FIR滤波原理及verilog设计
FIR(Finite Impulse Response)Filter:有限冲激响应滤波器,又称为非递归线性滤波器. FIR滤波器的冲击响应是一个值为滤波器抽头系数的采样序列,其脉冲响应由有限个采样值构 ...
- Unhandled Exception: System.BadImageFormatException: Could not load file or assembly (2008R2配置x64website)
.NET Error Message: Unhandled Exception: System.BadImageFormatException: Could not load file or asse ...
- java.sql.SQLException: ORA-00932: 数据类型不一致: 应为 -, 但却获得 CLOB
总是报:ORA-00932: 数据类型不一致: 应为 -, 但却获得 CLOB 是由于这个a.progressAndPlan字段clob字段. 第一种解决方法: a.progressAndPlan 改 ...
- 使用 CSS 接收用户的点击事情并对相关节点进行操作
问题背景:使用纯 CSS 方案,实现导航栏tab切换 实现 Tab 切换的难点在于如何使用 CSS 接收到用户的点击事情并对相关的节点进行操作.即是: 如何接收点击事件 如何操作相关DOM 下面看看如 ...
- Ubuntu环境变量解析
在Ubuntu中有如下几个文件可以设置环境变量 /etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. /e ...
- Linux:磁盘挂载
本来虚拟centos的服务器的磁盘分配的就不大,之前只分配了20G的样子,由于最近有装了不少软件,比如nifi压缩版就有1.2G的大小,一下子没有磁盘资源了.今晚就折腾在这事上了. [root@mas ...