ESP8266 在 OLED 屏上绘制图形

写在前面

在购买 ESP8266 开发板时,选择了带 0.96 OLED 屏的套餐,于是研究了一下怎么在屏幕上显示图像,自己用画图写了一个 Hello! ,将它显示到了 OLED 屏幕上。将信息显示到屏幕上是一种比较方便的交互方式,可以直观的查看设备运行状况。

概述

这篇文章主要讲述了用 Arduino 编写代码,将自己绘制的图片显示到 ESP8266 连接的 OLED 屏幕上。要点包括:

  • ESP8266 开发板的引脚分布:讲解如何连接 4 针的 OLED 屏,接线方式
  • Arduino 库管理的使用:下载 Arduino 的 ACROBOTIC SSD 1306 库
  • 使用第三方库中的示例:修改其中的 DrawLogo 示例,替换成自己的图片
  • 将位图(png)转换成字节数组。

技术要点

我们会使用 ACROBOTIC 提供 SSD1306 的库和 ESP8266 SDK 中提供的 Wire 库,使用 I2C 协议与 OLED 通信,具体可以查看文末的参考资料链接。

ESP8266 连接 OLED

在 OLED 这边有 4 个针脚,分别是 GND(GND)、VCC(3.3V供电)、SCL(串行时钟线)、SDA(双向串行数据线),分别与 ESP8266 开发板的 G、3V、D1、D2 连接,即 GND -> GVCC -> 3VSCL -> D1SDA -> D2

原理:我们使用 ESP8266 的 3V 和 G 用于给 OLED 供电,还需要调用 Wire 库来与 OLED 通信,在 Arduino 的 Wire 库中为 I2C 协议设置的默认 GPIO, GPIO5 为 SCL,GPIO4 为 SDA,对应查看 ESP8266 的针脚配置, GPIO5 为 D1,GPIO4 为 D2。

操作流程

绘制图形

使用 Windows 自带的画图绘制一个 128x64 像素大小的图像,也可以使用其他工具,如 ps 。

保存为 bmp 位图文件。

将图片转成字节数组

Image2Lcd 下载地址: http://source.qiniu.wqf31415.xyz/Image2Lcd.zip
Mac 中可以使用 LCD Creater:http://csmithsoftware.com/LCD_Creator/LCD_Creator.dmg

使用网上下载的 Image2Lcd 工具将位图转成字节数组,点击 打开 选择刚才保存的 bmp 文件,按如图所示设置参数:

输出数据类型:C语言数组(*.c)

扫描模式:数据水平字节垂直

最大宽度和高度:128 64

勾选 字节内像素数据反序 选项

点击 保存 即可将数据保存到文件。

打开保存的文件,里面是如下内容,是一个 c 语言定义的 char 数组,长度为 1024,一个字节表示 1 x 8 列中的像素数量,如希望下半部全部点亮,就使用 00001111,对应十六进制 0x0F。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const unsigned char gImage_hello[1024] = { /* 0X22,0X01,0X80,0X00,0X40,0X00, */
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0XF0,0XF8,0XFC,0XF8,0XF8,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X02,0XFF,0XFF,0XFF,0XFE,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XC0,0XE0,0XF0,0XE0,0XE0,0X00,0X00,0X00,0X00,0XC0,0XC0,0XC0,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XC0,0XC0,0XC0,0XC0,0XC0,
0XC0,0XC0,0X80,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X80,0XE0,0XF8,0XFC,0XFE,0XFE,
0XFF,0X3F,0X1F,0X1F,0X1F,0X1F,0XFF,0XFF,0XFF,0XFE,0XFE,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X01,0X01,0X00,0X00,0X00,0XF0,0XF8,
0XFC,0XF8,0XF8,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XE0,0XF0,0XF8,
0XF0,0XF0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0X07,0X07,0X07,0X07,0X07,
0X07,0X07,0X07,0XFF,0XFF,0XFF,0XFF,0XFF,0XF8,0XFC,0XFF,0XFF,0X3F,0X1F,0X0F,0X81,
0XC0,0XC0,0XC0,0XC0,0XC0,0XC0,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,
0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X0C,0X1E,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,
0XFE,0XF0,0XF0,0XE0,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,
0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X3F,0X7F,0X7F,0X7F,0X3F,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XF0,0XE0,0XE3,0XE7,
0XE7,0XE7,0XE7,0XE7,0XE7,0XE7,0XE7,0XE7,0XE7,0XC3,0XC1,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X1F,0XFF,
0XFF,0XFF,0XFF,0XF0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X1F,0XFF,0XFF,0XFF,0XFF,0XE7,0X87,0X07,0X07,0X07,0X07,0X07,0X07,0X07,0X83,0XF1,
0XFF,0XFF,0XFF,0XFF,0X3F,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,
0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X01,0X01,0X03,0X03,0X03,0X03,0X03,
0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X01,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,
0X07,0XFF,0XFF,0XFF,0XFF,0XFC,0XC0,0XC0,0XE0,0XF0,0XF8,0XFC,0XFE,0XFC,0X7C,0X00,
0X00,0X03,0X3F,0X7F,0X7F,0X7F,0X7F,0X7C,0X7C,0X7C,0X7C,0X7C,0X7E,0X7F,0X3F,0X1F,
0X0F,0X07,0X03,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X07,0X07,
0X07,0X03,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X0F,0X1F,0X1F,0X3F,0X3F,0X3E,0X3E,0X3E,0X3C,0X1C,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X01,0X03,0X03,0X07,0X0F,0X0F,0X0F,0X0F,0X07,0X01,0X01,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X78,0XFC,0XFE,0XFC,
0X7C,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};

