当前位置: 首页 > news >正文

第三篇、基于Arduino uno,用oled0.96寸屏幕显示dht11温湿度传感器的温度和湿度信息——结果导向

0、结果

说明:先来看看拍摄的显示结果,如果是你想要的,可以接着往下看。

1、外观

说明:本次使用的oled是0.96寸的,别的规格的屏幕不一定适用本教程,一般而言有显示白色、蓝色和蓝黄一起显示的,虽然dht11温湿度模块形态各异,但是代码都是适用的,因为它们的模块都是一样的。

2、连线

说明:只需要连接三根线。

uno————dht11

    5V--------------VCC
GND--------------GND

    D7--------------DATA

说明:只需要连接四根线。

uno————oled 0.96

    5V--------------VCC
GND--------------GND

 SCL--------------SCL

 SDA--------------SDA

3、源程序

说明:采用非阻塞方式编写,一定时间检测和显示一次温湿度数据,并将对应功能进行函数化,方便移植。

/****************************************dht11 part****************************************/
#include <dht11.h>                                      //include library
#define dht11Pin 7                                      //Define DHT11 sensor connection pins
#define dht11TimeInterval 1000                          //Detect the time interval of a tripdht11 DHT11;                                            //Instantiate an object
unsigned long dht11Times = 0;                           //Record the device running time
int dhtTemp = 0, dht11Humi = 0;                         //Storage temperature  //Storage humidity
/****************************************oled96 part****************************************/
#include <Arduino.h>                                                          //include library
#include <U8g2lib.h>                                                          //include library
#include <Wire.h>                                                             //include libraryU8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);#define oledTimeInterval 1000                                                 //Detect the time interval of a trip
unsigned long oledTimes = 0;                                                  // Record the device running time/****************************************set up and loop part*********************************/
void setup() {u8g2.begin();                                                               //Example Initialize the IIC
}
void loop() {getTempData();                                                              //Obtain the temperature and humidity valuesoledDisplayMath();                                                          //Display math
}
/****************************************oled96 part****************************************/
/*Display math*/
void oledDisplayMath() {if (millis() - oledTimes >= oledTimeInterval) {                              //This command is executed once in a whileoledTimes = millis();u8g2.setFont(u8g2_font_ncenB14_tr);                                         //u8g2_font_6x12_tru8g2.setFontDirection(0);u8g2.firstPage();do {u8g2.setCursor(0, 15);                                                    //0 means start at the first column and 15 means end at row 15u8g2.print("Temp:");u8g2.setCursor(64, 15);                                                    //0 means start at the first column and 15 means end at row 15u8g2.print(dhtTemp);u8g2.setCursor(96, 15);                                                    //0 means start at the first column and 15 means end at row 15u8g2.print(" C");u8g2.setCursor(0, 31);                                                    //0 means start at the first column and 15 means end at row 15u8g2.print("Humi: ");u8g2.setCursor(64, 31);u8g2.print(dht11Humi);u8g2.setCursor(96, 31);                                                    //0 means start at the first column and 15 means end at row 15u8g2.print(" %");} while ( u8g2.nextPage() );}
}
/****************************************dht11 part****************************************/
/*Obtain the temperature and humidity values*/
void getTempData() {if (millis() - dht11Times >= dht11TimeInterval) {dht11Times = millis();DHT11.read(dht11Pin);                               //Update all sensor informationdhtTemp = DHT11.temperature;dht11Humi = DHT11.humidity;Serial.print("Temperature: ");Serial.print(dhtTemp);Serial.print(" (C), ");Serial.print("Humidity: ");Serial.print(dht11Humi);Serial.println(" (%).");}
}

4、注意事项

说明:需要在线下载<u8glib.h>库文件和<dht11.h>库文件。

http://www.lryc.cn/news/69126.html

相关文章:

  • 什么是npu算力盒子,算力是越大越好吗?
  • 后端返回文件流时,前端如何处理并成功下载流文件以及解决下载后打开显示不支持此文件格式
  • Ansible的脚本-playbook 剧本
  • python lambda表达式表达式详解及应用
  • Windows 10计算机性能优化:让你的电脑更流畅
  • SpringMVC底层原理源码解析
  • 【CSS系列】第八章 · CSS浮动
  • janus videoroom 对接freeswitch conference 篇1
  • cs109-energy+哈佛大学能源探索项目 Part-2.1(Data Wrangling)
  • __101对称二叉树------进阶:你可以运用递归和迭代两种方法解决这个问题吗?---本题还没用【迭代】去实现
  • 怎么取消只读模式?硬盘进入只读模式怎么办?
  • 如何使用Java生成Web项目验证码
  • 【读书笔记】《亲密关系》
  • 面试季,真的太狠了...
  • 2023年十大最佳黑客工具!
  • 每日练习---C语言
  • 边缘计算如何推动物联网的发展
  • 第五章 栈与队列
  • PyQt5桌面应用开发(16):定制化控件-QPainter绘图
  • spring5源码篇(9)——mybatis-spring整合原理
  • 为什么需要防雷接地,防雷接地的作用是什么
  • 如何应用金字塔模型提高结构化表达能力
  • 2023年系统分析师考前几页纸
  • openwrt-安装NGINX
  • Linux安装MongoDB数据库并内网穿透在外远程访问
  • flutter系列之:使用AnimationController来控制动画效果
  • golang 函数调用栈笔记
  • 云端一体助力体验升级和业务创新
  • 【Linux Network】高级IO
  • Python语言基本控制结构