SPI SSD1306

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
sjp770
Posts: 7
Joined: Thu Aug 24, 2017 12:50 am

SPI SSD1306

Post by sjp770 » Thu Nov 16, 2017 11:57 pm

So it looks like Hardware SPI is working in the latest release, but I am having trouble getting the ssd1306 driver to work:

I'm following this guide: https://learn.adafruit.com/micropython- ... icropython
But it may be out of date by now? If i use ampy to put the ssd1306.py file on the esp32 I can import it but when I try the following i get an error.

Code: Select all

>>> import machine
>>> import ssd1306
>>> spi = machine.SPI(1, baudrate=8000000, polarity=0, phase=0)
>>> oled = ssd1306.SSD1306_SPI(128, 32, spi, machine.Pin(15), machine.Pin(0), machine.Pin(16))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SSD1306_SPI'

muth02446
Posts: 4
Joined: Sun Nov 12, 2017 2:28 pm

Re: SPI SSD1306

Post by muth02446 » Tue Nov 21, 2017 4:40 am

I am in a similar situation - I'd like to use a TFT Display with a ili9341 controller.
There are drivers here:
https://github.com/adafruit/micropython ... gb-display

What I struggle with is how to connect the display to the esp32.
Base on this pin layout:
http://www.shenzhen2u.com/image/catalog ... 2s_pin.png

I tried:

spi = machine.SPI(1,
baudrate=8000000,
mosi=machine.Pin(23, machine.Pin.OUT), #mosi
#miso=machine.Pin(19, machine.Pin.IN), # miso
sck=machine.Pin(18, machine.Pin.OUT)) # sclk
display = ili9341.ILI9341(spi,
cs=machine.Pin(17, machine.Pin.OUT), # cs
dc=machine.Pin(21, machine.Pin.OUT), # dc
rst=machine.Pin(22, machine.Pin.OUT), # rst
width=320,
height=240)

is that a plausible pin assignment?

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: SPI SSD1306

Post by rdagger » Wed Nov 22, 2017 5:44 pm

muth02446 wrote:
Tue Nov 21, 2017 4:40 am
I am in a similar situation - I'd like to use a TFT Display with a ili9341 controller.
There are drivers here:
https://github.com/adafruit/micropython ... gb-display

What I struggle with is how to connect the display to the esp32.
Base on this pin layout:
http://www.shenzhen2u.com/image/catalog ... 2s_pin.png

I tried:

spi = machine.SPI(1,
baudrate=8000000,
mosi=machine.Pin(23, machine.Pin.OUT), #mosi
#miso=machine.Pin(19, machine.Pin.IN), # miso
sck=machine.Pin(18, machine.Pin.OUT)) # sclk
display = ili9341.ILI9341(spi,
cs=machine.Pin(17, machine.Pin.OUT), # cs
dc=machine.Pin(21, machine.Pin.OUT), # dc
rst=machine.Pin(22, machine.Pin.OUT), # rst
width=320,
height=240)

is that a plausible pin assignment?
If you are going to specify 23 and 18 for MOSI and SCK respectively then you should use bus 2 to get hardware SPI.
Bus 1: HSPI is MOSI=GPIO13, MISO=GPIO12 and SCK=GPIO14
Bus 2: VSPI is MOSI=GPIO23, MISO=GPIO19 and SCK=GPIO18
Also I don't think you need to specify the pin direction with SPI. You can probably increase the baud rate too.

Code: Select all

from machine import Pin, SPI
spi = SPI(2, baudrate=8000000, sck=Pin(18), mosi=Pin(23))
I don't see anything wrong with using 17, 21 and 22 for CS, DC and RST respectively. I don't have an ili9341 but I have successfully tested an SSD1351 using your pin selections.

chelsea
Posts: 2
Joined: Sun Dec 10, 2017 10:09 pm
Location: France

Re: SPI SSD1306

Post by chelsea » Mon Dec 11, 2017 8:21 pm

Hello,

