SSD1306 SoftSPI (rewrite from CPP)

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
radimnov
Posts: 3
Joined: Wed Mar 17, 2021 5:13 pm

SSD1306 SoftSPI (rewrite from CPP)

Post by radimnov » Wed Mar 17, 2021 6:00 pm

Hi,

I have a NodeMCU V3 with SSD1306 OLED 0.96" 6 Pin display. It's already wired up on a PCB so I can't change the pins.
There is an existing CPP app written by someone else. It works but I'd like to make non-trivial changes so I'd rather write it again in Micropython. Everything works but I'm struggling with the display.

The display has GND VCC SCL SDA RES DC pins connected to NodeMCU as follows:

Code: Select all

SCL - D0 - GPIO16
SDA - D3 - GPIO0
RES - D2 - GPIO4
DC -  D1 - GPIO5
The CPP app uses the following to initialize the display:

Code: Select all

// Adafruit_SSD1306(int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin, int8_t rst_pin, int8_t cs_pin)
display = new Adafruit_SSD1306(D3, D0, D1, D2, -1);
Checking the Adafruit_SSD1306 code I found the following constructor matches what's used by the app:
https://github.com/adafruit/Adafruit_SS ... 6.cpp#L253

I have tried to set up SoftSPI and SSD1306 python library but without any success. I don't know how to set up the SoftSPI if there is no miso and how to set up SSD1306 if there is no CS pin :(

Does anyone know how to set up the SSD1306 display connected using the pinout mentioned above in Micropython?

Many thanks

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: SSD1306 SoftSPI (rewrite from CPP)

Post by Roberthh » Wed Mar 17, 2021 7:25 pm

The pin labels you mention belong to a I2C interface, not SPI. In any case, for suing the SSD1306 with MicroPython you can use the provided driver: https://github.com/micropython/micropyt ... ssd1306.py
It supports both SPI and I2C, and it works.

radimnov
Posts: 3
Joined: Wed Mar 17, 2021 5:13 pm

Re: SSD1306 SoftSPI (rewrite from CPP)

Post by radimnov » Thu Mar 18, 2021 8:25 am

Thank you for your reply. I must admit I don't really understand the hardware so it's a try/fail.

I think it should be SPI one as the original Adafruit constructor mentions it's a "constructor for SPI SSD1306 displays".
I've tried I2C anyway but it still does not work. I believe it works as it's widely used. It's probably something unusual in my setup, maybe the design is too old/simple and the current software interface does not support it?

Code: Select all

>>> from machine import Pin, I2C
>>> i2c = I2C(scl=Pin(16), sda=Pin(0))
>>> i2c.scan()
[]
>>> disp = SSD1306_I2C(128, 64, i2c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ssd1306.py", line 110, in __init__
  File "ssd1306.py", line 36, in __init__
  File "ssd1306.py", line 71, in init_display
  File "ssd1306.py", line 115, in write_cmd
OSError: [Errno 19] ENODEV
Thanks again

radimnov
Posts: 3
Joined: Wed Mar 17, 2021 5:13 pm

Re: SSD1306 SoftSPI (rewrite from CPP)

Post by radimnov » Thu Mar 18, 2021 3:18 pm

Finally got it working with help of viewtopic.php?t=3453 and also confirming I'm doing it right (SoftSPI - 4 wire SPI) from the attached datasheet.

Code: Select all

# Pin(16) is connected to SCL/CLK
# Pin(0) is connected to SDA/MOSI
# Pin(5) is connected to DC
# Pin(4) is connected to RES
# Pin(2) is not connected anywhere
from machine import Pin, SoftSPI
import ssd1306
spi = SoftSPI(sck=Pin(16), mosi=Pin(0), miso=Pin(2))
disp = ssd1306.SSD1306_SPI(128, 64, spi, Pin(5), Pin(4), Pin(2))
disp.init_display()
disp.text('Thank you', 0, 0)
disp.text('Roberthh', 0, 20)
disp.show()
Attachments
ER-OLEDM0.96-1_Series_Arduino-Interfacing.jpg
ER-OLEDM0.96-1_Series_Arduino-Interfacing.jpg (234.64 KiB) Viewed 2088 times

Post Reply