トップ 差分 一覧 ソース 検索 ヘルプ RSS ログイン

Tips-ssd1306

  ssd1306

aliexpress

amazon

VCCとGNDの違いがあるので注意

Arduinoで

https://qiita.com/jakalada/items/8f83d07291d984a31633

micropython で

https://qiita.com/inachi/items/ceb3e8e12022a7cbdf7b

git 

https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py

https://github.com/micropython/micropython/tree/master/drivers/display

配線

SSD1306 ESP32 STM32F103
GND GND GND
VCC 3v3 3v3
SCL IO4 SCL1(PB6)
SDA IO5 SDA1(PB7)

  接続の確認

from machine import I2C, Pin, SoftI2C
# i2c=I2C(scl=Pin(5),sda=Pin(4),freq=100000)
i2c=SoftI2C(scl=Pin(5),sda=Pin(4),freq=100000)
print(i2c.scan())
print("END")

  micropython サンプル

文字を表示

# SSD1306
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C

i2c = I2C(scl=Pin(4), sda=Pin(5))
## i2c = I2C(scl=Pin(34), sda=Pin(33))

oled = SSD1306_I2C(128, 64, i2c)

oled.text('Hello, World 1!', 0, 0)
oled.text('Hello, World 2!', 0, 10)
oled.text('Hello, World 3!', 0, 20)

oled.text('Hello, World 4!', 0, 30)
oled.text('Hello, World 5!', 0, 40)
oled.text('Hello, World 6!', 0, 50)

oled.show()

図形を描く

# SSD1306
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
i2c = I2C(scl=Pin(4), sda=Pin(5))
## i2c = I2C(scl=Pin(34), sda=Pin(33))
oled = SSD1306_I2C(128, 64, i2c)

oled.fill(0)
oled.hline(0,32,128,1)
oled.vline(64,0,64,1)
oled.show()

oled.line(0,0,127,63,1)
oled.rect(72,8,24,16,1)
oled.fill_rect(24,48,24,16,1)
oled.show()

大きなフォント

https://github.com/peterhinch/micropython-font-to-py/tree/master/writer

https://github.com/peterhinch/micropython-nano-gui

arial10.py
courier20.py
を使う

時刻を OLED に

QIITA のコードを参考(丸写し?)して大きなフォントで時刻表示https://qiita.com/tshimizu8/items/a7c189612a9ba0a5284b

# ORIGNAL CODE https://qiita.com/tshimizu8/items/a7c189612a9ba0a5284b
# Watch with NTP synchronization
# MicroPython on Lolin ESP32 OLED 128x64 16x6(0..50)

ssid = "yourAPid"; password =  "APpassword"
ntp_host = "jp.pool.ntp.org"
# (datetime.date(2000, 1, 1) - datetime.date(1900, 1, 1)).days * 24 * 60 * 60
NTP_DELTA = 3155673600

import machine, time, ssd1306
import network, socket, struct
import gc

i2c = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

oled.fill(0)
#         '1234567890123456'
oled.text('Python NtpClient', 0, 0)
# oled.text('SSD1306 128x64  ', 0, 10)

station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)

while station.isconnected() == False: pass

oled.text(station.ifconfig()[0], 0, 15,1)
oled.show()

import arial10   # font
import courier20 # FONT
from writer import Writer
wri10 = Writer(oled, arial10, verbose=False)
wri20 = Writer(oled, courier20, verbose=False)

# NTPの時刻取得ルーチン
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1b
# アドレス取得
addr = socket.getaddrinfo(ntp_host, 123)[0][-1]
# NTPポートを開く
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
s.close()

val = struct.unpack("!I", msg[40:44])[0]
# 時刻 val に9時間分付加してJSTにする
ntpSec = val - NTP_DELTA  + (9*60*60)
deltaSec = ntpSec - time.time() 

def f2(i): return '{:0>2}'.format(i)    

nowString = dayString = ""

while True:
       nowSec = time.time() + deltaSec
       now = time.localtime(nowSec)

       # oled.text(nowString,0,35,0)  # Clear 
       nowString = '%s:%s:%s' % (f2(now[3]),f2(now[4]),f2(now[5]))
       #print(nowString)
       #oled.text(nowString,0,35)
       Writer.set_textpos(oled, 30, 8 )
       wri20.printstring(nowString)

       # oled.text(dayString,0,50,0)
       dayString = '%s/%s/%s' % (str(now[0]),f2(now[1]),f2(now[2]))
       # oled.text(dayString,0,50)
       Writer.set_textpos(oled, 50, 8 )
       wri10.printstring(dayString)

       oled.show()
       time.sleep(1)
       

STM32F103