I have an equivalent problem:
on my ESP8266 with micropython my SSD1306-powered OLED works fine.
but if I copy the ssd1306.py (https://raw.githubusercontent.com/micro ... ssd1306.py) on my ESP32 the OLED keeps dark.

I run code like this:
[code]
import framebuf
import ssd1306
from machine import SPI, Pin

res =Pin(2)
dc = Pin(4)
ss = Pin(15)
oled = ssd1306.SSD1306_SPI(128,64,SPI(1),dc,res,ss,False)

# lighten up:
oled.fill(1)
oled.show()
[/code]
There comes no error, but on the ESP32 the OLED will not filled :-(
It is the same OLED. On the ESP32 i have tryed
SPI(1) / HSPI with mosi=Pin(23), clk=Pin(18), ss=Pin(5)
SPI(2) / VSPI with mosi=Pin(13),clk=Pin(14)

My ESP32 is a DevKit32 (https://bsfrance.fr/iot-sans-fil/1294-B ... -LoRa.html)

Perhaps somebody could help me....thankyou

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: SPI SSD1306

Post by rdagger » Tue Dec 12, 2017 5:59 pm

chelsea wrote:
Mon Dec 11, 2017 8:21 pm
Hello,

I have an equivalent problem:
on my ESP8266 with micropython my SSD1306-powered OLED works fine.
but if I copy the ssd1306.py (https://raw.githubusercontent.com/micro ... ssd1306.py) on my ESP32 the OLED keeps dark.

I run code like this:

Code: Select all

import framebuf
import ssd1306
from machine import SPI, Pin

res =Pin(2) 
dc = Pin(4)
ss = Pin(15)
oled = ssd1306.SSD1306_SPI(128,64,SPI(1),dc,res,ss,False)

# lighten up:
oled.fill(1)
oled.show()
There comes no error, but on the ESP32 the OLED will not filled :-(
It is the same OLED. On the ESP32 i have tryed
SPI(1) / HSPI with mosi=Pin(23), clk=Pin(18), ss=Pin(5)
SPI(2) / VSPI with mosi=Pin(13),clk=Pin(14)

My ESP32 is a DevKit32 (https://bsfrance.fr/iot-sans-fil/1294-B ... -LoRa.html)

Perhaps somebody could help me....thankyou
You need to specify the mosi and sck parameters when you instantiate your SPI.
For example:

Code: Select all

spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23))

chelsea
Posts: 2
Joined: Sun Dec 10, 2017 10:09 pm
Location: France

Re: SPI SSD1306

Post by chelsea » Thu Dec 14, 2017 10:25 pm

merci,

that's the solution for my problem. i've working it :-)
But perhaps you can explain me the difference? In my opinion the spi-ports are specified by hardware or can I also use some other pins for spi?
Is there any documentation like http://docs.micropython.org/en/latest/esp8266/ for the esp32?

thank-vous

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: SPI SSD1306

Post by rdagger » Mon Dec 18, 2017 5:25 pm

chelsea wrote:
Thu Dec 14, 2017 10:25 pm
merci,

that's the solution for my problem. i've working it :-)
But perhaps you can explain me the difference? In my opinion the spi-ports are specified by hardware or can I also use some other pins for spi?
Is there any documentation like http://docs.micropython.org/en/latest/esp8266/ for the esp32?

thank-vous
I’m not aware of any ESP32 specific SPI docs. I’ve been using the ESP8266 docs.

The ESP32 has 4 hardware SPI buses but 2 are reserved. Therefore, you currently can only access 2 which are called HSPI = 1 and VSPI =2. This may change in the future. Furthermore, VSPI can be reserved if you are using psRAM.
The hardware busses are often referred to as “hosts”. HSPI uses 14, 13 and 12 for clock, MOSI and MISO respectively. VSPI uses 18, 23 and 19 for clock, MOSI and MISO. It’s my understanding that the CS pins are controlled by software in MicroPython. Therefore, you don’t get any benefit from using the designated CS hardware pins.

You don’t have to use the hardware pins for clock, MOSI and MISO. However, if you use alternative pins I believe this limits your baud rate to 20MHz.

There was an issue where the SPI buffer limits writes to around 4096 bytes. This may have been resolved.
I’ve gleaned what I’ve needed primarily from this forum. Hopefully the experts here will correct any mistakes.

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: SPI SSD1306

Post by mattyt » Tue Dec 19, 2017 12:07 pm

That's all in-line with my understanding of the ESP32 SPI functionality.

It looks like a fix for the HW SPI fixed-length buffer issue was merged yesterday. I haven't had a chance to try the fix yet.

Also of note (since I referenced ESP32-specific issues!) is that the ESP32 port used to be developed as a fork but has recently been merged upstream. The ESP32 port is now in the ports directory and the fork should no longer be used.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: SPI SSD1306

Post by OutoftheBOTS_ » Fri Dec 22, 2017 10:25 am

I am using a ili9341 screen on my new ESP32. I am using a driver that I originally wrote for the esp8266 see https://github.com/OutOfTheBots/ili9341-tft

I have run it on my ESP32 WROVER board but changed the pins used from the ESP8266 needed pins to ESP32 pins 12,13,14. It gibes me an error of I try to use HSPI bus 1 or 2 as it says they are already in use. If I don't specify a bus then it runs fine, I assume it is using software SPI though. If I time how fast it runs my graphics routines of loading images and putting text as in my text_example.py on my github then change the used pins to some other pins it runs at the same speed, I assume this also indicates that I am using software SPI.

Is there a way that I can use 80mhz HSPI on a WROVER board that is using psRAM??

My driver ATM only can load full screen size images as background and place and remove and scale text for the foreground but I plan on porting across my drivers for the touch chip and GUI buttons for the touch as well as some GUI menus that will operate by touch. People can feel free to use then. For a demo see it here running on a ESP8266 https://youtu.be/xjEsYF2QsIg

maiky
Posts: 1
Joined: Tue Dec 24, 2019 8:27 pm

Re: SPI SSD1306

Post by maiky » Tue Dec 24, 2019 8:36 pm

Hello Chelsea!

We are using the SPI SSD1306 display, and it works perfectly for us.

import framebuf
import ssd1306
from machine import SPI, Pin

res =Pin(2)
dc = Pin(4)
ss = Pin(15)
oled = ssd1306.SSD1306_SPI(128,64,SPI(1),dc,res,ss,False)

# lighten up:
oled.fill(1)
oled.show()

The connection diagram, I found in the following link,

http://arduino-er.blogspot.com/2016/06/ ... 28x64.html

  which is for arduino, but it worked for micropython

Post Reply