树莓派3B使用I2C接口SSD1306 OLED屏幕

分享, 折腾 2019/12/17

之前用的是SPI接口的SSD1306,有7个针脚,前文:树莓派SSD1306屏幕180度旋转

后来被我不小心折了一下屏幕排线,断了……于是就又买了给I2C接口的SSD1306,4针脚

具体怎么玩,教程比较多,有些地方总结一下。

一、安装

sudo apt-get install -y i2c-tools

VCC\GND\SCL\SDA分别对应树莓派GPIO的1\6\5\3接口,raspi-config 里打开I2C开关,执行检测

sudo i2cdetect -y 1

在0x3C处有输出,说明接线OK。

二、驱动

这里使用的是luma.oled https://github.com/rm-hull/luma.oled

git clone https://github.com/rm-hull/luma.oled.git
cd luma.oled
python setup.py install

三、测试

可以使用 luma 配套的一些测试例程,https://github.com/rm-hull/luma.examples

git clone https://github.com/rm-hull/luma.examples.git
cd luma.examples/examples/
python demo.py

四、上手

在前文 树莓派SSD1306屏幕180度旋转 中,因为我的屏幕要旋转180度,代码大概如下:

from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import ssd1306, ssd1325, ssd1331, sh1106

serial = i2c(port=1, address=0x3C)
#rotate参数,整数值,可选0(默认)、1、2、3
#其中0为无旋转,1为顺时针旋转90°,2为180°,3为270°
device = ssd1306(serial, rotate=2)
……

另外其默认调用是,执行完毕后清除当前屏幕内容

如果需要执行完毕代码,并保留内容的话,需要重新定义 cleanup 方法

from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import ssd1306, ssd1325, ssd1331, sh1106

serial = i2c(port=1, address=0x3C)
device = ssd1306(serial, rotate=2)
#定义cleanup为空,覆盖原方法,可保留内容
device.cleanup = ""
本文标签:


一条评论

  • 谢东东 评论于 回复

    在用whlie反复读取刷新后,oled会在刷新前黑屏一下,然后再显示出来,有一段很明显的黑屏,请问这个问题怎么解决呢?谢谢

  • 评论(本站已开启评论回复邮件通知功能,请如实填写邮箱以便及时收到回复)关闭回复