硬件清单

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. unix时间戳time_t与UTC时区的关系

    一般我用C写unix时间戳是这样子的 #include<stdio.h> #include<time.h> void printfDateTimeStr(struct tm * ...

  2. fortran中提取字符串中可见字符的索引

    fortran中常常需要提取字符串中可见字符的索引,下面是个小例子: !============================================================= su ...

  3. JAVA动态编译(JavaCompiler)

    一.简介 在java中javax报下提供了JavaCompiler类,此类可以允许开发人员编译java文件为class文件. 下面示例中是利用JavaCompiler编译文件,并利用URLClassL ...

  4. walmart weekly sales

    最近参加了kaggle的walmart weekly sales 预测比赛,已经过期但还能提交获得评分.Walmart Recruiting - Store Sales Forecasting 提供的 ...

  5. Linq-Contains查询

    customers.Where(c => c.Name.Contains("john"));

  6. 如何获取当前应用程序所用的OpenGL ES的版本

      如何获取当前应用程序所用的OpenGL ES的版本? [答案]     char* glVersion = (char*)glGetString(GL_VERSION);     LOGW(&qu ...

  7. [Canvas]碰撞球

    观赏动态效果请点此下载并用Chrome/Firefox打开index.html 图例: 代码: <!DOCTYPE html> <html lang="utf-8" ...

  8. 转载:Unicode和Utf-8有何区别 转载自知乎 原文作者不详

    作者:于洋链接:https://www.zhihu.com/question/23374078/answer/69732605来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  9. Rainmeter 一部分 语法 中文教程

    ;Meter基本元素示例:;如果是[MeterStyle]表示Meter的公共Style,类似CSS的意义!!!;颜色可以使用网页的颜色定义方式,如半透明黄色:255.255.0.128=FFFF00 ...

  10. HDU1069(还是dp基础)

    今天不想说太多废话-由于等下要写自己主动提交机. 不知道能不能成功呢? 题目的意思就是,一个猴子,在叠砖头 ...以下的要严格大于上面的.求叠起来最高能到多少- n非常少,n^2算法毫无压力-话说dp ...