0.91 英吋 OLED 顯示器 - 產品介紹

語言

0.91 吋 OLED 顯示器是一款基於 I2C 通訊,解析度為 128x32 的低功耗顯示模組。它支援與 Jetson 系列主控、Raspberry Pi 和 Orange Pi 等多種開發板連接,能夠即時顯示系統運行狀態資訊。

核心功能

  • 📊 即時顯示 CPU 使用率
  • ⏰ 顯示目前系統時間
  • 💾 顯示記憶體使用率及總記憶體
  • 🗄️ 顯示 TF 卡空間使用率
  • 🌐 顯示設備本機 IP 位址
  • 🔌 支援熱插拔,穩定可靠

硬體連接

接線說明

採用 I2C 通訊介面,接線方式如下:

OLED 引腳 開發板引腳
VCC 3.3V
GND GND
SDA I2C SDA
SCL I2C SCL
⚠️ 注意:請確保接線正確,避免引腳短路,否則可能導致主板硬體損壞!

軟體使用指南

支援平台:

  • ✅ Jetson 系列主控
  • ✅ 所有 Raspberry Pi 型號
  • ✅ 所有 Orange Pi 型號
  • ✅ 其他支援 I2C 通訊的 Linux 開發板

Jetson 系列安裝步驟

1. 安裝依賴

sudo apt install -y python3-pip
sudo pip3 install smbus
sudo pip3 install Adafruit_SSD1306

2. 偵測 I2C 設備

# 列出所有 I2C 匯流排
i2cdetect -l

# 偵測 I2C 設備,OLED 預設位址為 0x3c
i2cdetect -y -r *

3. 執行測試程式碼

#!/usr/bin/env python3
# coding=utf-8
import time
import os
import sys
import Adafruit_SSD1306 as SSD
from PIL import Image, ImageDraw, ImageFont
import subprocess

class Juxi_OLED:
    def __init__(self, i2c_bus=1, debug=False):
        self.__debug = debug
        self.__i2c_bus = i2c_bus
        self.__top = -2
        self.__x = 0
        self.__total_last = 0
        self.__idle_last = 0
        self.__str_CPU = "CPU:0%"

    # 初始化 OLED,成功回傳 True,失敗回傳 False
    def begin(self):
        try:
            self.__oled = SSD.SSD1306_128_32(rst=None, i2c_bus=self.__i2c_bus, gpio=1)
            self.__oled.begin()
            self.__oled.clear()
            self.__oled.display()
            self.__width = self.__oled.width
            self.__height = self.__oled.height
            self.__image = Image.new('1', (self.__width, self.__height))
            self.__draw = ImageDraw.Draw(self.__image)
            self.__font = ImageFont.truetype("DejaVuSansMono.ttf",8)
            return True
        except:
            return False

    # 清除顯示器,refresh=True 即時刷新
    def clear(self, refresh=False):
        self.__draw.rectangle((0, 0, self.__width, self.__height), outline=0, fill=0)
        if refresh:
            self.refresh()

    # 在指定位置添加文字
    def add_text(self, start_x, start_y, text, refresh=False):
        if start_x > 128 or start_x < 0 or start_y < 0 or start_y > 32:
            return
        x = int(start_x + self.__x)
        y = int(start_y + self.__top)
        self.__draw.text((x, y), str(text), font=self.__font, fill=255)
        if refresh:
            self.refresh()

    # 在指定行(1-4)添加文字
    def add_line(self, text, line=1, refresh=False):
        if line < 1 or line > 4:
            return
        y = int(8 * (line - 1))
        self.add_text(0, y, text, refresh)

    # 刷新 OLED 顯示器
    def refresh(self):
        self.__oled.image(self.__image)
        self.__oled.display()

    # 獲取 CPU 使用率
    def getCPULoadRate(self, index):
        count = 10
        if index == 0:
            f1 = os.popen("cat /proc/stat", 'r')
            stat1 = f1.readline()
            data_1 = []
            for i in range(count):
                data_1.append(int(stat1.split(' ')[i+2]))
            self.__total_last = sum(data_1)
            self.__idle_last = data_1[3]
        elif index == 4:
            f2 = os.popen("cat /proc/stat", 'r')
            stat2 = f2.readline()
            data_2 = []
            for i in range(count):
                data_2.append(int(stat2.split(' ')[i+2]))
            total_now = sum(data_2)
            idle_now = data_2[3]
            total = int(total_now - self.__total_last)
            idle = int(idle_now - self.__idle_last)
            usage = int(total - idle)
            usageRate = int(float(usage / total) * 100)
            self.__str_CPU = "CPU:" + str(usageRate) + "%"
            self.__total_last = 0
            self.__idle_last = 0
        return self.__str_CPU

    # 獲取系統時間
    def getSystemTime(self):
        cmd = "date +%H:%M:%S"
        date_time = subprocess.check_output(cmd, shell=True)
        str_Time = str(date_time).lstrip('b\'').rstrip('\\n\'')
        return str_Time

    # 獲取記憶體使用率
    def getUsagedRAM(self):
        cmd = "free | awk 'NR==2{printf \"RAM:%2d%% -> %.1fGB \", 100*($2-$7)/$2, ($2/1048576.0)}'"
        FreeRam = subprocess.check_output(cmd, shell=True)
        str_FreeRam = str(FreeRam).lstrip('b\'').rstrip('\'')
        return str_FreeRam

    # 獲取磁碟使用率
    def getUsagedDisk(self):
        cmd = "df -h | awk '$NF==\"/\"{printf \"SDC:%s -> %.1fGB\", $5, $2}'"
        Disk = subprocess.check_output(cmd, shell=True)
        str_Disk = str(Disk).lstrip('b\'').rstrip('\'')
        return str_Disk

    # 獲取本機 IP 位址
    def getLocalIP(self):
        ip = os.popen("/sbin/ifconfig enP8p1s0 | grep 'inet' | awk '{print $2}'").read()
        ip = ip[0: ip.find('\n')]
        if(ip == ''):
            ip = os.popen("/sbin/ifconfig wlP1p1s0 | grep 'inet' | awk '{print $2}'").read()
            ip = ip[0: ip.find('\n')]
            if(ip == ''):
                ip = 'x.x.x.x'
        if len(ip) > 15:
            ip = 'x.x.x.x'
        return ip

    # 主運作循環,支援熱插拔
    def main_program(self):
        state = False
        try:
            cpu_index = 0
            state = self.begin()
            while state:
                self.clear()
                str_CPU = self.getCPULoadRate(cpu_index)
                str_Time = self.getSystemTime()
                if cpu_index == 0:
                    str_FreeRAM = self.getUsagedRAM()
                    str_Disk = self.getUsagedDisk()
                    str_IP = "IPA:" + self.getLocalIP()
                self.add_text(0, 0, str_CPU)
                self.add_text(50, 0, str_Time)
                self.add_line(str_FreeRAM, 2)
                self.add_line(str_Disk, 3)
                self.add_line(str_IP, 4)
                self.refresh()
                cpu_index = cpu_index + 1
                if cpu_index >= 5:
                    cpu_index = 0
                time.sleep(.1)
        except:
            pass

