四针角oled屏连接arduino_ESP8266连接OLED显示屏并显示位图图像

在本篇文章中,我们将OLED显示屏与NodeMCU ESP8266连接。 NodeMCU是一个开源的物联网平台,包括运行在Espressif Systems公司的低成本Wi-Fi ESP8266 SoC上的固件。它具有用于连接其他外设的GPIO引脚,并支持使用SPI、I2C和UART引脚进行串行通信。它还具有ADC和PWM引脚。

在本篇文章中,我们将使用SPI协议将单色7引脚SSD1306 0.96 OLED显示屏与NodeMCU连接,并将学习使用NodeMCU ESP8266在OLED屏上显示图像。

OLED显示屏

有机发光二极管(Organic Light Emitting Diode,OLED)是一种发光二极管,其中由有机化合物制成的发光层在供应电流时发光。该层放置在两个电极之间。该技术用于计算机、电视、智能手机等显示屏。OLED显示器具有自己的光,不需要像LCD那样的任何背光,因此它们是节能的并且与许多微控制器一起使用。在LCD上使用OLED显示器的另一个优点是在OLED上显示大量且更好的图形质量。

市场上有各种各样的OLED显示器。这些显示器的特征在于颜色、引脚数、控制器IC和屏幕尺寸。在基础颜色上,OLED有单色蓝色,单色白色和黄色/蓝色可供选择。通信方式上,主要有两种类型的OLED  -  3pin和7pin。 3引脚OLED可用于I2C通信模式,7引脚OLED可用于SPI模式或I2C模式。

在本篇文章中,我们将使用“单色7引脚SSD1306 0.96”OLED显示屏,其宽128像素,长64像素。该显示器可以在SPI和I2C通信协议上工作。我们将在本文中使用SPI协议。此OLED上使用SSD1306 IC,有助于在屏幕上显示像素。

需要的组件

●    单色7针SSD1306 0.96“OLED显示屏

●    NodeMCU ESP8266

●    Micro USB线

●    面包板

NodeMCU和OLED显示器之间的SPI引脚连接

以下是用于连接7引脚OLED显示器和NodeMCU以使用SPI串行通信协议进行通信的电路图。

Circuit-Diagram-for-Interfacing-OLED-Display-with-NodeMCU-ESP8266.png (42.21 KB, 下载次数: 23)

2019-5-28 17:50 上传

下表显示了OLED Display和NodeMCU ESP8266之间的连接。 GND引脚连接到NodeMCU GND,VDD引脚可以连接到3.3V或5V,SCK是OLED显示器上的时钟引脚,它连接到NodeMCU的D5用于SPI时钟。 SPI接口OLED上的MOSI引脚SDA引脚转到NodeMCU的D7。 RESET引脚转到D3。 DC,数据命令引脚连接到NodeMCU的D2。最后一个引脚是CS进入D8,芯片选择NodeMCU。

编号OLED显示屏NodeMCU

1GNDGND

2VDD3.3V

3SCKD5

4MOSI(SPI)或SDA(I2C)D7

5RESETD3

6DCD2

7CSD8

本文中,我们将使用“Adafruit _SSD1306.h”和“Adafruit_GFX.h”库来连接OLED和NodeMCU。打开Arduino IDE并从Arduino IDE安装最新版本(Sketch> Include Library> Manage Libraries或Ctrl + Shift_I)。

Adafruit-Library-for-Interfacing-OLED-with-NodeMCU.png (32.31 KB, 下载次数: 19)

2019-5-28 17:53 上传

由于OLED显示器的像素大小为128x64,因此我们必须对Adafruit_SSD1306的头文件进行更改。打开Arduino库,转到Adafruit_SSD1306并打开其头文件(Adafruit _SSD1306.h)。注释掉“#define SSD1306_128_32”行,取消注释“#define SSD1306_128_64”行,如下图所示,然后保存文件。默认情况下,此库附带“#define SSD1306_128_32”。

Code-for-Interfacing-OLED-with-NodeMCU.png (52.71 KB, 下载次数: 21)

2019-5-28 17:54 上传

