1.效果展示:
利用esp8266驱动1.3寸OLED显示屏显示出ds18b20模块所采集的当前实测温度数据,效果如下图:
需要添加一个10k的上拉电阻。
接线顺序:
ESP8266 | OLED 屏幕 | DS18B20 |
---|
GND | GND | GND |
VCC | VCC | VCC |
IO5 | SCL | / |
IO4 | SDA | / |
IO14 | / | DQ |
3.代码展示:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 5, /* data=*/ 4, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
// 数据输出脚接开发板数字引脚2
#define ONE_WIRE_BUS 14
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
const unsigned char touzimu[][32] U8X8_PROGMEM = {
{ 0x40, 0x00, 0x80, 0x00, 0xFE, 0x7F, 0x02, 0x40, 0x11, 0x21, 0x20, 0x01, 0x20, 0x01, 0x08, 0x01,
0x10, 0x01, 0x10, 0x01, 0xFF, 0x7F, 0x80, 0x02, 0x40, 0x04, 0x20, 0x08, 0x18, 0x10, 0x06, 0x20/*"实",0*/
},
{ 0x00, 0x10, 0x00, 0x10, 0x3E, 0x10, 0x22, 0x10, 0xA2, 0x7F, 0x22, 0x10, 0x22, 0x10, 0x3E, 0x10,
0x22, 0x11, 0x22, 0x12, 0x22, 0x12, 0x22, 0x10, 0x3E, 0x10, 0x22, 0x10, 0x00, 0x14, 0x00, 0x08/*"时",1*/
},
{ 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/*"温",2*/
},
{ 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/*"湿",3*/
},
{ 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/*"度",4*/
},
{ 0x00, 0x00, 0xF8, 0x0F, 0x08, 0x08, 0x08, 0x08, 0xF8, 0x0F, 0x08, 0x08, 0x08, 0x08, 0xF8, 0x0F,
0x20, 0x02, 0x22, 0x22, 0x24, 0x22, 0x28, 0x12, 0x28, 0x0A, 0x20, 0x02, 0xFF, 0x7F, 0x00, 0x00/*"显",5*/
},
{ 0x00, 0x00, 0xFC, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x7F, 0x80, 0x00,
0x80, 0x00, 0x88, 0x08, 0x88, 0x10, 0x84, 0x20, 0x82, 0x40, 0x81, 0x40, 0xA0, 0x00, 0x40, 0x00/*"示",6*/
},
{ 0x06, 0x00, 0x89, 0x2F, 0x69, 0x30, 0x36, 0x20, 0x10, 0x20, 0x18, 0x00, 0x18, 0x00, 0x18, 0x00,
0x18, 0x00, 0x18, 0x00, 0x18, 0x00, 0x10, 0x00, 0x30, 0x20, 0x60, 0x10, 0x80, 0x0F, 0x00, 0x00, /*"℃",7*/
},
};
void setup(void)
{ u8g2.begin();//u8g2初始化
Serial.begin(9600);
sensors.begin();
}
void loop(void)
{
u8g2.clearBuffer();
//float T = sensors.requestTemperatures(); // 发送命令获取温度
u8g2.drawXBM(16, 0, 16, 16, touzimu[0]);
u8g2.drawXBM(32, 0, 16, 16, touzimu[1]);
u8g2.drawXBM(48, 0, 16, 16, touzimu[2]);
u8g2.drawXBM(64, 0, 16, 16, touzimu[4]);
u8g2.drawXBM(80, 0, 16, 16, touzimu[5]);
u8g2.drawXBM(96, 0, 16, 16, touzimu[6]);
// u8g2.drawXBM(104, 0, 16, 16, touzimu[6]);
u8g2.setFontMode(4);
u8g2.drawXBM(16, 30, 16, 16, touzimu[2]);
u8g2.drawXBM(32, 30, 16, 16, touzimu[4]);
u8g2.drawXBM(90, 30, 16, 16, touzimu[7]);
u8g2.setFont(u8g2_font_cu12_tr);
u8g2.setCursor(48, 45);
u8g2.print(":");
u8g2.setFont(u8g2_font_cu12_tr);
u8g2.setCursor(54,45);
u8g2.print(sensors.getTempCByIndex(0));
u8g2.sendBuffer();
sensors.requestTemperatures(); // 发送命令获取温度
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
delay(500);
}