Editing OLED LCD Display

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 12: Line 12:
  
 
= How to control it in MicroPython =
 
= How to control it in MicroPython =
<syntaxhighlight lang="python" line='line'>
 
from machine import Pin, I2C, SoftI2C
 
from time import sleep
 
 
#comment/uncomment the one for you version or change the pins if you use a different setup
 
 
##for ESP32 - version 1,2 (WIFI KIT, Heltech)
 
#from ssd1306 import SSD1306_I2C
 
#i2c = I2C(scl=Pin(15), sda=Pin(4))
 
#pin16 = Pin(16, Pin.OUT)
 
#pin16.on()
 
 
#for ESP32-S3 - version 3 (WIFI KIT, Heltech)
 
from ssd1306v03 import SSD1306_I2C
 
i2c = SoftI2C(scl=Pin(18), sda=Pin(17))
 
 
oled_width = 128
 
oled_height = 64
 
oled = SSD1306_I2C(oled_width,oled_height,i2c)
 
 
# switch the display off, but it's still active
 
oled.poweroff()
 
 
# switch the display on again
 
oled.poweron()
 
 
# contrast in range 0-255 (0=darker)
 
oled.contrast(contrast)
 
 
# invert in True/False
 
oled.invert(invert)
 
 
# content will be shown, has to be called when something changes
 
oled.show()
 
 
 
# Graphics
 
# fill the screen in color c (0=dark, 1=bright)
 
oled.fill(c)
 
 
# set pixel at position x,y to color c
 
oled.pixel(x,y,c)
 
 
# returns color of pixel x,y
 
oled.pixel(x,y)
 
 
# draws a horizontal line from x,y, length=w in color c
 
oled.hline(x,y,w,c)
 
 
# draws a vertical line from x,y, length=w in color c
 
oled.vline(x,y,w,c)
 
 
# draws a line from x1,y1 to x2,y2 in color c
 
oled.line(x1,y1,x2,y2,c)
 
 
# draws a rectangle at position x,y with width=w and height=h in color c
 
oled.rect(x,y,w,h,c)
 
 
# draws a filled rectangle at position x,y with width=w and height=h in color c
 
oled.fill_rect(x,y,w,h,c)
 
 
 
# Text
 
# set text s at pixel x,y
 
oled.text(s,x,y,c)
 
 
# line = line number (0-5)
 
# pos = horizontal position (0-15)
 
def text_line(text, line, pos=0):
 
  x = 10*pos
 
  y = line*11
 
  oled.text(text,x,y)
 
  oled.show()
 
 
# don't forget to show!
 
oled.show()
 
</syntaxhighlight>
 
 
 
[[File:DisplayPixel.PNG|600px]]
 
[[File:DisplayLinePos.PNG|600px]]
 
 
= A small Program in MicroPython (for ESP32-S3, Heltec WIFI-Kit version 3) =
 
<syntaxhighlight lang="python" line='line'>
 
from machine import Pin, SoftI2C
 
from ssd1306v03 import SSD1306_I2C
 
from time import sleep
 
 
i2c = SoftI2C(scl=Pin(18), sda=Pin(17))
 
 
oled_width = 128
 
oled_height = 64
 
oled = SSD1306_I2C(oled_width,oled_height,i2c)
 
 
# line = line number (0-5)
 
# pos = horizontal position (0-15)
 
def text_line(text, line, pos=0):
 
  x = 10*pos
 
  y = line*11
 
  oled.text(text,x,y)
 
  oled.show()
 
 
 
# set text
 
oled.text("Hello",0,0)
 
# horizontal line from x=10, y=50, length=70, color bright
 
oled.hline(10,50,70,1)
 
# display it
 
oled.show()
 
sleep(3)
 
 
# delete all/ set all to dark
 
oled.fill(0)
 
# don't forget to show/display changes
 
oled.show()
 
sleep(1)
 
 
# set new text
 
oled.text("Hi",0,0)
 
# filled rectangle at x=50, y=20, width=20, height=20, color bright
 
oled.fill_rect(50,20,20,20,1)
 
# inverts colors of display
 
oled.invert(True)
 
oled.show()
 
</syntaxhighlight>
 
 
= How to control it in MicroPython (ESP32, Version 1, 2) =
 
 
<syntaxhighlight lang="python" line='line'>
 
<syntaxhighlight lang="python" line='line'>
 
from machine import Pin, I2C
 
from machine import Pin, I2C
Line 255: Line 129:
 
oled.show()
 
oled.show()
 
</syntaxhighlight>
 
</syntaxhighlight>
 
  
 
text
 
text

Please note that all contributions to Sketching with Hardware at LMU Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see My wiki:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)