if __name__ == "__main__":
    try:
        i2c_num = 7
        if len(sys.argv) > 1:
            if str(sys.argv[1]).isdigit():
                i2c_num = int(sys.argv[1])
        
        oled = Juxi_OLED(i2c_num, debug=True)
        while True:
            oled.main_program()
            time.sleep(2)
    except KeyboardInterrupt:
        oled.clear(True)
        del oled
        print(" Program closed! ")
        pass

4. 啟動程式

# 預設匯流排 7
python3 Oled_i2c.py

# 或指定匯流排 (例如匯流排 1)
python3 Oled_i2c.py 1

Raspberry Pi / Orange Pi 安裝步驟

1. 啟用 I2C 介面

進入系統設定:Preferences → Raspberry Pi Configuration → Interfaces → Enable I2C option,然後重新啟動系統。

或者使用命令列模式:

sudo raspi-config
# 選擇 Interface Options → I2C → Enable → 重新啟動系統

2. 安裝依賴

sudo apt install -y python3-dev python3-smbus i2c-tools python3-pil python3-pip python3-setuptools python3-rpi.gpio python3-venv

# 安裝所需字體
sudo apt-get install -y fonts-dejavu-core

3. 配置權限

sudo chmod a+rw /dev/i2c-* # 暫時生效,永久生效需要配置 udev 規則

4. 偵測設備

i2cdetect -y 1
# 通常會顯示設備位址 0x3c

5. 建立虛擬環境(推薦)

# 安裝虛擬環境工具(如果未安裝)
sudo apt-get install -y python3-venv

# 建立虛擬環境
python3 -m venv oled_env

# 啟用虛擬環境
source oled_env/bin/activate

# 安裝所需函式庫
pip install Adafruit_SSD1306 pillow -i https://mirrors.aliyun.com/pypi/simple/

6. 執行程式

python3 Oled_i2c.py 1
# 如果出現權限錯誤,請檢查 sudo 配置或 I2C 設備權限

顯示效果

程式成功運行後,OLED 螢幕將分 4 行顯示以下資訊:

  1. 第一行:CPU 使用率 + 系統時間
  2. 第二行:記憶體使用率 + 總記憶體
  3. 第三行:TF 卡使用率 + 總容量
  4. 第四行:設備本機 IP 位址

相關連結

 

這篇文章是摘要。請在維基上閱讀完整的技術文件。

在維基百科上閱讀全文 →

在飛書知識庫中開啟