OLED mini-display for ESP32

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Capstan
Posts: 117
Joined: Sun Jan 29, 2017 4:03 pm
Location: Texas, USA

OLED mini-display for ESP32

Post by Capstan » Thu Sep 07, 2017 8:23 pm

I got a little I2C display device, wired it up to an Adafruit ESP32 Feather board flashed with Micropython, and can display text on it! The display appears to use very little power, so I was able to just drive it with the 3.3v output pin from the Feather (labelled BAT) which gets power from the USB/serial cable. I attached 10k pull ups to SDA and SCL, had inconsistent behavior without them.

Image

This is the device; https://smile.amazon.com/gp/product/B07 ... UTF8&psc=1

Got the driver code from here; https://github.com/adafruit/micropython ... it-ssd1306

And here's the code I wrote that writes the text;

Code: Select all

from machine import Pin, I2C
from SSD1306 import SSD1306, SSD1306_I2C

esp32i2cPins = {'sda': 23, 'scl': 22}
frequency=100000

sclPin=esp32i2cPins['scl']
sdaPin=esp32i2cPins['sda']
i2c = I2C(scl=Pin(sclPin), sda=Pin(sdaPin), freq = frequency)
        
devices = i2c.scan()
print(devices)   # should see [60]

ssd = SSD1306_I2C(width=128, height=64, i2c=i2c, external_vcc=False)

ssd.init_display()

ssd.text('hello', 0, 0)
ssd.text('world', 0, 20)
ssd.show()

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: OLED mini-display for ESP32

Post by pythoncoder » Fri Sep 08, 2017 7:38 am

Pullups are essential for I2C - I'm surprised it worked at all without them. If you want to know the reason it's explained here https://en.wikipedia.org/wiki/I%C2%B2C.

There is an official SSD1306 driver here https://github.com/micropython/micropyt ... ssd1306.py.

If you're interested in displaying different (larger) fonts you might like to look at this driver https://github.com/peterhinch/micropyth ... er/SSD1306.
Peter Hinch
Index to my micropython libraries.

Capstan
Posts: 117
Joined: Sun Jan 29, 2017 4:03 pm
Location: Texas, USA

Re: OLED mini-display for ESP32

Post by Capstan » Fri Sep 08, 2017 3:16 pm

Thanks for the links, larger/different fonts would definitely be nice. I'll experiment with it.

I've found that some I2C devices incorporate their own pull ups, so I try with and without them. This display actually did print characters without, but then threw ENODEV.

slzatz
Posts: 92
Joined: Mon Feb 09, 2015 1:09 am

Re: OLED mini-display for ESP32

Post by slzatz » Sat Sep 09, 2017 8:40 pm

When I try to use the standard ssd1306 driver with Loboris' ESP32 port, I get the following Traceback:

Code: Select all

>>> ssd = SSD1306_I2C(128, 32, i2c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ssd1306_official.py", line 109, in __init__
  File "ssd1306_official.py", line 37, in __init__
  File "ssd1306_official.py", line 64, in init_display
  File "ssd1306_official.py", line 89, in show
  File "ssd1306_official.py", line 119, in write_data
AttributeError: 'HWI2C' object has no attribute 'start'
>>>
With the Adafruit driver at https://github.com/adafruit/micropython ... it-ssd1306, it does work. The only difference from Capstan's code is that I get an error when specifying the frequency when creating the i2c object, but it works fine when I omit the frequency.

Steve

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: OLED mini-display for ESP32

Post by loboris » Sat Sep 09, 2017 11:09 pm

slzatz wrote:When I try to use the standard ssd1306 driver with Loboris' ESP32 port, I get the following Traceback...
... I get an error when specifying the frequency when creating the i2c object...
The correct way to create i2c object in my MicroPython port is:

Code: Select all

i2c=machine.I2C(scl=machine.Pin(26),sda=machine.Pin(25),speed=400000)
Default speed (frequency), if not specified, is 100000 Hz.
Only the hardware I2C is available. Internal pull-up resistors are enabled on initialization, so often it can work without external pullups. External pull-up resistors are still recommended.

Available methods are:
init, deinit, scan, readfrom, readfrom_into, writeto, readfrom_mem, readfrom_mem_into, writeto_mem

slzatz
Posts: 92
Joined: Mon Feb 09, 2015 1:09 am

Re: OLED mini-display for ESP32

Post by slzatz » Sat Sep 09, 2017 11:38 pm

@loboris -- thanks. For some reason, I had thought that the i2c constructor parameter was "frequency" and didn't realize it was "speed" -- my mistake.

Somewhat related:

When I try to use the ssd1306 driver you've included as a frozen module (? whether it's the "official" driver or one you've modified), when I construct the oled driver object like this:

Code: Select all

ssd = SSD1306_I2C (width=128, height=32, i2c=i2c, external_vcc=False)
I get:

Code: Select all

AttributeError: 'HWI2C' object has no attribute 'start'
I get the same error with the official MicroPython driver but not the one located at: https://github.com/adafruit/micropython ... it-ssd1306

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: OLED mini-display for ESP32

Post by loboris » Sun Sep 10, 2017 8:46 am

@slzatz
self.i2c.start() & self.i2c.stop() must be removed from ssd1306.py. They are not needed in this driver.
Sorry I forgot to do that. I've only tested with SPI interface, I'll do some tests with I2C today...

I'm planning to include some low level i2c methods including start and stop...

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: OLED mini-display for ESP32

Post by deshipu » Mon Sep 11, 2017 9:50 am

loboris wrote: The correct way to create i2c object in my MicroPython port is:

Code: Select all

i2c=machine.I2C(scl=machine.Pin(26),sda=machine.Pin(25),speed=400000)
Default speed (frequency), if not specified, is 100000 Hz.
Only the hardware I2C is available. Internal pull-up resistors are enabled on initialization, so often it can work without external pullups. External pull-up resistors are still recommended.

Available methods are:
init, deinit, scan, readfrom, readfrom_into, writeto, readfrom_mem, readfrom_mem_into, writeto_mem
Have you considered following the same API conventions as all the other MicroPython ports use, so as to make all the existing libraries work? Otherwise, perhaps it could be a good idea to call it something else than "MicroPython"?

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: OLED mini-display for ESP32

Post by loboris » Mon Sep 11, 2017 4:22 pm

@deshipu

I could change speed to freq, I don't think it is very important (by the way, "clock speed" is commonly used in i2c terminology...).
Driver's parameters names are in no way part of any (Micro)Python "standard".
What makes MicroPython "MicroPython" is the core of the Python language implementation, at least it is my understanding...

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: OLED mini-display for ESP32

Post by pythoncoder » Tue Sep 12, 2017 6:34 am

The issue here is that we have a substantial body of user-contributed unofficial device drivers. These are in wide use, often by relatively inexperienced users. Where possible we write them for portability using libraries such as machine. API changes cause problems both for the users and the authors of such drivers.
Peter Hinch
Index to my micropython libraries.

Post Reply