Skip to content

Latest commit

 

History

History
185 lines (124 loc) · 5.86 KB

011_display_ssd1306_tutorials.md

File metadata and controls

185 lines (124 loc) · 5.86 KB

OLED Display (ssd1306)

Table of Contents

Prolog

Today, almost nothing works for end users without displays. Displays are available in almost all sizes, shapes and colors and some still offer options (e.g. touch) that were unthinkable years ago. There are also different types such as LCD, OLED, TFT and so on.

You can use many of them on your ESP via MicroPython. Here are a few examples.

I2C OLED basics (ssd1306)

The first example should show you a few basics. Play around with it (modify the code) and find out for yourself what else works.

Requirements

  • mandatory 1x I2C OLED (example 0.96" 128x64)
  • few cables
  • optional a breadboard

Example

oled_i2c_ssd1306.jpg

Circuit

015_circuit_diagram_i2c_oled.png

Code

Install first the ssd1306 driver! Here you will find some solutions proposals about how to install.

If you're using the SPI version, just adapt the code! Here it's described.

# create new subdirectory
$ mkdir -p ~/Projects/ESP/examples/display

# create script
$ touch ~/Projects/ESP/examples/display/i2c_oled_ssd1306_basics.py

Source Code for i2c_oled_ssd1306_basics.py

Source Code for module lib/ssd1306.py

Check your circuit (adapt pins if needed) and copy the script to the microcontroller as main.py.

# copy script as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/display/i2c_oled_ssd1306_basics.py /pyboard/main.py

# copy module file into /pyboard/lib/
(venv) $ rshell -p [SERIAL-PORT] cp lib/ssd1306.py /pyboard/lib/

# start repl
(venv) $ rshell -p [SERIAL-PORT] repl

Start with keys Control + d. To leave the REPL, press keys Control + x.

I2C OLED date/time with fonts (ssd1306)

Actually, this task seems very simple. But that is a bit deceptive! The font size and the current time will play a role in this example.

Requirements

... same as previous example ...

Circuit

... same as previous example ...

Code

# create script
$ touch ~/Projects/ESP/examples/display/i2c_oled_ssd1306_time.py

Source Code for i2c_oled_ssd1306_time.py

Source Code for module lib/ssd1306.py

You need 2 more modules. Download this to your local environment (in the lib folder).

# download font freesans
$ curl -L 'https://raw.githubusercontent.com/peterhinch/micropython-font-to-py/master/writer/freesans20.py' -o lib/freesans20.py

# download writer.py
$ curl -L 'https://raw.githubusercontent.com/miguelgrinberg/micropython-iot-tutorial/master/chapter6/writer.py' -o lib/writer.py

Check your circuit and copy the script to the microcontroller as main.py.

# start rshell serial connection
(venv) $ rshell -p [SERIAL-PORT]

# copy module files from local project to microcontroller
/your/current/path> cp lib/freesans20.py /pyboard/lib/
/your/current/path> cp lib/writer.py /pyboard/lib/
/your/current/path> cp lib/ssd1306.py /pyboard/lib/

# copy script as main.py
/your/current/path> cp examples/display/i2c_oled_ssd1306_time.py /pyboard/main.py

# start the REPL
/your/current/path> repl

Start with keys Control + d. Stop the loop with keys Control + c. To leave the REPL, press keys Control + x.

Since the time is set by rshell when connecting, the display is not up-to-date without rshell! An NTP time synchronization would be necessary for this. You can reproduce, for example, by pressing reset button of the device.

Icons on OLED (ssd1306)

The objective of this example is to show how you can create icons. There will be two absolute identical icons (25x25 px) displayed, but created in two different ways.

Requirements

... same as first example ...

Circuit

... same as first example ...

Code

# create script
$ touch ~/Projects/ESP/examples/display/i2c_oled_ssd1306_icons.py

# download image
$ curl -L 'https://raw.githubusercontent.com/Lupin3000/ESP/master/images/src/demo.pbm' -o images/src/demo.pbm

Source Code for i2c_oled_ssd1306_icons.py

Source Code for module lib/ssd1306.py

Check your circuit and copy the script to the microcontroller as main.py.

# start rshell serial connection
(venv) $ rshell -p [SERIAL-PORT]

# copy module file into /pyboard/lib/
/your/current/path> cp lib/ssd1306.py /pyboard/lib/

# copy script as main.py
/your/current/path> cp examples/display/i2c_oled_ssd1306_icons.py /pyboard/main.py

# copy image into /pyboard/ directory
/your/current/path> cp images/src/demo.pbm /pyboard/

# start the REPL
/your/current/path> repl

Start with keys Control + d. Stop the loop with keys Control + c. To leave the REPL, press keys Control + x.

The simplest solution to create *.pbm is, to convert a standard format (like JPG or PNG) with GIMP (GNU Image Manipulation Program).

Import an image: File -> Open

Select on menubar: Image -> Mode -> Indexed

Select radio button: Use black and white (1-bit) palette

Enable checkbox: Remove unused colors from colormap

Press button: Convert

Export with filename: [YOUR NAME].pbm

Select radio button: Raw and press button Export

Home | Previous | Next