Using the 128×64 I2C SH1106 OLED Display Module Screen with a WeMos D1 Mini (ESP8266)

In this blog post I talk about how you can use a 128×64 I2C SH1106 OLED Display Module Screen with a WeMos D1 Mini (ESP8266) using the Arduino IDE.

Summary of the 128×64 I2C SH1106 OLED Display Module Screen

The SH1106 OLED display module is a nice 128×64 pixel screen. This unit provides a single colour allowing each pixel to either be on or off individually.

One of the useful features is that this is an OLED screen which draws much less power compared to a standard LED screen. This is useful in IoT devices which may be powered by batteries where power draw is important.

This OLED display uses the SH1106 driver IC which is a standard supported library on Arduino and WeMos systems. This library provides the ability to draw text and primitives (triangles, rectangles, etc) to the screen.

Another useful feature is that this unit supports being powered by 3.3V and 5V supply which means the WeMos can power it using its 3.3V line.

Important differences compared to using the 128×64 I2C SH1106 OLED Display Module Screen on an Arduino

Before wiring up the WeMos it is important to note that there are some differences between the WeMos D1 Mini (ESP8266) and an Arduino.

The Arduino IDE is configured to use pin constants for the Arduino. This means that the WeMos Digital pin 1 is not referred to as 1 as you would using an Arduino. Instead to refer to digital pin 1 you need to use the special constant D1.

I go into more details for this in my blog post Pin numbering for WeMos D1 Mini (ESP8266). If you haven’t read this and are having trouble connecting your 128×64 I2C SH1106 OLED Display Module Screen properly I recommend having a read.

Wiring it up to a WeMos D1 Mini

This OLED screen has four pins which are GND, VCC, SCL, SDA (left to right). The GND will be wired to the GND on the WeMos and the VCC is attached to the 3.3 volts.

The SCL and SDA pins need to be connected to the I2C pins on the WeMos. Reading on I2C Connection Pins on the WeMos D1 Mini (ESP8266) will tell you which these are.

Programming the WeMos to use the 128×64 I2C SH1106 OLED Display Module Screen

To program the OLED screen I am using the U8glib library for monochrome displays library. When using the Arduino IDE it can be installed by pressing “Manage Libraries”. The library name is “U8g2 by oliver”.

This library makes it easier to program the OLED screen by providing functions to create text and standard primitives (squares, triangles, etc).

#include <U8g2lib.h>
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);

void setup(void) {
  u8g2.begin();
}

The first line imports the library which we will be using to control the OLED screen. Then once it is imported an object for the screen is instantiated. This uses the default I2C ports on the WeMos which are D1 and D2.

The parameter passed in the constructor of this is U8G2_R0 which defines the rotation of the screen.

The setup function then begins communication with the screen on the I2C bus.

void loop(void) {
  u8g2.firstPage();
  do {
    //keep this the same as it pages through the data generating the screen
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(0,20,"Hello World!");
  } while ( u8g2.nextPage() );

  delay(1000); 
}

Inside the loop function firstPage is called on the u8g2 variable. This triggers the start of drawing the OLED screen.

To optimise drawing and reduce the hardware requirements of rendering the images the screen is split up and paged across the I2C bus.

This is done by looping over each created page and calculating the piece of the image which needs to be drawn. All drawing code should be kept inside the loop so that each page is drawing the same image.

Inside the do while loop I perform two operations. The first just configures which font to use. The second then draws a string as position (0, 20) of “Hello World!”.

This will be looped over depending on the number of pages. Once all loops are created the buffered image is then displayed. Once this is done we ask the WeMos to wait a second and redraw the image. This is less important for static images but will be useful when animating or changing the text.

Summary

Since the OLED screen works at 3.3 volts and 5 volts it is ideal to use with the WeMos D1 mini. The low power features also mean that if the WeMos is battery powered it will be able to run for longer. This small screen works really well with the WeMos.

For a full review and guide to use the sensor with an Arduino please read my previous post.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.