010 - MicroPython TechNotes: 0.96 OLED Display

Introduction

In this article, we will look at 0.96 OLED display using the MicroPython language. OLED stands for Organic Light Emitting Diode is a self-illuminating so backlight is not needed and more power efficient.

Bill Of Materials

  1. ESP32 development board.
  2. Gorillacell ESP32 shield (this is optional, you can directly connect to ESP32 if you don’t have this)
  3. 4-pin female-female Dupont jumper wires.
  4. Gorillacell 0.96 OLED display module.

Pinout

  1. GND – for the ground pin.
  2. VCC – for the supply voltage.
  3. SDA – for the i2c serial data pin.
  4. SCL – for the i2c serial clock pin.

Hardware Instruction

  1. First, attach the ESP32 development board at the top of ESP32 shield making sure that the USB ports are on the same sides.
  2. Next, attach the dupont jumper wires to the OLED display according to the color coding which is black for the GND, red for the VCC, yellow for the SDA, and white for the SCL.
  3. Next, attach the other side of the dupont jumper wires to the ESP32 shield by matching the colors of the pin headers and the colors of the jumper wires which is black to black, red to red, yellow and following colors to the yellow. In this tutorial, I choose GPIO 21 for the SDA and GPIO 22 for the SCL.
  4. Next, power the ESP32 shield by attaching an external power supply with a type-C USB cable and make sure that the power switch is slide to the ON state.
  5. Next, connect the ESP32 to the computer by attaching a micro USB cable. The demo circuit should now be ready.

Software Instruction

  1. In order to easily use the 0.96 OLED display, we need an external driver library. Thankfully, there is an available library from Adafruit which is the SSD1306.py: https://github.com/adafruit/micropython-adafruit-ssd1306/blob. Copy and paste it to the Thonny Python IDE.
  2. Save it to ESP32 MicroPython root directory by clicking the File menu, select Save As.
  3. Click the MicroPython device and save it as “ssd1306.py”. To check if you successfully save the OLED driver library, click the View menu and select File. The ssd1306.py should be seen under the MicroPython device.

Video Demonstration

Call To Action

For any concern, write your message in the comment section.

You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.

Thank you and have a good days ahead.

See you,

– George Bantique | tech.to.tinker@gmail.com

Source Code

1. Example # 1, exploring the 0.96 OLED driver library using the REPL:

 1# More details can be found in TechToTinker.blogspot.com 
 2# George Bantique | tech.to.tinker@gmail.com
 3
 4from machine import Pin, I2C 
 5from ssd1306 import SSD1306_I2C 
 6
 7i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000) 
 8oled = SSD1306_I2C(128, 64, i2c, addr=0x3C)
 9
10# The following codes should be tested using the REPL.
11# #1. To print a string:  
12# oled.text('Hello world', 0, 0) 
13# #2. To display all the commands in queue:     
14# oled.show() 
15# #3. Now to clear the oled display:  
16# oled.fill(0) 
17# oled.show() 
18# #4. You may also use the invert function to invert the display.  
19# oled.invert(1) 
20# #5.To display a single pixel.  
21# oled.pixel(10,20,1) 
22# oled.show() 
23# #6. To display a horizontal line  
24# oled.hline(30,40,10,1) 
25# oled.show() 
26# #7. To display a vertical line  
27# oled.vline(30,45,5,1) 
28# oled.show() 
29# #8. While hline and vline is quite useful, there is another function that is more flexible to use which is the line function.  
30# oled.line(0,50,10,50,1) 
31# oled.show() 
32# #9.We may also be able to print a rectangle.  
33# oled.rect(10,60,10,5,1) 
34# oled.show() 
35# #10. Or we may also print a filled rectangle:  
36# oled.fill_rect(10,70,10,5,1) 
37# oled.show()

2. Example # 2, displaying a Real Time Clock (RTC) using the ESP32’s builtin RTC module:

 1# More details can be found in TechToTinker.blogspot.com 
 2# George Bantique | tech.to.tinker@gmail.com
 3
 4from machine import Pin 
 5from machine import I2C 
 6from machine import RTC 
 7from time import sleep_ms 
 8from ssd1306 import SSD1306_I2C
 9
