很久以前买了一块树莓派的小屏幕,时间久了就忘记怎么驱动了,现在重新记录一下

开启SPI和I2C接口

打开树莓派终端,输入以下指令进入配置界面
sudo raspi-config


选择Interfacing Options -> SPI -> Yes
开启SPI接口
I2C同理

然后重启树莓派:
sudo reboot

安装依赖

sudo apt update
sudo apt install python3-smbus
sudo apt-get install python3-pil
sudo apt install python3-numpy

下载测试程序

打开树莓派终端,执行:

sudo apt-get install p7zip-full
sudo wget  https://www.waveshare.net/w/upload/2/2c/OLED_Module_Code.7z
7z x OLED_Module_Code.7z 
cd OLED_Module_Code/RaspberryPi

修改config.py

nano ~/OLED_Module_Code/RaspberryPi/python/lib/waveshare_OLED/config.py

接口选择

Device_SPI = 0
Device_I2C = 1

注意:切换SPI/I2C修改这里

执行内置测试脚本

sudo python3 ~/OLED_Module_Code/RaspberryPi/python/example/OLED_0in91_test.py

设置Stats显示脚本

nano ~/OLED_Module_Code/RaspberryPi/python/stats.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys, os
base_dir = os.path.dirname(os.path.abspath(__file__))
libdir   = os.path.join(base_dir, 'lib')
if os.path.exists(libdir):
    sys.path.insert(0, libdir)

import subprocess
import time
from waveshare_OLED import OLED_0in91
from PIL import Image, ImageDraw, ImageFont

# ---------- 1. 初始化 Waveshare ----------
disp = OLED_0in91.OLED_0in91()
disp.Init()
disp.clear()

# ---------- 2. PIL 画布 ----------
width  = disp.width
height = disp.height
image = Image.new('1', (width, height), "WHITE")   # 0.91inch 底色=1 熄灭
draw  = ImageDraw.Draw(image)
font  = ImageFont.load_default()

# ---------- 3. 主循环 ----------
padding = -2
top     = padding
x       = 0

while True:
    # 清屏(白底)
    draw.rectangle((0, 0, width, height), outline=1, fill=1)

    # 抓取系统信息(三引号避免转义麻烦)
    IP = subprocess.check_output(
        """hostname -I | awk '{print $1}'""", shell=True).decode().strip() or "No IP"

    CPU = subprocess.check_output(
        """top -bn1 | grep load | awk '{printf "CPU Load: %.2f", $(NF-2)}'""", shell=True
    ).decode().strip()

    MemUsage = subprocess.check_output(
        """free -m | awk 'NR==2{printf "Mem: %s/%sMB %.2f%%", $3,$2,$3*100/$2}'""", shell=True
    ).decode().strip()

    Disk = subprocess.check_output(
        """df -h | awk '$NF=="/"{printf "Disk: %d/%dGB %s", $3,$2,$5}'""", shell=True
    ).decode().strip()

    # 画文字(黑字)
    draw.text((x, top),    "IP: " + IP,      font=font, fill=0)
    draw.text((x, top+8),  CPU,              font=font, fill=0)
    draw.text((x, top+16), MemUsage,         font=font, fill=0)
    draw.text((x, top+25), Disk,             font=font, fill=0)

    # 刷新屏幕
    disp.ShowImage(disp.getbuffer(image))
    time.sleep(2)

执行脚本
python3 ~/OLED_Module_Code/RaspberryPi/python/stats.py

设置成开机自启

下面给出 Raspberry Pi OS(systemd) 下最简洁、可靠的 开机自启动 方案,让你的
stats.py 在系统一上电就运行,无需登录,异常退出会自动重启。

  1. 准备脚本
    假设文件路径:
    ~/OLED_Module_Code/RaspberryPi/python/stats.py
    确保有执行权限:

    chmod +x ~/OLED_Module_Code/RaspberryPi/python/stats.py

    脚本第一行必须是
    #!/usr/bin/env python3

  2. 创建 systemd 服务文件

    sudo nano /etc/systemd/system/oled-stats.service

    内容(复制即用,只需改用户名和路径):

    [Unit]
    Description=0.91inch OLED Stats Monitor
    After=network.target
    
    [Service]
    Type=simple
    User=改成你的用户名
    WorkingDirectory=/home/你的用户名/OLED_Module_Code/RaspberryPi/python
    ExecStart=/usr/bin/python3 /home/你的用户名/OLED_Module_Code/RaspberryPi/python/stats.py
    Restart=always
    RestartSec=10
    
    [Install]
    WantedBy=multi-user.target
  3. 启用并立即启动

    sudo systemctl daemon-reload
    sudo systemctl enable --now oled-stats.service
  4. 查看状态/排错

    sudo systemctl status oled-stats.service

    实时日志:

    journalctl -u oled-stats -f
  5. 停止 / 禁用(如需)

    sudo systemctl stop oled-stats
    sudo systemctl disable oled-stats

完成

重启树莓派:

sudo reboot

屏幕将在 开机后 10 秒左右 自动开始显示系统信息,无需登录。

参考图片

参考资料:

在树莓派上使用 SSD1306 OLED 屏幕
waveshare 0.91inch OLED Module 官方说明

最后修改:2025 年 11 月 06 日
请作者喝咖啡!