硬件清单

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的更多相关文章

  1. Javascript实例技巧精选(6)—滚动鼠标中键读取Json数据分页显示网页内容

    >>点击这里下载完整html源码<< 截图如下: 滚动鼠标中键读取Json数据分页显示网页内容,关键的Javascript如下: <script type="t ...

  2. Django读取Mysql数据并显示在前端

    一.首先按添加网页的步骤添加网页,我的网页名为table.html, app名为web table.html放到相应目录下, froms文件提前写好 修改views.py ? 1 2 3 4 5 6 ...

  3. Qt监控Arduino开关状态(读取串口数据)

    setup.ini配置文件内容 [General] #游戏所在主机IP GameIp1=192.168.1.151 GameIp2=192.168.1.152 GameIp3=192.168.1.15 ...

  4. 使用Arduino Wire Library读取温湿度传感器AM2321

    AM2321是采用I2C总线或单总线通讯的国产温湿度传感器.在AM2321手册中,当采用I2C通讯时,手册指定了多处需要主机等待的时间间隔,包括: (1)唤醒传感器时,从机不回复ACK,但主机主要等待 ...

  5. C#SerialPort如何读取串口数据并显示在TextBox上

    SerialPort中串口数据的读取与写入有较大的不同.由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取.一.线程实时读串口:二.事件触发方式实现. 由于线程实时读串口的效率不是十分高 ...

  6. SerialPort如何读取串口数据并显示在TextBox上,多线程委托

    namespace SerialPort { public partial class Form3 : Form { delegate void UpdateTextEventHandler(stri ...

  7. php分页例子实现读取mysql数据分页显示

    以下代码是PHP分页案例,测试通过,主要是PHP+mysql实现分页,代码来处百度空间,有兴趣看的话可以了解一下PHP是如何分页的? <?php $link = mysql_connect(&q ...

  8. Android 读取后台数据并显示。模拟小区车辆管理系统

    帮别人做的演示系统,只具有基本的增删查改功能. 核心是android端和后台通过http传输数据 后台是asp.net,数据库是ms sql 2008 android端 private void ge ...

  9. ARDUINO MEGA2560 经过ESP8266 WIFI模块上传温湿度数据到 OneNet 服务器

    简述 原来写了一个C++的wifi库但是发现用c++ arduino这小身板有点扛不住,代码比较大,使用String类型数据处理速度慢,而且很容易无缘无故跑飞.而且封装成库后使用还需要修改arduin ...

随机推荐

  1. jvm垃圾回收原理(转)

    原文链接:jvm垃圾回收原理 在jvm中堆空间划分为三个代:年轻代(Young Generation).年老代(Old Generation)和永久代(Permanent Generation).年轻 ...

  2. FMX.Controls单元 中图形矩阵变换

    procedure TControl.DoMatrixChanged(Sender: TObject);var TranslateMatrix, ScaleMatrix, RotMatrix: TMa ...

  3. Python traceback 模块, 打印异常信息

    Python感觉是模仿Java, 到处都需要加try..catch.... 这里记录一下用法,方便后续使用. # -*- coding:utf-8 -*- import os import loggi ...

  4. mysql主从备份及原理分析

    一.mysql主从备份(复制)的基本原理mysql支持单向.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.mysql复制基于主服务器在二进制日志中跟踪所有对数据库的更 ...

  5. tail -f 然后grep,处理缓存的问题

    学习了:http://www.quwenqing.com/read-134.html 对日志记录做多次grep过滤输出,格式如下: tail -f log | grep xxx | grep yyy ...

  6. JSP中利用JSTL标签对日期格式化

    数据库:Mysql 开发语言:JAVA 页面类型:JSP 对Mysql中的日期类型格式化,在JSP中,如何办呢,很多人说在JAVA后台去日期格式化,无奈了,于是找到了比较靠谱的答案 需要先引入JSTL ...

  7. ASP入门(十八)-访问Access中的数据库

    Northwind 数据库 这是一个微软为软件的使用者提供的一个练习用的数据库,它模拟了一个商贸公司日常业务流程所需要的一些数据. 并且网络上大部分的 SQL 语句的讲解都是以这个数据库为例子讲解的. ...

  8. 程序员需要经纪人吗?10x 最好的程序员其生产力相当于同行的 10 倍~

    原文地址 10x 起源于技术界一个流行的说法,即最好的程序员是超级明星,其生产力相当于同行的 10 倍: Google 园区以好玩的设施闻名:小憩舱.球坑.按摩.干洗.随便吃到饱的自助餐.(为了拍人才 ...

  9. 在CentOS 7上安装Nginx服务器

    下面我就我在CentOS上安装Nginx经验做简单的记录,以备后查. 1.下载nginx-release包 以CentOS 7为例,下载nginx软件包:http://nginx.org/packag ...

  10. 【转】application.properties 常见配置

    Various properties can be specified inside your application.properties/application.yml file or as co ...