下载 ACROBOTIC SSD1306库

点击 Arduino 菜单栏 “项目” - “加载库” - “管理库” ,在弹出的库管理窗口的搜索框中输入 ACROBOTIC SSD1306 ,选择一个版本安装,一般选择最新版就可以了。

安装完成后,在 Arduino 的示例中就可以找到 ACROBOTIC SSD1306 相关的示例了。

程序代码

打开 ACROBOTIC SSD1306 示例文件中的 DrawLogo,官方示例可以在 OLED 上显示 ACROBOTIC 的 logo,我们替换其中字节数组的内容,换成我们自己图片生成的字节数组(用 Image2Lcd 工具生成的数组第一行有一些图像头信息,把这些删掉)。

注意定义数组时使用的 PROGMEM 关键字,加上这个之后,数组会存到开发板的 FlashROM 中,而不是 RAM 中,这样可以防止单片机出现动态内存不足的问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <Wire.h>
#include <ACROBOTIC_SSD1306.h>

static unsigned char HELLO[] PROGMEM ={
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0XF0,0XF8,0XFC,0XF8,0XF8,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X02,0XFF,0XFF,0XFF,0XFE,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XC0,0XE0,0XF0,0XE0,0XE0,0X00,0X00,0X00,0X00,0XC0,0XC0,0XC0,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XC0,0XC0,0XC0,0XC0,0XC0,
0XC0,0XC0,0X80,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X80,0XE0,0XF8,0XFC,0XFE,0XFE,
0XFF,0X3F,0X1F,0X1F,0X1F,0X1F,0XFF,0XFF,0XFF,0XFE,0XFE,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X01,0X01,0X00,0X00,0X00,0XF0,0XF8,
0XFC,0XF8,0XF8,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XE0,0XF0,0XF8,
0XF0,0XF0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0X07,0X07,0X07,0X07,0X07,
0X07,0X07,0X07,0XFF,0XFF,0XFF,0XFF,0XFF,0XF8,0XFC,0XFF,0XFF,0X3F,0X1F,0X0F,0X81,
0XC0,0XC0,0XC0,0XC0,0XC0,0XC0,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,
0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X0C,0X1E,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,
0XFE,0XF0,0XF0,0XE0,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,
0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X3F,0X7F,0X7F,0X7F,0X3F,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XF0,0XE0,0XE3,0XE7,
0XE7,0XE7,0XE7,0XE7,0XE7,0XE7,0XE7,0XE7,0XE7,0XC3,0XC1,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X1F,0XFF,
0XFF,0XFF,0XFF,0XF0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X1F,0XFF,0XFF,0XFF,0XFF,0XE7,0X87,0X07,0X07,0X07,0X07,0X07,0X07,0X07,0X83,0XF1,
0XFF,0XFF,0XFF,0XFF,0X3F,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,
0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X01,0X01,0X03,0X03,0X03,0X03,0X03,
0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X01,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,
0X07,0XFF,0XFF,0XFF,0XFF,0XFC,0XC0,0XC0,0XE0,0XF0,0XF8,0XFC,0XFE,0XFC,0X7C,0X00,
0X00,0X03,0X3F,0X7F,0X7F,0X7F,0X7F,0X7C,0X7C,0X7C,0X7C,0X7C,0X7E,0X7F,0X3F,0X1F,
0X0F,0X07,0X03,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X07,0X07,
0X07,0X03,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X0F,0X1F,0X1F,0X3F,0X3F,0X3E,0X3E,0X3E,0X3C,0X1C,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X01,0X03,0X03,0X07,0X0F,0X0F,0X0F,0X0F,0X07,0X01,0X01,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X78,0XFC,0XFE,0XFC,
0X7C,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00
};

void setup()
{
Wire.begin();
oled.init(); // 初始化
oled.clearDisplay(); // 清除屏幕
oled.drawBitmap(HELLO, 1024); // 1024 像素
}

void loop()
{
}

上传代码查看结果

将代码上传到 ESP8266 上,即可看到我们自己绘制的图像显示到了 OLED 上,显示内容如文章开头的效果图一样。

