0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rasbery Pi を使って電光掲示板を作る

0
Last updated at Posted at 2025-12-19

はじめに

Rasbery Pi 3B+ で電光掲示板を作るメモです。

こちらで売っている 8x32 dot マトリクスの電光掲示板を利用しました。
https://store.shopping.yahoo.co.jp/solinnovay/led.html?sc_i=shopping-pc-web-detail-item-bclst-_

配線、サンプルプログラムが記載されたマニュアルがダウンロードできるのですが
おそらく、それだけでは動作しない可能性があるので記載しておきます。
※) 実際に動作しなかった

ラズベリーパイの設定

設定画面の表示

ラズベリーパイの設定画面表示させます。

raspi-config

設定メニューを表示し、「Interface Options」 を選択します。
image.png

SPI 設定の有効化

「SPI」を選択して「OK」を押して SPI を有効化

image.png

※ SPI とは

Serial Peripheral Interface(シリアル・ペリフェラル・インターフェース)

センサーやディスプレイなどの周辺ICと高速にデータをやり取りするための同期式シリアル通信方式です。全二重通信に対応し、SCLK(クロック)、MOSI(マスター出力/スレーブ入力)、MISO(マスター入力/スレーブ出力)、CS/SS(チップセレクト)の最低4本の信号線で、マスター(ラズパイ)とスレーブ(周辺機器)が1対1または1対多で高速なデータ転送するインターフェースのこと。

I2C 設定の有効化

「I2C」を選択して「OK」を押して I2C を有効化

image.png

※) I2C とは

シリアルバスの通信規格です。
こちらは使わないかもしれませんが、一応有効にしておきます。

再起動と確認

設定を変更したら、再起動をしてモジュールがロードされて、デバイスが存在するかを確認します。

再起動

reboot

確認

以下のコマンドを実行して「spi_bcm2835」と表示されていればOKです。

lsmod |grep -i spi
spidev                 24576  0
spi_bcm2835            20480  0

デバイスの存在確認で「spidev0.0」と「spidev0.1」が存在すればOKです。

ls -l /dev/spi*
crw-rw---- 1 root spi 153, 0 12月 19 22:24 /dev/spidev0.0
crw-rw---- 1 root spi 153, 1 12月 19 22:24 /dev/spidev0.1

ライブラリのインストール

matrix を実行させるためのライブラリをインストールをします。

apt-get update
usermod -a -G spi,gpio pi
apt install build-essential python3-dev python3-pip libfreetype6-dev libjpeg-dev libopenjp2-7 libtiff6
apt update sudo apt install -y swig liblgpio-dev python3-dev

4行目にある swig をインストールしないと以下のエラーになる。

実行エラー
Traceback (most recent call last):

  File "/home/iseki/test/clock.py", line 17, in <module>

    serial = spi(port=0, device=0)

  File "/home/iseki/denko_keijiban/lib/python3.13/site-packages/luma/core/interface/serial.py", line 302, in __init__

    bitbang.__init__(self, gpio, transfer_size, reset_hold_time, reset_release_time, DC=gpio_DC, RST=gpio_RST)

    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/home/iseki/denko_keijiban/lib/python3.13/site-packages/luma/core/interface/serial.py", line 185, in __init__

    self._gpio = gpio or self.__rpi_gpio__()

                         ~~~~~~~~~~~~~~~~~^^

  File "/home/iseki/denko_keijiban/lib/python3.13/site-packages/luma/core/lib.py", line 23, in __rpi_gpio__

    import RPi.GPIO as GPIO

ModuleNotFoundError: No module named 'RPi'
``` 

</details>

原因は、仮想環境内に RPi.GPIO というライブラリがインストールされていないことです。

luma.core というライブラリが、Raspberry PiのGPIOピン(信号線)を制御するために RPi.GPIO を探していますが、それが見つからないために ModuleNotFoundError が発生しているエラーです。

これをインストールしようとすると、これまた以下の swig というツールが必要なるエラーとなります。

<details><summary>実行エラー</summary>

```bash
pip install rpi-lgpio

Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple

Collecting rpi-lgpio

  Downloading https://www.piwheels.org/simple/rpi-lgpio/rpi_lgpio-0.6-py3-none-any.whl (11 kB)

Collecting lgpio>=0.1.0.1 (from rpi-lgpio)

  Downloading lgpio-0.2.2.0.tar.gz (90 kB)

  Installing build dependencies ... done

  Getting requirements to build wheel ... done

  Preparing metadata (pyproject.toml) ... done

Building wheels for collected packages: lgpio

  Building wheel for lgpio (pyproject.toml) ... error

  error: subprocess-exited-with-error



  × Building wheel for lgpio (pyproject.toml) did not run successfully.

  x exit code: 1

  mq> [8 lines of output]

      running bdist_wheel

      running build

      running build_py

      running build_ext

      building '_lgpio' extension

      swigging lgpio.i to lgpio_wrap.c

      swig -python -o lgpio_wrap.c lgpio.i

      error: command 'swig' failed: No such file or directory

      [end of output]



  note: This error originates from a subprocess, and is likely not a problem with pip.

  ERROR: Failed building wheel for lgpio

Failed to build lgpio

error: failed-wheel-build-for-install



× Failed to build installable wheels for some pyproject.toml based projects

mq> lgpio

ということで、4行目の swig をインストールが必要です。

python の仮想環境作成と実行環境の用意(ツールのインストール)

今どきの環境であれば仮想環境を作成したほうがいいと思いますので、仮想環境上で、rpi-lgpio(GPIO制御用)と spidev(SPI通信用)をインストールします。

python3 -m denko_keijiban venv 
source denko_keijiban/bin/activate
pip install --upgrade --ignore-installed pip setuptools
pip install rpi-lgpio spidev

コード作成

pyhotn で書きます。
現在の年月日時分秒を表示するプログラム (clock.py) です。

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

import time, datetime
from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi
from luma.core.render import canvas
from luma.core.virtual import viewport
from PIL import Image, ImageDraw, ImageFont

FONT = "DejaVuSans.ttf"
CASCADED = 4       # 8x8のドットマトリクスLEDの接続個数

ORIENTATION = -90  # -90, 0, 90, 180
DELAY = 0.05

try:
    serial = spi(port=0, device=0)
    device = max7219(serial, cascaded=CASCADED, block_orientation=ORIENTATION, rotate=0)
    font = ImageFont.truetype(FONT,9)
    virtual = viewport(device, width=30*8, height=20)
    while 1:
        text = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
        with canvas(virtual) as draw:
            draw.text((8*CASCADED,0), text, fill="white", font=font)
        for offset in range(len(text)*8):
            virtual.set_position((offset, 2))
            time.sleep(DELAY)
        with canvas(virtual) as draw:
            draw.text((0,0), " "*30, fill="white", font=font)
        virtual.set_position((0, 0))

except KeyboardInterrupt:
    pass

実行

以下のコマンドで実行します。

python clock.py

(再起動したりで)仮想環境に入っていない場合は、仮想環境に入ってから実行してください。

source ~/denko_keijiban/bin/activate
python3 clock.py

まとめ

不随のマニュアルではいくつか足りないライブラリ、ツールがあったのでエラーとなったが、AI(Gemini) のおかげで解決ができた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?