菜鸟之旅:ESP 8266开发板 + 1.8寸OLED液晶屏(驱动器IC:ST 7735)

测试平台:Arduino IDE 1.8.9

硬件:esp8266 (ESP-12E) , 1.8寸 OLED (驱动器IC ST7735)

开发环境及配置

安装arduino,在arduino中获取esp8266的支持,首选项加入如下:

开发环境及配置

安装arduino,在arduino中获取esp8266的支持,首选项加入如下:

打开软件菜单:文件--首选项,附加开发版网址,复制如下

http://arduino.esp8266.com/stable/package_esp8266com_index.json

打开开发板管理器,搜索esp8266并安装

安装后,在开发板里选择NodeMCU1.0(ESP-12E Module)了。

打开开发板管理器,搜索并安装st7735,Adafruit ST7735 and ST7739 Library

选择示例如下图


#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735

#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>

#if defined(ARDUINO_FEATHER_ESP32) // Feather Huzzah32
  #define TFT_CS         14
  #define TFT_RST        15
  #define TFT_DC         32

#elif defined(ESP8266)
  #define TFT_CS         4
  #define TFT_RST        16                                            
  #define TFT_DC         5

#else
  // For the breakout board, you can use any 2 or 3 pins.
  // These pins will also work for the 1.8" TFT shield.
  #define TFT_CS        10
  #define TFT_RST        9 // Or set to -1 and connect to Arduino RESET pin
  #define TFT_DC         8
#endif

// OPTION 1 (recommended) is to use the HARDWARE SPI pins, which are unique
// to each board and not reassignable. For Arduino Uno: MOSI = pin 11 and
// SCLK = pin 13. This is the fastest mode of operation and is required if
// using the breakout board's microSD card.

// For 1.44" and 1.8" TFT with ST7735 use:
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

// For 1.14", 1.3", 1.54", 1.69", and 2.0" TFT with ST7789:
//Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);


// OPTION 2 lets you interface the display using ANY TWO or THREE PINS,
// tradeoff being that performance is not as fast as hardware SPI above.
//#define TFT_MOSI 11  // Data out
//#define TFT_SCLK 13  // Clock out

// For ST7735-based displays, we will use this call
//Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);

// OR for the ST7789-based displays, we will use this call
//Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);

以上代码,保留红色字符,其它删除

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735

#define TFT_CS    4    //  D2   (液晶屏片选信号)
#define TFT_RST   16   //  D0  (重启接口)                                        
#define TFT_DC    5    //  D1   (寄存器、数据选择信号)

#define TFT_SDA 13    //   D7  Data outV  (SDA 数据线)
#define TFT_SCL 14    //  D5   Clock out  (SCL 时钟线)

// For ST7735-based displays, we will use this call
// initialize the LCD
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_SDA, TFT_SCL, TFT_RST);


void setup(void) {
  Serial.begin(9600);

  tft.initR(INITR_BLACKTAB);      // Init ST7735S chip, black tab

  tft.fillScreen(ST77XX_BLACK);

  tft.setTextColor(color);
  tft.setTextSize(2);
  tft.println("Hello World!");

void loop() {
  tft.invertDisplay(true);
  delay(500);
  tft.invertDisplay(false);
  delay(500);
}

硬件连接

先通电:

oled 的VDD 对应 esp 8266 的  3.3V  //电源正

oled 的GND  对应 esp 8266 的 GND  //接地

再接线:

#define TFT_CS    4    //  D2   (液晶屏片选信号)
#define TFT_RST   16   //  D0  (重启接口)                                        
#define TFT_DC    5    //  D1   (寄存器、数据选择信号)

#define TFT_SDA 13    //   D7  Data outV  (SDA 数据线)
#define TFT_SCL 14    //  D5   Clock out  (SCL 时钟线)

oled 的 RES  对应 esp 8266的  D0 (gpio 16) // LCD复位接口

oled 的 CS   对应 esp 8266的D2 (gpio 4) /  LCD片选控制

oled 的 DC    对应 esp 8266的D1 (gpio 5) //SPI数据/命令选择

oled 的 SDA  对应 esp 8266的  D7 (gpio 13) //通信数据线(MOSI)

oled 的 SCL  对应 esp 8266的   D5 (gpio 14) //(SCL 时钟线)

//oled 的 BLK对应 esp 8266的   D6 (gpio 16) // LED背光控制,低电平关闭(SCLK)

最终效果

  • 33
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
首先,要确保你已经正确安装了U8x8库和ESP8266开发板的支持库。 接下来,我们需要连接OLED屏幕到ESP8266开发板上。4针OLED屏需要接线如下: OLED VCC -> ESP8266 3.3V OLED GND -> ESP8266 GND OLED SCL -> ESP8266 D1 (GPIO5) OLED SDA -> ESP8266 D2 (GPIO4) 然后,打开Arduino IDE,创建一个新的Sketch。在Sketch中,我们需要引入U8x8库并声明一个U8x8对象: ```C++ #include <U8x8lib.h> U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ D1, /* data=*/ D2, /* reset=*/ U8X8_PIN_NONE); ``` 在setup()函数中,我们需要初始化U8x8对象和OLED屏幕: ```C++ u8x8.begin(); u8x8.setFlipMode(1); u8x8.setFont(u8x8_font_chroma48medium8_r); ``` 在loop()函数中,我们可以编写代码来切换不同的显示页面。例如,我们可以使用u8x8.draw2x2String()函数在OLED屏幕上显示文本: ```C++ u8x8.clear(); u8x8.draw2x2String(0, 0, "Page 1"); u8x8.refreshDisplay(); delay(2000); u8x8.clear(); u8x8.draw2x2String(0, 0, "Page 2"); u8x8.refreshDisplay(); delay(2000); u8x8.clear(); u8x8.draw2x2String(0, 0, "Page 3"); u8x8.refreshDisplay(); delay(2000); ``` 完整的代码示例: ```C++ #include <U8x8lib.h> U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ D1, /* data=*/ D2, /* reset=*/ U8X8_PIN_NONE); void setup() { u8x8.begin(); u8x8.setFlipMode(1); u8x8.setFont(u8x8_font_chroma48medium8_r); } void loop() { u8x8.clear(); u8x8.draw2x2String(0, 0, "Page 1"); u8x8.refreshDisplay(); delay(2000); u8x8.clear(); u8x8.draw2x2String(0, 0, "Page 2"); u8x8.refreshDisplay(); delay(2000); u8x8.clear(); u8x8.draw2x2String(0, 0, "Page 3"); u8x8.refreshDisplay(); delay(2000); } ``` 上传代码到ESP8266开发板,即可在OLED屏幕上看到不同的显示页面。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值