问题

图形显示不完整

我发现用上面的方法,OLED 上的图形显示不完整,画面整体向上移动了 8 像素,所以图片最上面的 8 像素将显示不出来,我们需要在图片的字节数组最前面添加 8 * 16 个 0x00 ,将画面向下顶 8 个像素。此外还需要修改绘图函数的参数 oled.drawBitmap(HELLO, 1024); ,将 1024 修改为 1152。

如我们修改上面的图片,在图片最边上添加一圈边框,如下:

按上面描述的方法生成字节数组,并在开头添加 8 * 16 个 0x00,然后修改代码,最终代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <Wire.h>
#include <ACROBOTIC_SSD1306.h>

static unsigned char HELLO[] PROGMEM ={
/* 前 8 行 是添加的 0x00, 后面的是图像的字节数组 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0XF5,0XFC,0XF3,0XFA,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,
0X0F,0X0F,0X0F,0X0F,0X8F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,
0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,
0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,
0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,
0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,
0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,
0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0X0F,0XFA,0XF3,0XFC,0XF5,
0XFF,0XFF,0XFF,0XFF,0X00,0X00,0XF0,0XF8,0XFC,0XF8,0XF8,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X02,0XFF,0XFF,0XFF,0XFE,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XC0,0XE0,0XF0,0XE0,0XE0,0X00,0X00,0X00,0X00,0XC0,0XC0,0XC0,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XC0,0XC0,0XC0,0XC0,0XC0,
0XC0,0XC0,0X80,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X80,0XE0,0XF8,0XFC,0XFE,0XFE,
0XFF,0X3F,0X1F,0X1F,0X1F,0X1F,0XFF,0XFF,0XFF,0XFE,0XFE,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X01,0X01,0X00,0X00,0X00,0XF0,0XF8,
0XFC,0XF8,0XF8,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XE0,0XF0,0XF8,
0XF0,0XF0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0X07,0X07,0X07,0X07,0X07,
0X07,0X07,0X07,0XFF,0XFF,0XFF,0XFF,0XFF,0XF8,0XFC,0XFF,0XFF,0X3F,0X1F,0X0F,0X81,
0XC0,0XC0,0XC0,0XC0,0XC0,0XC0,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,
0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X0C,0X1E,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,
0XFE,0XF0,0XF0,0XE0,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,
0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X3F,0X7F,0X7F,0X7F,0X3F,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XF0,0XE0,0XE3,0XE7,
0XE7,0XE7,0XE7,0XE7,0XE7,0XE7,0XE7,0XE7,0XE7,0XC3,0XC1,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X1F,0XFF,
0XFF,0XFF,0XFF,0XF0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X1F,0XFF,0XFF,0XFF,0XFF,0XE7,0X87,0X07,0X07,0X07,0X07,0X07,0X07,0X07,0X83,0XF1,
0XFF,0XFF,0XFF,0XFF,0X3F,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,
0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X01,0X01,0X03,0X03,0X03,0X03,0X03,
0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X01,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,
0X07,0XFF,0XFF,0XFF,0XFF,0XFC,0XC0,0XC0,0XE0,0XF0,0XF8,0XFC,0XFE,0XFC,0X7C,0X00,
0X00,0X03,0X3F,0X7F,0X7F,0X7F,0X7F,0X7C,0X7C,0X7C,0X7C,0X7C,0X7E,0X7F,0X3F,0X1F,
0X0F,0X07,0X03,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X07,0X07,
0X07,0X03,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X0F,0X1F,0X1F,0X3F,0X3F,0X3E,0X3E,0X3E,0X3C,0X1C,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X01,0X03,0X03,0X07,0X0F,0X0F,0X0F,0X0F,0X07,0X01,0X01,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X78,0XFC,0XFE,0XFC,
0X7C,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,
0XAF,0X3F,0XCF,0X5F,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,
0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,
0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,
0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,
0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,
0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,
0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,
0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0X5F,0XCF,0X3F,0XAF
};

void setup()
{
Wire.begin();
oled.init(); // 初始化
oled.clearDisplay(); // 清除屏幕
oled.drawBitmap(HELLO, 1152); // 1152 像素
}

void loop()
{
}

然后将代码上传到 ESP8266 上,发现正确显示了图形。

参考资料

小结

千万不要迷信卖家提供的资料!我购买 ESP8266 的卖家提供的资料中,ESP8266 开发板与 OLED 的接线是错的,SCL 和 SDA 接反了,这个倒没啥,程序可以修改参数。但 VCC 和 GND 也接反了,我插上之后,立马就感觉 OLED 开始发烫,烫手那种,赶紧给拔了。。。然后自己到网上找资料,总算调试出来了,这对新人小白来说太不友好了。。。初次接触 ESP8266 ,以上内容如果有错误之处,请帮忙指出,感激不尽!