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>库文件。