arduino 驱动i2c oled 显示温湿度

时间:2024-03-25 19:50:34

arduino 驱动i2c oled 显示温湿度 

源码
//IIC接口只需要两根线OLED屏的VCC和GND分别连接开发板的3.3V和GND,OLED屏的SDA和SCL分别连接开发板A4和A5
#include"DHT.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> 

#define OLED_RESET   4

DHT wd1(2, DHT11);//设定dht11的一个子类并指定端口为D2,和DHT11模块
Adafruit_SSD1306 display(128, 64, &Wire,OLED_RESET);//设定一个olcd的一个子类并初始化
static const uint8_t PROGMEM wo[] = {
  
0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,
0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00
};//温

static const uint8_t PROGMEM ni[] = {
  
0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E

};//度

static const uint8_t PROGMEM ta[] = 
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00

};//冒号

static const uint8_t PROGMEM shi[] ={
  
0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,0xF8,
0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00

};//湿

void setup() 
{
  

  wd1.begin();
  display.begin(SSD1306_SWITCHCAPVCC,0x3C);//设定为i2c 0.96寸的oled模块
  display.setTextColor(WHITE);//开像素点发光
  display.clearDisplay();//清屏

}


void loop() {

wendu();
  
}


void wendu(void){
  
  
 float h = wd1.readHumidity();
 float t = wd1.readTemperature(); 
    display.clearDisplay();   // 清屏
   display.drawBitmap(8,8,wo,16,16,WHITE);//温
   display.drawBitmap(25,8,ni,16,16,WHITE);//度
   display.drawBitmap(42,8,ta,16,16,WHITE);//冒号
 // display.setTextColor(WHITE);//设置字体颜色
   display.setTextSize(1);//设置字体大小  
   display.setCursor(59, 16);//设置显示位置
    //display.println(i);//显示数字
    display.print(t);//温度
    //Serial.println(t);//调试

  display.drawBitmap(8,25,shi,16,16,WHITE);//湿
   display.drawBitmap(25,25,ni,16,16,WHITE);//度
   display.drawBitmap(42,25,ta,16,16,WHITE);//冒号
  display.setCursor(59, 32);//设置位置
  display.print(h);//湿度
    
    display.display();//显示
     delay(1000);
 
}

需要的库直接在arduino搜索并下载DHT  sensor library 和 Adafruit_SSD1306,只要电路图连接一样代码可以直接复制烧录上电即可用