Skip to content

Instantly share code, notes, and snippets.

@laurivosandi
Last active February 8, 2022 03:22
Show Gist options
  • Save laurivosandi/1dcc68aa1e665336f4867a4d81ce64b7 to your computer and use it in GitHub Desktop.
Save laurivosandi/1dcc68aa1e665336f4867a4d81ce64b7 to your computer and use it in GitHub Desktop.
OLED screen demo on ESP32 with MicroPython
# Pull the SDD1306 lib from here https://github.com/adafruit/micropython-adafruit-ssd1306/blob/master/ssd1306.py
from time import sleep_ms
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
buf = "wubba lubba dub dub "
i2c = I2C(-1, Pin(4),Pin(5),freq=40000) # Bitbanged I2C bus
assert 60 in i2c.scan(), "No OLED display detected!"
oled = SSD1306_I2C(128, 64, i2c)
oled.invert(0) # White text on black background
oled.contrast(255) # Maximum contrast
for j in range(0, 500):
oled.fill(0)
oled.text(buf[j%len(buf):]+buf, 10, 10)
oled.show()
sleep_ms(40)
@Bougakov
Copy link

Thanks for sharing this. It helped me to sort out the issue with "ESP32 OLED V2.0 TTGO" board I've got from AliExpress (it comes with a screen soldered). It uses pins 4 and 15 (not 5), and the screen's reset pin is 16 (I don't know why they chose such wiring).

My code:

# need to edit SDD1306 lib obtained from https://github.com/adafruit/micropython-adafruit-ssd1306/blob/master/ssd1306.py
# manually: change 3C address to 3D
from time import sleep_ms
from machine import Pin
p16 = Pin(16, Pin.OUT)
p16.value(1) # resets screen

buf = "wubba lubba dub dub  "

from machine import I2C
i2c = I2C(1, scl=Pin(15), sda=Pin(4), freq=400000)
# i2c.scan()
assert 60 in i2c.scan(), "No OLED display detected!"

import ssd1306
oled = ssd1306.SSD1306_I2C(128, 64, i2c, 60)
oled.invert(0) # White text on black background
oled.fill(0)
oled.contrast(255) # Maximum contrast

for j in range(0, 500):
    oled.fill(0)
    oled.text(buf[j%len(buf):]+buf, 10, 10)
    oled.show()
    sleep_ms(40)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment