我们之前学习了OLED屏幕和DHT11温湿度传感器,那么就可以做一个简单的温湿度计项目
通过OLED屏幕可以显示DHT11的温湿度数据
OLED教程:【Arduino uno教程 】(七)0.96寸OLED屏幕
DHT11教程:【Arduino uno教程 】(九)DHT11温湿度传感器
(二)程序
①引入库文件
- #include <dht11.h>
- #include <U8g2lib.h>
- #include <Wire.h>
②定义
定义SCL引脚A5,SDA引脚A4
设置U8G2屏幕库引脚,并且设置屏幕
定义DHT11传感器和引脚
- #define BOARD_I2C_SCL A5
- #define BOARD_I2C_SDA A4
- U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ BOARD_I2C_SCL, /* data=*/ BOARD_I2C_SDA, /* reset=*/ U8X8_PIN_NONE);
- dht11 DHT11;//定义传感器类型
- #define DHT11PIN 2//定义传感器连接引脚。
③定义“温湿度”三个字的字模
- uint8_t day[][32] =
- {
- {0x00,0x00,0xC4,0x1F,0x48,0x10,0x48,0x10,0xC1,0x1F,0x42,0x10,0x42,0x10,0xC8,0x1F,
- 0x08,0x00,0xE4,0x3F,0x27,0x25,0x24,0x25,0x24,0x25,0x24,0x25,0xF4,0x7F,0x00,0x00},/*"温",0*/
- {0x00,0x00,0xE4,0x1F,0x28,0x10,0x28,0x10,0xE1,0x1F,0x22,0x10,0x22,0x10,0xE8,0x1F,
- 0x88,0x04,0x84,0x04,0x97,0x24,0xA4,0x14,0xC4,0x0C,0x84,0x04,0xF4,0x7F,0x00,0x00},/*"湿",1*/
- {0x80,0x00,0x00,0x01,0xFC,0x7F,0x44,0x04,0x44,0x04,0xFC,0x3F,0x44,0x04,0x44,0x04,
- 0xC4,0x07,0x04,0x00,0xF4,0x0F,0x24,0x08,0x42,0x04,0x82,0x03,0x61,0x0C,0x1C,0x70},/*"度",2*/
- };
④初始化函数
设置串口通信,波特率位115200
设置U8G2初始化,并且设置字体
- void setup()
- {
- Serial.begin(115200);
- Serial.println("Init u8g2 ....");
- u8g2.begin();
- u8g2.setFont(u8g2_font_ncenB08_tr); //设置字体
- }
⑤循环函数
设置temp和humi两个整数型变量
DHT11读取传感器信息
将DHT11传感器的数据保存在变量内
每次更新屏幕时,都清除缓存
U8G2设置中文和变量,并且将屏幕上传缓存区
- void loop() {
- int temp = 0;
- int humi = 0;
- DHT11.read(DHT11PIN); //更新传感器所有信息
-
-
- humi = DHT11.humidity;
- temp = DHT11.temperature;
-
- u8g2.clearBuffer(); //清除缓存
- u8g2.drawXBM(2,2,16,16,day[0]);
- u8g2.drawXBM(18,2,16,16,day[2]);
- u8g2.drawXBM(2,30,16,16,day[1]);
- u8g2.drawXBM(18,30,16,16,day[2]);
- u8g2.setCursor (50, 14);
- u8g2.println(temp);
- u8g2.setCursor (50, 42);
- u8g2.println(humi);
-
- u8g2.sendBuffer();
- Serial.print("当前温度 (℃): ");
- Serial.println(temp);
- Serial.print("当前湿度 (%): ");
- Serial.println(humi);
- delay(2000);
⑥现象展示
⑦完整代码
- #include <dht11.h>//引入DHT11库
- #include <U8g2lib.h>
- #include <Wire.h>
- #define BOARD_I2C_SCL A5
- #define BOARD_I2C_SDA A4
- U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ BOARD_I2C_SCL, /* data=*/ BOARD_I2C_SDA, /* reset=*/ U8X8_PIN_NONE);
- /********************###定义###********************/
- dht11 DHT11;//定义传感器类型
- #define DHT11PIN 2//定义传感器连接引脚。
- uint8_t day[][32] =
- {
- {0x00,0x00,0xC4,0x1F,0x48,0x10,0x48,0x10,0xC1,0x1F,0x42,0x10,0x42,0x10,0xC8,0x1F,
- 0x08,0x00,0xE4,0x3F,0x27,0x25,0x24,0x25,0x24,0x25,0x24,0x25,0xF4,0x7F,0x00,0x00},/*"温",0*/
- {0x00,0x00,0xE4,0x1F,0x28,0x10,0x28,0x10,0xE1,0x1F,0x22,0x10,0x22,0x10,0xE8,0x1F,
- 0x88,0x04,0x84,0x04,0x97,0x24,0xA4,0x14,0xC4,0x0C,0x84,0x04,0xF4,0x7F,0x00,0x00},/*"湿",1*/
- {0x80,0x00,0x00,0x01,0xFC,0x7F,0x44,0x04,0x44,0x04,0xFC,0x3F,0x44,0x04,0x44,0x04,
- 0xC4,0x07,0x04,0x00,0xF4,0x0F,0x24,0x08,0x42,0x04,0x82,0x03,0x61,0x0C,0x1C,0x70},/*"度",2*/
- };
- void setup()
- {
- Serial.begin(115200);
- Serial.println("Init u8g2 ....");
- u8g2.begin();
- u8g2.setFont(u8g2_font_ncenB08_tr); //设置字体
- }
- void loop() {
- int temp = 0;
- int humi = 0;
- DHT11.read(DHT11PIN); //更新传感器所有信息
-
-
- humi = DHT11.humidity;
- temp = DHT11.temperature;
-
- u8g2.clearBuffer(); //清除缓存
- u8g2.drawXBM(2,2,16,16,day[0]);
- u8g2.drawXBM(18,2,16,16,day[2]);
- u8g2.drawXBM(2,30,16,16,day[1]);
- u8g2.drawXBM(18,30,16,16,day[2]);
- u8g2.setCursor (50, 14);
- u8g2.println(temp);
- u8g2.setCursor (50, 42);
- u8g2.println(humi);
-
- u8g2.sendBuffer();
- Serial.print("当前温度 (℃): ");
- Serial.println(temp);
- Serial.print("当前湿度 (%): ");
- Serial.println(humi);
- delay(2000);
- }