SH1106-based OLED driver

This module provides an interface for controlling an OLED display using the SH1106 driver over the I2C protocol. It allows users to control the display’s power, contrast, and pixel data, as well as render text and images. It inherits from the framebuf.FrameBuffer class to enable drawing on the display’s buffer and updating the OLED screen.

Example

from sh1106 import SH1106_I2C

# Init OLED display
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400_000)
oled = SH1106_I2C(i2c)

# Add some text at (x, y)
oled.text("Using OLED and", 0, 40)
oled.text("ESP32", 50, 50)

# Update the OLED display so the text is displayed
oled.show()

Authors:

  • Shujen Chen et al., Raspberry Pi Pico Interfacing and Programming with MicroPython

  • MicroPython SH1106 OLED driver, I2C and SPI interfaces

  • Tomas Fryza

Modification history

  • 2024-11-11 : Added Sphinx-style comments for documentation.

  • 2024-11-02 : Added demo method to demonstrate usage of the display.

  • 2023-10-27 : File created, initial release.

class sh1106.SH1106_I2C(*args: Any, **kwargs: Any)[source]

Bases: FrameBuffer

DEV_ADDR = 60[source]
HEIGHT = 64[source]
HIGH_COLUMN_ADDR = 16[source]
LOW_COLUMN_ADDR = 0[source]
PAGES = 8[source]
PAGE_ADDRESS = 176[source]
WIDTH = 128[source]
contrast(val)[source]

Set the contrast of the OLED display.

Parameters:

val – Contrast value (0 to 255).

poweroff()[source]

Turn off the OLED display.

poweron()[source]

Turn on the OLED display.

show()[source]

Refresh the OLED display with the current buffer data.

sleep(value)[source]

Put the OLED display into sleep mode or wake it up.

write_cmd(cmd)[source]

Write a command byte to the SH1106 OLED display.

Parameters:

cmd – The command byte to be sent to the display.

write_data(data)[source]

Write a data buffer to the SH1106 OLED display.

Parameters:

data – A byte array containing the data to be sent.

sh1106.demo()[source]

Demo function to showcase the usage of the SH1106 OLED display.