第三篇、基于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 trip

dht11 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 library

U8G2_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 values
  oledDisplayMath();                                                          //Display math
}
/****************************************oled96 part****************************************/
/*Display math*/
void oledDisplayMath() {
  if (millis() - oledTimes >= oledTimeInterval) {                              //This command is executed once in a while
    oledTimes = millis();

    u8g2.setFont(u8g2_font_ncenB14_tr);                                         //u8g2_font_6x12_tr
    u8g2.setFontDirection(0);
    u8g2.firstPage();
    do {
      u8g2.setCursor(0, 15);                                                    //0 means start at the first column and 15 means end at row 15
      u8g2.print("Temp:");
      u8g2.setCursor(64, 15);                                                    //0 means start at the first column and 15 means end at row 15
      u8g2.print(dhtTemp);
      u8g2.setCursor(96, 15);                                                    //0 means start at the first column and 15 means end at row 15
      u8g2.print(" C");
      u8g2.setCursor(0, 31);                                                    //0 means start at the first column and 15 means end at row 15
      u8g2.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 15
      u8g2.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 information
    dhtTemp = 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>库文件。

  • 8
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值