サイトロゴ

Raspberry PiでOLEDに文字表示

著者画像
Toshihiko Arai

この記事では、Raspberry Piと0.91インチのOLEDディスプレイを使って、文字を表示するまでのやり方を説明します。

具体的にはAdafruit CircuitのPythonドライバーを使って、I2C通信でOLEDに文字を表示します。

ArduinoでOLEDを使いたい場合は、 をご覧ください。

Raspberry PiとOLEDモジュールの配線

Raspberry PiとOLEDモジュールの配線を次の通り行ってください。

OLEDモジュール Raspberry Pi
GND GND
VCC 3.3V
SCL SCL(GPIO3)
SDA SDA(GPIO2)

アドレスの確認

Raspberry PiとOLEDディスプレイの配線ができたら、次のコマンドをRaspberry Piで実行してI2Cデバイスのアドレスを確認しましょう。ここでは、3cがOLEDモジュールのアドレスになりました。

$ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- 

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

OLEDモジュールをRaspberry Piで簡単に扱えるように、ライブラリのインストールを行います。 ### Adafruit CircuitPython SSD1306のインストール 今回使う「DSP TECH 0.91インチディスプレイ」には、メインチップにSSD1306が使われてます。 「Adafruit Python SSD1306」ライブラリが公開されいてますが、こちらはdeprecatedとなっており、現在開発が止まっているようです。今後は、CircuitPythonのライブラリ推奨とのことで、そちらを使用します。

次のコマンドを実行し、「Adafruit CircuitPython SSD1306」をインストールします。

$ sudo pip3 install adafruit-circuitpython-ssd1306

▼ Adafruit CircuitPython SSD1306 https://github.com/adafruit/Adafruit_CircuitPython_SSD1306

▼ DSD TECH/OLEDディスプレイの仕様 http://www.dsdtech-global.com/2018/05/iic-oled-lcd-u8glib.html

必要なライブラリのインストール

ほかにもいくつか必要なライブラリがありますので、次の通りインストールしておきましょう。

$ sudo pip3 install adafruit-blinka
$ sudo pip3 install pillow

pillow関係のエラーが出てしまったので、次のモジュールもインストールしました。

$ sudo apt-get update
$ sudo apt-get install libopenjp2-7
$ sudo apt install libtiff5

フォントのインストール

さいごに、OLEDに文字を表示させるには、Raspberry Piにフォントをインストールする必要があります。

今回は定評のある「Noto Fonts」の日本語版をインストールしました。ちなみに「Noto Fonts」とは、GoogleがAdobeと協力して作成したフォントファミリーです。

$ sudo apt-get install fonts-noto-cjk

インストールしたフォントは、/usr/share/fonts/ディレクトリ下に置かれました。

 $ ls /usr/share/fonts/opentype/noto/
NotoSansCJK-Black.ttc      NotoSansCJK-Light.ttc    NotoSansCJK-Thin.ttc    NotoSerifCJK-ExtraLight.ttc  NotoSerifCJK-Regular.ttc
NotoSansCJK-Bold.ttc       NotoSansCJK-Medium.ttc   NotoSerifCJK-Black.ttc  NotoSerifCJK-Light.ttc       NotoSerifCJK-SemiBold.ttc
NotoSansCJK-DemiLight.ttc  NotoSansCJK-Regular.ttc  NotoSerifCJK-Bold.ttc   NotoSerifCJK-Medium.ttc

▼ Noto Fonts https://www.google.com/get/noto/

OLEDディスプレイに文字表示

それではさっそく、OLEDに文字を表示させてみましょう。

Adafruitのサンプルプログラムを参考に、プログラミングしてみました。OLEDの解像度は128x32ですので、他のサイズのOLEDの場合は宣言部分を書き換えるようにしてください。

import board
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306


i2c = board.I2C()
oled = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c)

oled.fill(0)
oled.show()

image = Image.new("1", (oled.width, oled.height))
draw = ImageDraw.Draw(image)


font = ImageFont.truetype("/usr/share/fonts/opentype/noto/NotoSansCJK-DemiLight.ttc", 13)
font2 = ImageFont.truetype("/usr/share/fonts/opentype/noto/NotoSansCJK-DemiLight.ttc", 10)

draw.text((0, 0), "こんにちは、世界!", font=font, fill=255)
draw.text((0, 17), "This is OLED test.", font=font2, fill=255)

oled.image(image)
oled.show()

実行するとこんな感じで文字を表示することができました。

他にもOLEDにいろいろな文字を表示するサンプルプログラムがあります。

▼ Adafruit CircuitPython SSD1306サンプルプログラム https://github.com/adafruit/Adafruit_CircuitPython_SSD1306/tree/master/examples

関連記事