Skip to content

Oled Display

A class to control an OLED display using I2C.

Source code in wsd_esp32\oled_display.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class OledDisplay:
    """
    A class to control an OLED display using I2C.
    """

    def __init__(self, sda_pin=21, scl_pin=22, freq=400000, width=128, height=32):
        """
        Initializes the OLED display.

        Parameters:
            sda_pin (int): The GPIO pin number for the I2C data line (SDA). Default is 21.
            scl_pin (int): The GPIO pin number for the I2C clock line (SCL). Default is 22.
            freq (int): The frequency for the I2C communication in Hz. Default is 400,000.
            width (int): The width of the OLED display in pixels. Default is 128.
            height (int): The height of the OLED display in pixels. Default is 32.
        """
        # Initialize I2C and OLED display
        self.i2c = I2C(0, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=freq)
        self.width = width
        self.height = height
        self.display = ssd1306.SSD1306_I2C(width, height, self.i2c)
        self.clear()

    def clear(self):
        """
        Clears the OLED display by filling it with black pixels.
        """
        self.display.fill(0)
        self.display.show()

    def write(self, text, x=0, y=0):
        """
        Displays text on the OLED screen.

        Parameters:
            text (str): The text to display on the screen.
            x (int): The x-coordinate (horizontal position) where the text starts. Default is 0.
            y (int): The y-coordinate (vertical position) where the text starts. Default is 0.
        """
        self.display.text(text, x, y)
        self.display.show()

    def demo(self):
        """
        Runs a simple demo to showcase the OLED display functionality.
        """
        self.clear()
        self.write("Hello, World!", 0, 0)
        time.sleep(2)
        self.clear()
        self.write("Demo Complete!", 0, 10)
        time.sleep(2)

__init__(sda_pin=21, scl_pin=22, freq=400000, width=128, height=32)

Initializes the OLED display.

Parameters:

Name Type Description Default
sda_pin int

The GPIO pin number for the I2C data line (SDA). Default is 21.

21
scl_pin int

The GPIO pin number for the I2C clock line (SCL). Default is 22.

22
freq int

The frequency for the I2C communication in Hz. Default is 400,000.

400000
width int

The width of the OLED display in pixels. Default is 128.

128
height int

The height of the OLED display in pixels. Default is 32.

32
Source code in wsd_esp32\oled_display.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def __init__(self, sda_pin=21, scl_pin=22, freq=400000, width=128, height=32):
    """
    Initializes the OLED display.

    Parameters:
        sda_pin (int): The GPIO pin number for the I2C data line (SDA). Default is 21.
        scl_pin (int): The GPIO pin number for the I2C clock line (SCL). Default is 22.
        freq (int): The frequency for the I2C communication in Hz. Default is 400,000.
        width (int): The width of the OLED display in pixels. Default is 128.
        height (int): The height of the OLED display in pixels. Default is 32.
    """
    # Initialize I2C and OLED display
    self.i2c = I2C(0, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=freq)
    self.width = width
    self.height = height
    self.display = ssd1306.SSD1306_I2C(width, height, self.i2c)
    self.clear()

clear()

Clears the OLED display by filling it with black pixels.

Source code in wsd_esp32\oled_display.py
28
29
30
31
32
33
def clear(self):
    """
    Clears the OLED display by filling it with black pixels.
    """
    self.display.fill(0)
    self.display.show()

write(text, x=0, y=0)

Displays text on the OLED screen.

Parameters:

Name Type Description Default
text str

The text to display on the screen.

required
x int

The x-coordinate (horizontal position) where the text starts. Default is 0.

0
y int

The y-coordinate (vertical position) where the text starts. Default is 0.

0
Source code in wsd_esp32\oled_display.py
35
36
37
38
39
40
41
42
43
44
45
def write(self, text, x=0, y=0):
    """
    Displays text on the OLED screen.

    Parameters:
        text (str): The text to display on the screen.
        x (int): The x-coordinate (horizontal position) where the text starts. Default is 0.
        y (int): The y-coordinate (vertical position) where the text starts. Default is 0.
    """
    self.display.text(text, x, y)
    self.display.show()

demo()

Runs a simple demo to showcase the OLED display functionality.

Source code in wsd_esp32\oled_display.py
47
48
49
50
51
52
53
54
55
56
def demo(self):
    """
    Runs a simple demo to showcase the OLED display functionality.
    """
    self.clear()
    self.write("Hello, World!", 0, 0)
    time.sleep(2)
    self.clear()
    self.write("Demo Complete!", 0, 10)
    time.sleep(2)