10i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000) 
11oled = SSD1306_I2C(128, 64, i2c, addr=0x3C)
12
13rtc = RTC() 
14rtc.datetime((2021, 2, 6, 6, 19, 44, 0, 0)) 
15# rtc.datetime((YYYY, MM, DD, WD, HH, MM, SS, MS)) 
16# WD 1 = Monday 
17# WD 7 = Sunday 
18isPoint = True
19
20while True: 
21    t = rtc.datetime() 
22    oled.fill(0) 
23    oled.text('** 0.96 OLED **', 4, 0) 
24    oled.text('Date: {}-{:02d}-{:02d}' .format(t[0],t[1],t[2]), 0, 25)  
25    if isPoint: 
26        colon = ':' 
27    else: 
28        colon = ' ' 
29    oled.text('Time: {:02d}{}{:02d}' .format(t[4], colon, t[5]), 0, 40) 
30    oled.show() 
31    sleep_ms(500) 
32    isPoint = not isPoint

3. 0.96 OLED driver library

  1#MicroPython SSD1306 OLED driver, I2C and SPI interfaces created by Adafruit
  2
  3import time
  4import framebuf
  5
  6# register definitions
  7SET_CONTRAST        = const(0x81)
  8SET_ENTIRE_ON       = const(0xa4)
  9SET_NORM_INV        = const(0xa6)
 10SET_DISP            = const(0xae)
 11SET_MEM_ADDR        = const(0x20)
 12SET_COL_ADDR        = const(0x21)
 13SET_PAGE_ADDR       = const(0x22)
 14SET_DISP_START_LINE = const(0x40)
 15SET_SEG_REMAP       = const(0xa0)
 16SET_MUX_RATIO       = const(0xa8)
 17SET_COM_OUT_DIR     = const(0xc0)
 18SET_DISP_OFFSET     = const(0xd3)
 19SET_COM_PIN_CFG     = const(0xda)
 20SET_DISP_CLK_DIV    = const(0xd5)
 21SET_PRECHARGE       = const(0xd9)
 22SET_VCOM_DESEL      = const(0xdb)
 23SET_CHARGE_PUMP     = const(0x8d)
 24
 25class SSD1306:
 26    def __init__(self, width, height, external_vcc):
 27        self.width = width
 28        self.height = height
 29        self.external_vcc = external_vcc
 30        self.pages = self.height // 8
 31        # Note the subclass must initialize self.framebuf to a framebuffer.
 32        # This is necessary because the underlying data buffer is different
 33        # between I2C and SPI implementations (I2C needs an extra byte).
 34        self.poweron()
 35        self.init_display()
 36
 37    def init_display(self):
 38        for cmd in (
 39            SET_DISP | 0x00, # off
 40            # address setting
 41            SET_MEM_ADDR, 0x00, # horizontal
 42            # resolution and layout
 43            SET_DISP_START_LINE | 0x00,
 44            SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
 45            SET_MUX_RATIO, self.height - 1,
 46            SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
 47            SET_DISP_OFFSET, 0x00,
 48            SET_COM_PIN_CFG, 0x02 if self.height == 32 else 0x12,
 49            # timing and driving scheme
 50            SET_DISP_CLK_DIV, 0x80,
 51            SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1,
 52            SET_VCOM_DESEL, 0x30, # 0.83*Vcc
 53            # display
 54            SET_CONTRAST, 0xff, # maximum
 55            SET_ENTIRE_ON, # output follows RAM contents
 56            SET_NORM_INV, # not inverted
 57            # charge pump
 58            SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14,
 59            SET_DISP | 0x01): # on
 60            self.write_cmd(cmd)
 61        self.fill(0)
 62        self.show()
 63
 64    def poweroff(self):
 65        self.write_cmd(SET_DISP | 0x00)
 66
 67    def contrast(self, contrast):
 68        self.write_cmd(SET_CONTRAST)
 69        self.write_cmd(contrast)
 70
 71    def invert(self, invert):
 72        self.write_cmd(SET_NORM_INV | (invert & 1))
 73
 74    def show(self):
 75        x0 = 0
 76        x1 = self.width - 1
 77        if self.width == 64:
 78            # displays with width of 64 pixels are shifted by 32
 79            x0 += 32
 80            x1 += 32
 81        self.write_cmd(SET_COL_ADDR)
 82        self.write_cmd(x0)
 83        self.write_cmd(x1)
 84        self.write_cmd(SET_PAGE_ADDR)
 85        self.write_cmd(0)
 86        self.write_cmd(self.pages - 1)
 87        self.write_framebuf()
 88
 89    def fill(self, col):
 90        self.framebuf.fill(col)
 91
 92    def pixel(self, x, y, col):
 93        self.framebuf.pixel(x, y, col)
 94
 95    def scroll(self, dx, dy):
 96        self.framebuf.scroll(dx, dy)
 97
 98    def text(self, string, x, y, col=1):
 99        self.framebuf.text(string, x, y, col)
