The mini OLED 0.96 in display is 128 x 64 pixels and communication protocol is I2C.

There is also a SPI version which has more pins 6 or 7.

The I2C model has only 4 pins Vcc,Gnd,SCL & SDA.

The supply volt is 3.3 to 5v. As an I2C device it has got an address , by default it is 0x3C or decimal 60. This can be changed by soldering the position of a resistor on the back of pcb.

 

ol3

Connect Vcc of OLED to 3.3v, Gnd to Gnd

SCL  –>  GPIO 22

SDA  –> GPIO 21

IMG_20200328_140033

Resolution of display is 128 x 64 pixels. 0 to 127 in X axis and 0 –63 on Y axis.

oled_pix

 

MicroPython provides some built-in support for these displays, and a simple framebuffer which can be used as a drawing surface.

However, we still need a driver to interface the buffer to the display.

There is a Python ssd1306 module for OLED displays available.This is python library for interfacing OLED (both I2C & SPI supported).

https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py

Click on Raw button and save as ssd1306.py file on your computer.This save as appears only in Chrome browser.

Open THONNY PYTHON

Use File  –> Open and browse to the location to select the ssd1306.py file.

Click File  –>  Save as

Select Micropython device (ESP32)

 

o1

Save the file as ssd1306.py

Now the driver module is on ESP32.

o2

By default already you have boot.py

Now we have loaded the driver ssd1306.py.

o3

From interpreter of Thonny Python , the individual code can be tested.

From machine module import Pin , I2C

Pin Class handles all GPIO pins and I2C handles i2c protocol..

Then import the OLED driver module ssd1306.

 

o4

To use the display you first need to create an I2C interface. In MicroPython I2C is  via a software implementation so you can select any GPIO pins you like.

We shall select the default SCL pin GPIO 22 &  SDA pin GPIO 21.

 

o5

 

Then create an oled object from class SSD1306_I2C

as this class is inside file ssd1306, we use

ssd1306.SSD1306_I2C

The arguments are  128 – width , 64 – height , i2c – protocol, 0x3c – address

 

o6

 

Once the object is instantiated , the methods can be used.

.text() method is to print message on display.

Pass on the message to print and the X,Y coordinates.

.show() method is used to show the message from buffer to display.

Unless you issue this oled.show() , you cannot see the result.

 

o7

 

The framebuffer class provides support for writing text using a simple 8×8 bitmap font. The pixel x,y positions are relative to the top-left of the 8×8 character, so positioning at 0,0 will give the absolute top left of the screen.

Here we have selected x= 30, y =30 POSITION.

 

IMG_20200330_055934

 

To CLEAR the display we call   fill ( 0) method an then show()

 

o8

 

To draw an illuminated  line. Origin  Origin (10,10) and final (80,20)

For illuminated , we use 1 at end

o9

 

IMG_20200330_060216

 

Draw an illuminated horizontal line. Origin (5, 30) width 100 pixels

o10

 

IMG_20200330_060410

Draw an illuminated filled rectangle. Origin (20,10) and width x height 60×40 pixels

o11

 

IMG_20200330_060526

 

 

The ssd1306 object itself  provides methods for direct control of the display component.

The display can be turned on and off using

oled.poweron() and oled.poweroff() respectively.

o13

 

You can set the contrast for the display using

.contrast() passing in parameter c which is a value between 0 and 255. This controls the contrast between the foreground, active colour 1 and the background 0.

o14

To invert the display, switching foreground and background colours, call .invert(1).

Calling .invert(0) will return the display to how it was originally. You can use this for display-flashing visual effects:

o15

Download code and library module from here :

http://www.alselectro.com/files/micropython_oled_dht_ultrasonic.zip

After testing OLED using interpreter , we shall write code on to EDITOR of Thonny Python.

ol1

Save this file as main.py on Micro Python device.

Now you have 3 files on ESP32

boot.py

main.py

ssd1306.py

ol2

 

Micropython executes boot.py on power on , then looks for main.py to execute.

Click on F5 to execute and see the result on OLED display.

Video :

 

 

blog_image