Raspberry Pi3可以直接使用GPIO接口驱动OLED屏
一、接线
根据网上随便找的图可以看到树莓派3的GPIO接口引脚顺序
PS:26pin的GPIO为前26针
根据OLED屏的引脚说明,如表连接即可:
树莓 | OLED |
5V | VCC |
GND | GND |
SCL | SCL |
SDA | SDA |
二、安装必要工具
sudo apt-get install -y python-smbus
sudo apt-get install -y i2c-tools
安装完成后,运行sudo raspi-config,将I2C接口启用
运行
sudo i2cdetect -y 1
如果在0x3c处有标记,则说明I2C启动成功
三、编码显示
树莓python驱动oled屏需要luma.oled库,直接pip安装即可
代码:
from luma.core.interface.serial import i2c from luma.oled.device import sh1106 from luma.core.render import canvas from PIL import ImageFont import time def stats(oled): font = ImageFont.load_default() with canvas(oled) as draw: localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) draw.text((2, 5), localtime, font=font, fill=255) def main(): serial = i2c(port=1, address=0x3C) oled = sh1106(serial) while True: stats(oled) time.sleep(1) if __name__ == "__main__": main()