"""
07_oled_test.py
First year hardware project
School of ICT
Metropolia University of Applied Sciences
23.1.2023, Sakari Lukkarinen
This demo
- writes text and draws rectangles to the OLED
- uses I2C to control OLED
- uses ssd1306 library
Notes
- You need to install ssd1306 library in Thonny:
(Tools > Manage Packages, Search: ssd1306, Install)
- The original code can be found:
https://github.com/sakluk/first_year_hardware_project/blob/main/python/07_oled_test.py
- This simulation is based on the instructions and model
provided by TechnicalUpdate, see:
https://www.youtube.com/watch?v=TsakxMANaBQ
https://wokwi.com/projects/320405630824219218
"""
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import utime
# Pins for OLED
OLED_SCL = 15
OLED_SDA = 14
# Initialize I2C to use OLED
i2c = I2C(1, scl=Pin(OLED_SCL), sda=Pin(OLED_SDA), freq=400000)
OLED_WIDTH = 128
OLED_HEIGHT = 64
oled = SSD1306_I2C(OLED_WIDTH, OLED_HEIGHT, i2c)
# Wait for a second
utime.sleep(1)
# Fill with light
oled.fill(1)
oled.show()
# Wait for a second
utime.sleep(1)
# Clear the display (off)
oled.fill(0)
# Repeat 10 times
for n in range(5):
# Write text to OLED
oled.text('Hardware project', 1, 1)
oled.text('School of ICT', 1, 11)
oled.text('Metropolia UAS', 1, 21)
oled.text('23.1.2023', 1, 31)
#oled.text('----------------', 1, 41)
oled.text('123456789-123456', 1, 48)
# Draw rectangles around the last numbers
oled.rect(0, 41, 128, 23, 2)
oled.rect(0, 42, 128, 21, 2)
oled.show()
# Wait for 2 seconds
utime.sleep(2)
# Clear the display
oled.fill(0)
oled.show()
# Wait for 0.5 second
utime.sleep(0.5)