Raspberrypi5 仮想空間でSSD1306表示

AE-BME280と同じ仮想空間内でSMBUS2を使ってテキスト表示

smbus2のインストールはBME280の時に実行済み
pip install smbus2

ここからはchatgpt様と相談しながら
必要なライブラリをインストール
pip install luma.oled

もう一つ
pip install Pillow

示してくれたソースは


import time
from luma.core.interface.serial import i2c
from luma.oled.device import ssd1306
from luma.core.render import canvas

# I2Cバス番号(Raspberry Piでは通常1)
serial = i2c(port=1, address=0x3C)

# SSD1306デバイスのインスタンスを作成
device = ssd1306(serial)

# テキストを表示する関数
def display_text(text):
    with canvas(device) as draw:
        # テキスト表示の座標を設定
        draw.text((10, 10), text, fill="white")

# メイン実行部
try:
    display_text("Hello, Raspberry Pi!")
    time.sleep(10)  # 10秒間表示

except KeyboardInterrupt:
    print("プログラムを終了します。")

finally:
    # 終了前にディスプレイをクリア
    with canvas(device) as draw:
        pass

実行は 仮想空間
source venv/bin/activate

python ssd1306dsp.py

カテゴリー iot