Adsense HTML/JavaScript

Saturday, December 12, 2020

nanoESP32-S2/CircuitPython: I2C and 128x64 SSD1306 OLED

Trying I2C on nanoESP32-S2/CircuitPython, and display on 128x64 OLED with I2C SSD1306 driver (3.3V), using adafruit_ssd1306.mpy and adafruit_framebuf.mpy of Adafruit CircuitPython Library Bundle.

Connection between nanoESP32-S2 and 128x64 I2C SSD1306 OLED:

ESP32-S2 - OLED
3V3 VCC
GND GND
IO40           SDA
IO41           SCL 

Exercise to 128x64 OLED with I2C SSD1306 driver:

import time
import os
import board
import busio
import adafruit_ssd1306
# lib needed:
# adafruit_ssd1306.mpy
# adafruit_framebuf.mpy

# Helper function to draw a circle from a given position with a given radius
# This is an implementation of the midpoint circle algorithm,
# see https://en.wikipedia.org/wiki/Midpoint_circle_algorithm#C_example for details
def draw_circle(xpos0, ypos0, rad, col=1):
    x = rad - 1
    y = 0
    dx = 1
    dy = 1
    err = dx - (rad << 1)
    while x >= y:
        display.pixel(xpos0 + x, ypos0 + y, col)
        display.pixel(xpos0 + y, ypos0 + x, col)
        display.pixel(xpos0 - y, ypos0 + x, col)
        display.pixel(xpos0 - x, ypos0 + y, col)
        display.pixel(xpos0 - x, ypos0 - y, col)
        display.pixel(xpos0 - y, ypos0 - x, col)
        display.pixel(xpos0 + y, ypos0 - x, col)
        display.pixel(xpos0 + x, ypos0 - y, col)
        if err <= 0:
            y += 1
            err += dy
            dy += 2
        if err > 0:
            x -= 1
            dx += 2
            err += dx - (rad << 1)
    display.show()

print("==============================")
print(os.uname())
print("Hello nanoESP32-S2/CircuitPython ssd1306 OLED example")
print(adafruit_ssd1306.__name__ + "version: " + adafruit_ssd1306.__version__)
print()

WIDTH = 128
HEIGHT = 64
CENTER_X = int(WIDTH/2)
CENTER_Y = int(HEIGHT/2)

SDA = board.IO40
SCL = board.IO41
i2c = busio.I2C(SCL, SDA)

if(i2c.try_lock()):
    print("i2c.scan(): " + str(i2c.scan()))
    i2c.unlock()

display = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c)

display.fill(0)
display.show()
time.sleep(1.0)
display.fill(1)
display.show()
time.sleep(1.0)
display.fill(0)
display.show()
time.sleep(1.0)

for y in range(HEIGHT):
    for x in range(WIDTH):
        display.pixel(x, y, 1)
    display.show()
    
draw_circle(CENTER_X, CENTER_Y, 25, 0)

print("- bye -\n")

~ more exercises on nanoESP32-S2/CircuitPython.



Remark about ESP32-S2 pull-up resistor for I2C:

As mentioned in Espressif document ESP-IDF Programming Guide :  API Reference > Peripherals API > I2C DriverESP32-S2’s internal pull-ups are in the range of tens of kOhm, which is, in most cases, insufficient for use as I2C pull-ups. Users are advised to use external pull-ups with values described in the I2C specification.

As I understand, most I2C SSD1306 OLED module have pull-up resistors. It's no external resistor needed in this exercise.

Next:






No comments:

Post a Comment