LoginSignup
3
3

More than 5 years have passed since last update.

30: Raspberry Pi3 で小型OLEDに表示する

Last updated at Posted at 2018-05-07

はじめに

こちらは
Arduino と Raspberry Pi の違いのまとめ
の個別記事で、Raspberry Pi でstatusを小型OLEDに表示する。

目的

小型のOLED (128x64)や(128x32)を接続して、status などを表示する。

Arduino では、u8g2 libraryを利用していた。

接続

Raspberry Pi では、下記のPin 1, 3, 5, 6とOLEDと4本それぞれ接続する。
- Pin1 3.3V
- Pin3 SDA
- Pin5 SCL
- Pin6 GND

正常に接続されていると、次のようにi2cdetectコマンドを使うとアドレスが表示される。
i2cdtect -y だけではだめで、1 を引数に指定する必要がある。


$ 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: -- -- -- -- -- -- -- --    

ちなみに線が抜けていると、何も Address は表示されない。安物のワイヤを使っていると接続が緩くなって何も表示されない。接触不良はいらいらするので、XHコネクタを利用してがっちり接続するようにした。


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

Software

Adafruitのlibraryを利用する。

installation


$ git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git
$ cd Adafruit_Python_SSD1306
$ sudo python setup.py install

動作確認

exampleのstats.pyで動作を確認した。


$ python stats.py

ただし、SSD 1306 OLED 用のライブラリを作るのにAdafruitが時間を費やしたのでAdafruitから対応hardware を購入しろということが readme に記載されていた。

sample

stats.pyからの変更点は、font を
http://www.dafont.com/bitmap.php
からdownloadして使っているところのみ。


$ cat 31_OLED-stats.py 
#!/usr/bin/python

import time

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

import subprocess

# Raspberry Pi pin configuration:
RST = None     # on the PiOLED this pin isnt used
# Note the following are only used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0

# 128x32 display with hardware I2C:
#disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)

# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)

# Note you can change the I2C address by passing an i2c_address parameter like:
# disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, i2c_address=0x3C)

# Initialize library.
disp.begin()

# Clear display.
disp.clear()
disp.display()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0


# Load default font.
font = ImageFont.load_default()

# Alternatively load a TTF font.  Make sure the .ttf font file is in the same directory as the python script!
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
# font = ImageFont.truetype('Minecraftia.ttf', 8)

font2 = ImageFont.truetype('upheavtt.ttf',15)

while True:
    # Draw a black filled box to clear the image.
    draw.rectangle((0,0,width,height), outline=0, fill=0)

    # Write two lines of text.
    draw.text((x, top+12), "test",  font=font2, fill=255)
    draw.text((x, top+24), "system",  font=font2, fill=255)

    # Display image.
    disp.image(image)
    disp.display()
    time.sleep(.1)

親記事

Arduino と Raspberry Pi の違い

3
3
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
3
3