硬件清单

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. sda, sdb, sdc, sda1, sda2在Linux中都代表什么

    意义如下: 第一个软驱 /dev/fd0. 第二个软驱 /dev/fd1. 第一块硬盘 /dev/sda. 第二块硬盘 /dev/sdb, 以此类推. 第一个SCSI CD-ROM /dev/scd0 ...

  2. 已知(x,y,z,yaw,pitch,roll)如何得到4*4的转换矩阵?

    作者:Nicholas链接:https://www.zhihu.com/question/41514206/answer/104827395来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商 ...

  3. linux服务器上面部署ShowDoc 安装Composer

    1.安装Composer Composer 是 PHP 的一个依赖管理工具,功能上类似于Java 的 Maven,Python 的 pip,Ruby的 gem,Nodejs 的 npm.详细介绍可参考 ...

  4. 【Spark】SparkStreaming-Kafka-集成-终极参考资料

    SparkStreaming-Kafka-集成-终极参考资料 Spark Streaming和Kafka整合开发指南(二) – 过往记忆 Streamingkafka零丢失 | 等英博客 spark- ...

  5. intel 汇编中断解释

    汇编中的10H中断是由BIOS对显示器和屏幕所提供的服务程序.使用int 10h服务程序时,必须先指定ah寄存器为以下显示服务编号之一,以指定需要调用的功用. 显示服务 (Video Service: ...

  6. ERROR in index.web.js from UglifyJs

    使用weexpack构建weex应用时,npm run serve一直报这个错误 ERROR in index.web.js from UglifyJs Unexpected token: name ...

  7. javascript 关键字不能作为变量来使用

    var cfg={export: "export.aspx"} 这句代码中使用了一个关键字“export” 所以在IE8中报错. 那么有哪些关键字不能作为变量呢? 关键字”就是 J ...

  8. 编程之美 1.1 让cpu占用率曲线听你指挥(多核处理器)

    [目录] 不考虑其他进程,cpu画正弦曲线 获取总体cpu利用率 获取多核处理器单个cpu利用率 考虑其他进程,cpu画正弦曲线 下面的程序针对多核处理器,可以设置让任何一个cpu显示相应的曲线(本文 ...

  9. 微信小程序 - 日期(起止)选择器组件

    2019-01-03 : 修复了日期day-1,新增了年月日(除去时分秒),删除了不必要的touchmove 新增: column: ""(年月日) 配置: pickerConfi ...

  10. VMware的存储野心(上):软件定义、分布式DAS支持

    ChinaByte比特网 http://storage.chinabyte.com/291/12477791_2.shtml 11月29日(文/黄亮)- SDN(软件定义的网络,Software De ...