【备忘】极简ESP8266+OLED显示(IIC)

91 阅读1分钟

小型开发板没有接显示器的接口,有时需要在运行过程中显示一些信息,就可以使用OLED屏幕。通常OLED屏幕有两种接口:IIC和SPI。IIC接口需要4根线(SDA和SCL),而SPI接口需要6根线(MOSI、MISO、SCLK、CS等)。IIC接口的通信速度较慢,但接线更方便‌。以下是用ESP8266显示文字的方法,显示图像也可以,过稍微复杂点,有需要可以再研究。 ESP8266+OLED(IIC)显示文字。 代码:

#include <Wire.h>               // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306Wire.h"        // legacy: #include "SSD1306.h"

// Initialize the OLED display using Arduino Wire:
SSD1306Wire display(0x3c, SDA, SCL);   // ADDRESS, SDA, SCL  -  SDA and SCL usually populate automatically based on your board's pins_arduino.h e.g. https://github.com/esp8266/Arduino/blob/master/variants/nodemcu/pins_arduino.h

void setup() {
  Serial.begin(115200);
  // Initialising the UI will init the display too.
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_24);
  display.setTextAlignment(TEXT_ALIGN_LEFT);

}

void loop() {
  display.clear();
  display.drawString(0, 0, String(millis()));
  display.drawString(0, 26, "Hello world");
  display.display();
  delay(1000);
}

接线图: image.png

运行结果:

e04e32c2923984e8d1e690797234b63.png