最后根据显示的表格更改Adafruit SSD1306示例中“ssd1306_128x64_spi”的引脚编号。现在,在将OLED显示器与NodeMCU正确连接后运行草图时,您将在OLED显示屏上看到Adafruit的徽标,默认情况下保存在库中。在Adafruit徽标之后,它会显示许多其他图形,如直线、矩形、三角形、圆形、字符串、数字、动画和位图。在本文中,我们将学习如何使用NodeMCU ESP8266在OLED上显示任何图像。

Testing-OLED-Display-with-NodeMCU-ESP8266.jpg (54.16 KB, 下载次数: 16)

2019-5-28 17:58 上传

编程NodeMCU用于连接OLED

本文末尾处提供了完整的代码,这里我们已经详细解释了代码。

通过导入必要的库来启动代码。由于我们使用SPI协议,因此我们将导入“SPI.h”库并导入OLED显示屏的“Adafruit_GFX.h”和“Adafruit_SSD1306.h”。

#include

#include

#include 复制代码

我们的OLED尺寸为128x64,因此我们将屏幕宽度和高度分别设置为128和64。 因此,定义连接到NodeMCU以进行SPI通信的OLED引脚的变量。

#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using software SPI (default case):

#define OLED_MOSI   D7

#define OLED_CLK   D5

#define OLED_DC    D2

#define OLED_CS    D8

#define OLED_RESET D3

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,

OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);复制代码

通过使用SSD1306_SWITCHCAPVCC在内部生成3.3V以初始化显示器来初始化OLED显示器。

if(!display.begin(SSD1306_SWITCHCAPVCC))

{

Serial.println(F("SSD1306 allocation failed"));

for(;;); // Don't proceed, loop forever

}复制代码

在显示任何内容之前,通过调用函数display.clearDisplay()清除OLED屏幕的显示。 我们通过调用函数setTextSize(font-size)将字体大小设置为2,并使用setTextColor和setCursor函数设置文本颜色和光标位置。 Display.display()命令用于将数据传输到SSD1306控制器的内部存储器。 传输后,像素显示在屏幕上。 现在我们可以通过调用display.startscrollright(x-pos,y-pos)和display.startscrollleft(x-pos,y-pos)以延迟函数给出的时间以各种方式开始滚动文本。 可以使用display.stopscroll()函数停止滚动文本。

void testscrolltext(void) {

display.clearDisplay(); // clear the display screen of the OLED

display.setTextSize(2); // Draw 2X-scale text

display.setTextColor(WHITE);

display.setCursor(0, 0);

display.println(F("CIRCUIT"));

display.println(F("DIGEST"));

display.display();      // Show initial text

delay(100);

// Scroll in various directions, pausing in-between:

display.startscrollright(0x00, 0x0F);

delay(2000);

display.stopscroll();

delay(1000);

display.startscrollleft(0x00, 0x0F);

delay(2000);

display.stopscroll();

delay(1000);

display.startscrolldiagright(0x00, 0x07);

delay(2000);

display.startscrolldiagleft(0x00, 0x07);

delay(2000);

display.stopscroll();

delay(1000);

}复制代码

我们调用display.drawBitmap()函数,该函数采用6个参数(x坐标,y坐标,位图数组,宽度,高度和颜色)在OLED上绘制图像。 由于我们的显示尺寸为128x64,因此我们将宽度和高度分别设置为128和64。 这里位图数组包含用于在屏幕上绘制像素以创建图像的像素信息。 该位图数组可以在线生成,本文下面将对此进行说明,或者有许多软件可用于将图像转换为位图数组。

