With MicroPython firmware installed on ESP32-C3, It's a exercise to drive 0.96 inch 128x64 OLED with SSD1306 I2C driver.
To driver SSD1306 I2C OLED, we are going to install MicroPython ssd1306
driver:
- Visit
https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py
- Copy ssd1306.py and upload to MicroPython device, '/' directory.
In my exercise, the default I2C(0) is used to drive SSD1306; SCL = 18, SDA = 19.
Connection between:
ESP32-C3 I2C SSD1306 OLED
============================
GND GND
3V3 VCC
18 SCL
19 SDA
Exercise code
mpyESP32C3_ssd1306.py, a simplest example. Also show how to catch OSError of ENODEV if no I2C ssd1306 connected."""
MicroPython/ESP32C3 exercise run on ESP32-C3-DevKitM-1,
work with SSD1306 I2C OLED.
And,catch exception of ENODEV.
"""
import uos
import usys
import machine
import time
import ssd1306
print("====================================")
print(usys.implementation[0], uos.uname()[3],
"\nrun on", uos.uname()[4])
print("====================================")
oled_i2c = machine.I2C(0)
print("Default I2C:", oled_i2c)
"""
oled_ssd1306 = ssd1306.SSD1306_I2C(128, 64, oled_i2c)
print("Default SSD1306 I2C address:",
oled_ssd1306.addr, "/", hex(oled_ssd1306.addr))
oled_ssd1306.text('Hello, World!', 0, 0, 1)
oled_ssd1306.show()
"""
try:
oled_ssd1306 = ssd1306.SSD1306_I2C(128, 64, oled_i2c)
print("Default SSD1306 I2C address:",
oled_ssd1306.addr, "/", hex(oled_ssd1306.addr))
oled_ssd1306.text('Hello, World!', 0, 0, 1)
oled_ssd1306.show()
except OSError as exc:
print("OSError!", exc)
if exc.errno == errno.ENODEV:
print("No such device")
print("~ bye ~")
mpyESP32C3_ssd1306_hello.py
"""
MicroPython/ESP32C3 exercise run on ESP32-C3-DevKitM-1,
work with SSD1306 I2C OLED.
HelloWorld! with something.
"""
import uos
import usys
import machine
import time
import ssd1306
print("====================================")
print(usys.implementation[0], uos.uname()[3],
"\nrun on", uos.uname()[4])
print("====================================")
oled_i2c = machine.I2C(0)
print("Default I2C:", oled_i2c)
oled_ssd1306 = ssd1306.SSD1306_I2C(128, 64, oled_i2c)
print("Default SSD1306 I2C address:",
oled_ssd1306.addr, "/", hex(oled_ssd1306.addr))
oled_ssd1306.fill(1) #all on
oled_ssd1306.show()
time.sleep(1)
oled_ssd1306.fill(0) #all off
oled_ssd1306.show()
time.sleep(1)
def splitLine(src, cnt):
return [src[i:i+cnt] for i in range(0, len(src), cnt)]
oled_ssd1306.text('Hello, World!', 0, 0, 1)
oled_ssd1306.text(str(usys.implementation[0]), 0, 20, 1)
oled_ssd1306.text(str(uos.uname()[2]), 0, 30, 1)
line_y = 40
for l in splitLine(str(uos.uname()[4]), 15):
oled_ssd1306.text(l, 0, line_y, 1)
line_y += 10
oled_ssd1306.show()
time.sleep(1)
oled_ssd1306.invert(1)
oled_ssd1306.show()
time.sleep(0.5)
oled_ssd1306.invert(0)
oled_ssd1306.show()
time.sleep(0.5)
oled_ssd1306.rect(15, 15, oled_ssd1306.width-30,
oled_ssd1306.height-30, 1)
oled_ssd1306.show()
time.sleep(1)
for x in range(oled_ssd1306.width):
oled_ssd1306.scroll(1, 0)
oled_ssd1306.show()
time.sleep_ms(50)
oled_ssd1306.fill_rect(15, 15, oled_ssd1306.width-30,
oled_ssd1306.height-30, 1)
oled_ssd1306.show()
time.sleep(0.5)
oled_ssd1306.text('~ bye ~', 55, 55, 1)
oled_ssd1306.show()
time.sleep(0.5)
print("~ bye ~")
mpyESP32C3_ssd1306_pixel.py
"""
MicroPython/ESP32C3 exercise run on ESP32-C3-DevKitM-1,
work with SSD1306 I2C OLED.
Draw randow pixels
"""
import uos
import usys
import machine
import time
import urandom
import ssd1306
print("====================================")
print(usys.implementation[0], uos.uname()[3],
"\nrun on", uos.uname()[4])
print("====================================")
oled_i2c = machine.I2C(0)
print("Default I2C:", oled_i2c)
oled_ssd1306 = ssd1306.SSD1306_I2C(128, 64, oled_i2c)
print("Default SSD1306 I2C address:",
oled_ssd1306.addr, "/", hex(oled_ssd1306.addr))
def drawRandomPixels():
for i in range(500):
rx = urandom.randint(0, oled_ssd1306.width)
ry = urandom.randint(0, oled_ssd1306.height)
oled_ssd1306.pixel(rx, ry, 1)
oled_ssd1306.show()
oled_ssd1306.fill(0)
oled_ssd1306.text('Random Pixels', 0, 0, 1)
oled_ssd1306.show()
drawRandomPixels()
oled_ssd1306.fill(0)
oled_ssd1306.text('Random Pixels', 0, 0, 1)
oled_ssd1306.invert(1)
oled_ssd1306.show()
drawRandomPixels()
print("~ bye ~")
mpyESP32C3_ssd1306_text.py
"""
MicroPython/ESP32C3 exercise run on ESP32-C3-DevKitM-1,
work with SSD1306 I2C OLED.
Display scrolling text (read from /boot.py) on OLED.
"""
import uos
import usys
import machine
import time
import ssd1306
# Read and display /boot.py file
def readBoot():
print("========================")
fileBoot = "/boot.py"
print(fileBoot)
print("========================")
with open(fileBoot, "r") as f:
line = f.readline()
while line != '':
line = line.rstrip() #strip trailing whitespace
print(line)
oled_ssd1306.scroll(0, -10)
oled_ssd1306.text(line, 0, 46, 1)
oled_ssd1306.show()
time.sleep(0.5)
line = f.readline()
print("========================")
print("====================================")
print(usys.implementation[0], uos.uname()[3],
"\nrun on", uos.uname()[4])
print("====================================")
oled_i2c = machine.I2C(0)
print("Default I2C:", oled_i2c)
oled_ssd1306 = ssd1306.SSD1306_I2C(128, 64, oled_i2c)
print("Default SSD1306 I2C address:",
oled_ssd1306.addr, "/", hex(oled_ssd1306.addr))
oled_ssd1306.text('Hello, World!', 0, 0, 1)
oled_ssd1306.show()
readBoot()
print("~ bye ~")
No comments:
Post a Comment