sudo raspi-config
# enable i2c in Interfacing options
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: -- -- -- -- -- -- -- --
pip3 install adafruit-circuitpython-ssd1306
mkdir display
cd display
# fix missing font
curl -sL https://github.com/adafruit/Adafruit_CircuitPython_framebuf/blob/master/examples/font5x8.bin?raw=true -o font5x8.bin
# Basic example of clearing and drawing pixels on a SSD1306 OLED display.
# This example and library is meant to work with Adafruit CircuitPython API.
# Author: Tony DiCola
# Author: Wolfgang Fahl
# License: Public Domain
# Import all board pins.
from board import SCL, SDA
import busio
# Import the SSD1306 module.
import adafruit_ssd1306
# time helpers
import time
from time import strftime
class TestOLED(object):
""" test the OLED SSD1306 display"""
def __init__(self,width,height):
""" construct me with the given width and height"""
# Create the I2C interface.
self.i2c = busio.I2C(SCL, SDA)
# Create the SSD1306 OLED class.
# The first two parameters are the pixel width and pixel height. Change these
# to the right size for your display!
self.oled= adafruit_ssd1306.SSD1306_I2C(width, height, self.i2c)
# Alternatively you can change the I2C address of the device with an addr parameter:
#display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x31)
def white(self):
""" show all white pixels"""
# Clear the display. Always call show after changing pixels to make the display
# update visible!
self.oled.fill(1)
self.oled.show()
def black(self):
""" show all black pixels"""
self.oled.fill(0)
self.oled.show()
def pattern(self):
""" show some pixel patterns"""
for p in range(32,2,-2):
self.oled.fill(0)
for y in range(self.oled.height):
for x in range(self.oled.width):
if x%(2*p)==0 and y%p==0:
self.oled.pixel(x,y,1)
self.oled.show()
def shapes(self):
""" show some shapes"""
w=self.oled.width
h=self.oled.height
for d in range(0,h//2,2):
self.oled.fill(0)
self.oled.rect(d, d, w-2*d,h-2*d, True)
self.oled.line(d, d, w-d,h-d, True)
self.oled.line(d, h-d, w-d, d, True)
self.oled.fill_rect((w-d-1)//2, (h-d-1)//2, d+1, d+1, True)
self.oled.show()
def text(self,lines,size=1):
""" show the given lines of text at the given size"""
self.oled.fill(0)
yp=0
for line in lines:
self.oled.text(line,0,yp,True,size=size)
yp=yp+10*size
self.oled.show()
def clock(self):
# please note that the order of lines is in reverse at size 2
while True:
curDate=strftime("%Y-%m-%d")
curTime=strftime("%H:%M:%S")
test.text({curTime,curDate},size=2)
time.sleep(1)
test=TestOLED(128,64)
time.sleep(1)
test.black()
time.sleep(1)
test.shapes()
time.sleep(1)
test.pattern()
time.sleep(1)
test.text({"Good morning","Dear Maria!","How are you?"})
time.sleep(3)
test.clock()
python3 show.py
# Basic example of clearing and drawing pixels on a SSD1306 OLED display.
# This example and library is meant to work with Adafruit CircuitPython API.
# Author: Tony DiCola
# Author: Wolfgang Fahl
# License: Public Domain
# Import all board pins.
from board import SCL, SDA
import busio
# Import the SSD1306 module.
import adafruit_ssd1306
# time helpers
import time
from time import strftime
class TestOLED(object):
""" test the OLED SSD1306 display"""
def __init__(self,width,height):
""" construct me with the given width and height"""
# Create the I2C interface.
self.i2c = busio.I2C(SCL, SDA)
# Create the SSD1306 OLED class.
# The first two parameters are the pixel width and pixel height. Change these
# to the right size for your display!
self.oled= adafruit_ssd1306.SSD1306_I2C(width, height, self.i2c)
# Alternatively you can change the I2C address of the device with an addr parameter:
#display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x31)
def white(self):
""" show all white pixels"""
# Clear the display. Always call show after changing pixels to make the display
# update visible!
self.oled.fill(1)
self.oled.show()
def black(self):
""" show all black pixels"""
self.oled.fill(0)
self.oled.show()
def pattern(self):
""" show some pixel patterns"""
for p in range(32,2,-2):
self.oled.fill(0)
for y in range(self.oled.height):
for x in range(self.oled.width):
if x%(2*p)==0 and y%p==0:
self.oled.pixel(x,y,1)
self.oled.show()
def shapes(self):
""" show some shapes"""
w=self.oled.width
h=self.oled.height
for d in range(0,h//2,2):
self.oled.fill(0)
self.oled.rect(d, d, w-2*d,h-2*d, True)
self.oled.line(d, d, w-d,h-d, True)
self.oled.line(d, h-d, w-d, d, True)
self.oled.fill_rect((w-d-1)//2, (h-d-1)//2, d+1, d+1, True)
self.oled.show()
def text(self,lines,size=1):
""" show the given lines of text at the given size"""
self.oled.fill(0)
yp=0
for line in lines:
self.oled.text(line,0,yp,True,size=size)
yp=yp+10*size
self.oled.show()
def clock(self):
# please note that the order of lines is in reverse at size 2
while True:
curDate=strftime("%Y-%m-%d")
curTime=strftime("%H:%M:%S")
test.text({curTime,curDate},size=2)
time.sleep(1)
test=TestOLED(128,64)
time.sleep(1)
test.black()
time.sleep(1)
test.shapes()
time.sleep(1)
test.pattern()
time.sleep(1)
test.text({"Good morning","Dear Maria!","How are you?"})
time.sleep(3)
test.clock()
python3 show.py