const unsigned char myBitmap [] PROGMEM = {

0xff, 0xff, 0xff, 0xe0, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xff, 0xc0, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xf7, 0xc0, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xc7, 0x80, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0x0f, 0x01, 0xc0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xfe, 0x0f, 0x03, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xf8, 0x1e, 0x03, 0x3f, 0xf8, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xf0, 0x3e, 0x03, 0x3f, 0xfc, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xe0, 0x3c, 0x03, 0x7f, 0xfe, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xc0, 0x7c, 0x03, 0xf0, 0x3f, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0x80, 0x78, 0x00, 0xc0, 0x0f, 0xc1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0x00, 0xf8, 0x00, 0x00, 0x07, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xfe, 0x01, 0xf0, 0x00, 0x00, 0x03, 0xf8, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xfc, 0x01, 0xf0, 0x00, 0x00, 0x00, 0xfc, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xfc, 0x03, 0xe0, 0x00, 0x0f, 0x00, 0x7e, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xf8, 0x07, 0xc0, 0x3f, 0xff, 0x80, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xc0, 0x7f, 0xf9, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0x80, 0xff, 0xf9, 0x80, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0x80, 0xff, 0xff, 0x80, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0x01, 0xf0, 0x1f, 0x80, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xc0, 0x00, 0x03, 0xe0, 0x06, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xc0, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xc0, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x80, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x80, 0x00, 0x0f, 0x80, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x80, 0x00, 0x0f, 0x00, 0x80, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x80, 0x00, 0x1e, 0x01, 0xe0, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x00, 0x00, 0x1e, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x00, 0x00, 0x3c, 0x03, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x00, 0x00, 0x7c, 0x03, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xf8, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xf8, 0x01, 0xe0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x80, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x80, 0x00, 0x03, 0xb0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x80, 0x00, 0x03, 0x18, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0x80, 0x00, 0x03, 0xbc, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xc0, 0x00, 0x01, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xc0, 0x00, 0x00, 0xff, 0xff, 0x80, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xf0, 0x00, 0x1f, 0xff, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xfe, 0x00, 0x07, 0xfc, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0x80, 0x03, 0xf0, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xe0, 0x01, 0xc0, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xf8, 0x03, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xf8, 0x00, 0xff, 0xe0, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xfc, 0x00, 0x3f, 0xe0, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xfe, 0x00, 0x0e, 0x30, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0x00, 0x07, 0x70, 0x00, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0x80, 0x03, 0xe0, 0x1b, 0xfc, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xc0, 0x01, 0xc0, 0x7f, 0xf0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xe0, 0x00, 0x00, 0x7f, 0xc0, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xf0, 0x00, 0x00, 0x67, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xf8, 0x00, 0x00, 0x66, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xfe, 0x00, 0x00, 0x7e, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0x00, 0x00, 0x3c, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xc0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xf0, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xfe, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff

};

display.drawBitmap(35, 0,  myBitmap, 128, 64, BLACK, WHITE);

display.display();复制代码

将图像转换为位图值

可以从http://javl.github.io/image2cpp/生成在线位图。 上传要在OLED上显示的图像文件,并将大小设置为128x64。 将显示预览图像,然后将生成位图数组。

下面的屏幕截图显示了生成任何图像的位图值的过程。

Convert-Image-into-Bitmap-Values.png (26.11 KB, 下载次数: 19)

2019-5-28 18:01 上传

Selecting-Image-for-Converting-into-Bitmap-Values.png (52.09 KB, 下载次数: 15)

2019-5-28 18:01 上传

最后将完整的代码上传到NodeMCU ESP8266,您将在OLED屏幕上看到图像显示。

Interfacing-OLED-Display-with-NodeMCU-ESP8266.jpg (102.1 KB, 下载次数: 21)

2019-5-28 17:58 上传

代码

本文使用的完整代码如下:

main.rar

(1.76 KB, 下载次数: 45)

2019-5-28 18:02 上传

点击文件名下载附件

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STM32是一款由意法半导体(STMicroelectronics)公司推出的32位单片机系列产品。它具有高性能、低功耗和丰富的外设接口,广泛应用于各种嵌入式系统中。在引用\[1\]中的代码中,展示了如何初始化和控制STM32的GPIO口,以控制LED的亮灭。 而四针OLED幕是一种小尺寸的显示屏,通常由OLED显示技术驱动。在引用\[2\]和\[3\]中的代码中,展示了如何使用STM32控制OLED幕进行显示。通过调用相应的函数,可以在OLED幕上显示数字、字符和汉字等内容。 综上所述,STM32和四针OLED幕可以通过STM32的GPIO口进行连接和控制,从而实现对OLED幕的显示操作。 #### 引用[.reference_title] - *1* *2* [基于stm32的四针OLED显示](https://blog.csdn.net/qq_51454236/article/details/125837037)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [stm32使用模拟IIC控制四针0.96寸OLED](https://blog.csdn.net/qq_48453845/article/details/131115969)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值