100    
101    # Missing from current files in github:
102    # https://github.com/adafruit/micropython-adafruit-ssd1306/blob/master/ssd1306.py
103    def hline(self, x, y, w, col):
104        self.framebuf.hline(x, y, w, col)
105    def vline(self, x, y, h, col):
106        self.framebuf.vline(x, y, h, col)
107    def line(self, x1, y1, x2, y2, col):
108        self.framebuf.line(x1, y1, x2, y2, col)
109    def rect(self, x, y, w, h, col):
110        self.framebuf.rect(x, y, w, h, col)
111    def fill_rect(self, x, y, w, h, col):
112        self.framebuf.fill_rect(x, y, w, h, col)
113    def blit(self, fbuf, x, y):
114        self.framebuf.blit(fbuf, x, y)
115
116class SSD1306_I2C(SSD1306):
117    def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False):
118        self.i2c = i2c
119        self.addr = addr
120        self.temp = bytearray(2)
121        # Add an extra byte to the data buffer to hold an I2C data/command byte
122        # to use hardware-compatible I2C transactions.  A memoryview of the
123        # buffer is used to mask this byte from the framebuffer operations
124        # (without a major memory hit as memoryview doesn't copy to a separate
125        # buffer).
126        self.buffer = bytearray(((height // 8) * width) + 1)
127        self.buffer[0] = 0x40  # Set first byte of data buffer to Co=0, D/C=1
128        self.framebuf = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height)
129        super().__init__(width, height, external_vcc)
130
131    def write_cmd(self, cmd):
132        self.temp[0] = 0x80 # Co=1, D/C#=0
133        self.temp[1] = cmd
134        self.i2c.writeto(self.addr, self.temp)
135
136    def write_framebuf(self):
137        # Blast out the frame buffer using a single I2C transaction to support
138        # hardware I2C interfaces.
139        self.i2c.writeto(self.addr, self.buffer)
140
141    def poweron(self):
142        pass
143
144class SSD1306_SPI(SSD1306):
145    def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
146        self.rate = 10 * 1024 * 1024
147        dc.init(dc.OUT, value=0)
148        res.init(res.OUT, value=0)
149        cs.init(cs.OUT, value=1)
150        self.spi = spi
151        self.dc = dc
152        self.res = res
153        self.cs = cs
154        self.buffer = bytearray((height // 8) * width)
155        self.framebuf = framebuf.FrameBuffer1(self.buffer, width, height)
156        super().__init__(width, height, external_vcc)
157
158    def write_cmd(self, cmd):
159        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
160        self.cs.high()
161        self.dc.low()
162        self.cs.low()
163        self.spi.write(bytearray([cmd]))
164        self.cs.high()
165
166    def write_framebuf(self):
167        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
168        self.cs.high()
169        self.dc.high()
170        self.cs.low()
171        self.spi.write(self.buffer)
172        self.cs.high()
173
174    def poweron(self):
175        self.res.high()
176        time.sleep_ms(1)
177        self.res.low()
178        time.sleep_ms(10)
179        self.res.high()

References And Credits

  1. Adafruit SSD1306: https://github.com/adafruit/micropython-adafruit-ssd1306/blob

  2. Gorillacell ESP32 kit: gorillacell.kr



Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.