(一)简介
我们之前学习了OLED屏幕和DHT11温湿度传感器,那么就可以做一个简单的温湿度计项目
通过OLED屏幕可以显示DHT11的温湿度数据
OLED教程:【Arduino uno教程 】(七)0.96寸OLED屏幕
DHT11教程:【Arduino uno教程 】(九)DHT11温湿度传感器

(二)程序
①引入库文件
  1. #include <dht11.h>
  2. #include <U8g2lib.h>
  3. #include <Wire.h>

②定义
定义SCL引脚A5,SDA引脚A4
设置U8G2屏幕库引脚,并且设置屏幕
定义DHT11传感器和引脚
  1. #define BOARD_I2C_SCL   A5
  2. #define BOARD_I2C_SDA   A4   

  3. U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ BOARD_I2C_SCL, /* data=*/ BOARD_I2C_SDA, /* reset=*/ U8X8_PIN_NONE);  

  4. dht11 DHT11;//定义传感器类型
  5. #define DHT11PIN 2//定义传感器连接引脚。


③定义“温湿度”三个字的字模
  1. uint8_t day[][32] =
  2. {
  3. {0x00,0x00,0xC4,0x1F,0x48,0x10,0x48,0x10,0xC1,0x1F,0x42,0x10,0x42,0x10,0xC8,0x1F,
  4. 0x08,0x00,0xE4,0x3F,0x27,0x25,0x24,0x25,0x24,0x25,0x24,0x25,0xF4,0x7F,0x00,0x00},/*"温",0*/

  5. {0x00,0x00,0xE4,0x1F,0x28,0x10,0x28,0x10,0xE1,0x1F,0x22,0x10,0x22,0x10,0xE8,0x1F,
  6. 0x88,0x04,0x84,0x04,0x97,0x24,0xA4,0x14,0xC4,0x0C,0x84,0x04,0xF4,0x7F,0x00,0x00},/*"湿",1*/

  7. {0x80,0x00,0x00,0x01,0xFC,0x7F,0x44,0x04,0x44,0x04,0xFC,0x3F,0x44,0x04,0x44,0x04,
  8. 0xC4,0x07,0x04,0x00,0xF4,0x0F,0x24,0x08,0x42,0x04,0x82,0x03,0x61,0x0C,0x1C,0x70},/*"度",2*/

  9. };


④初始化函数
设置串口通信,波特率位115200
设置U8G2初始化,并且设置字体
  1. void setup()
  2. {  
  3.   Serial.begin(115200);  
  4.   Serial.println("Init u8g2 ....");
  5.   u8g2.begin();
  6.   u8g2.setFont(u8g2_font_ncenB08_tr); //设置字体
  7. }


⑤循环函数
设置temp和humi两个整数型变量
DHT11读取传感器信息
将DHT11传感器的数据保存在变量内
每次更新屏幕时,都清除缓存
U8G2设置中文和变量,并且将屏幕上传缓存区
  1. void loop() {
  2.   int temp = 0;
  3.   int humi = 0;
  4.   DHT11.read(DHT11PIN); //更新传感器所有信息
  5.   
  6.   
  7.   humi = DHT11.humidity;
  8.   temp = DHT11.temperature;
  9.   

  10.   u8g2.clearBuffer();  //清除缓存     

  11.   u8g2.drawXBM(2,2,16,16,day[0]);
  12.   u8g2.drawXBM(18,2,16,16,day[2]);

  13.   u8g2.drawXBM(2,30,16,16,day[1]);
  14.   u8g2.drawXBM(18,30,16,16,day[2]);

  15.   u8g2.setCursor (50, 14);
  16.   u8g2.println(temp);

  17.   u8g2.setCursor (50, 42);
  18.   u8g2.println(humi);
  19.   
  20.   u8g2.sendBuffer();


  21.   Serial.print("当前温度 (℃): ");
  22.   Serial.println(temp);

  23.   Serial.print("当前湿度 (%): ");
  24.   Serial.println(humi);


  25.   delay(2000);


⑥现象展示
9e2da6814e00b72247447a502f11b78.jpg


⑦完整代码
  1. #include <dht11.h>//引入DHT11库
  2. #include <U8g2lib.h>
  3. #include <Wire.h>

  4. #define BOARD_I2C_SCL   A5
  5. #define BOARD_I2C_SDA   A4   

  6. U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ BOARD_I2C_SCL, /* data=*/ BOARD_I2C_SDA, /* reset=*/ U8X8_PIN_NONE);  

  7. /********************###定义###********************/
  8. dht11 DHT11;//定义传感器类型
  9. #define DHT11PIN 2//定义传感器连接引脚。

  10. uint8_t day[][32] =
  11. {
  12. {0x00,0x00,0xC4,0x1F,0x48,0x10,0x48,0x10,0xC1,0x1F,0x42,0x10,0x42,0x10,0xC8,0x1F,
  13. 0x08,0x00,0xE4,0x3F,0x27,0x25,0x24,0x25,0x24,0x25,0x24,0x25,0xF4,0x7F,0x00,0x00},/*"温",0*/

  14. {0x00,0x00,0xE4,0x1F,0x28,0x10,0x28,0x10,0xE1,0x1F,0x22,0x10,0x22,0x10,0xE8,0x1F,
  15. 0x88,0x04,0x84,0x04,0x97,0x24,0xA4,0x14,0xC4,0x0C,0x84,0x04,0xF4,0x7F,0x00,0x00},/*"湿",1*/

  16. {0x80,0x00,0x00,0x01,0xFC,0x7F,0x44,0x04,0x44,0x04,0xFC,0x3F,0x44,0x04,0x44,0x04,
  17. 0xC4,0x07,0x04,0x00,0xF4,0x0F,0x24,0x08,0x42,0x04,0x82,0x03,0x61,0x0C,0x1C,0x70},/*"度",2*/

  18. };


  19. void setup()
  20. {  
  21.   Serial.begin(115200);  
  22.   Serial.println("Init u8g2 ....");
  23.   u8g2.begin();
  24.   u8g2.setFont(u8g2_font_ncenB08_tr); //设置字体
  25. }

  26. void loop() {
  27.   int temp = 0;
  28.   int humi = 0;
  29.   DHT11.read(DHT11PIN); //更新传感器所有信息
  30.   
  31.   
  32.   humi = DHT11.humidity;
  33.   temp = DHT11.temperature;
  34.   

  35.   u8g2.clearBuffer();  //清除缓存     

  36.   u8g2.drawXBM(2,2,16,16,day[0]);
  37.   u8g2.drawXBM(18,2,16,16,day[2]);

  38.   u8g2.drawXBM(2,30,16,16,day[1]);
  39.   u8g2.drawXBM(18,30,16,16,day[2]);

  40.   u8g2.setCursor (50, 14);
  41.   u8g2.println(temp);

  42.   u8g2.setCursor (50, 42);
  43.   u8g2.println(humi);
  44.   
  45.   u8g2.sendBuffer();


  46.   Serial.print("当前温度 (℃): ");
  47.   Serial.println(temp);

  48.   Serial.print("当前湿度 (%): ");
  49.   Serial.println(humi);


  50.   delay(2000);

  51. }