128×64 I2C SH1106 OLED Display Module Screen for Arduino Review and Code
Today I review the 128×64 I2C SH1106 OLED Display Module Screen for Arduino and provide example code.
Overview of the 128×64 I2C SH1106 OLED Display Module
This rectangular OLED display module gives you a 128×64 pixel screen to display content on. This is a single colour model allowing for each pixel to be on or off. Since this is an OLED screen this will draw a lot less power compared to a standard LCD screen. This is useful in small embedded devices such as when used with an Arduino as these may be battery powered.
This OLED display uses the SH1106 driver IC which has a standard supported library on the Arduino. This allows easy control on the Arduino to draw and write to the screen.
The approximate size of this module is 27 * 27 mm which is small enough to keep the footprint of the device low.
A final advantage to this unit is that it can easily be used with various microcontrollers as it supports both 3.3 and 5-volt power inputs. These are the two most common power levels among IC’s.
Wiring it up to the Arduino
The OLED screen has four pins, left to right these are GND, VCC, SCL, and SDA. I am going to wire GND and VCC to the Arduino’s GND and 5 volt pins respectively.
The SCL and SDA pins have to be connected to the Arduino’s I2C pins. For my Arduino Uno these are A4 (SDA), A5 (SCL). If you are not using an Arduino Uno you can find your Arduino’s I2C pins by looking at this table.
Programming the OLED screen to display Hello World
To program the OLED screen I am using the U8glib library for monochrome displays. library. If you are using the Arduino IDE you will need to install it using the Manage Libraries feature, the library name in the Arduino IDE is “U8g2 by oliver”.
This library allows easy programming of the OLED screen allowing both text and standard graphical shapes.
#include <U8g2lib.h> U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0); void setup(void) { u8g2.begin(); }
First the U8g2lib library is imported so that its functions can be used. Then the u8g2 module is initialized for the SH1106 OLED display module. The parameter passed to the constructor sets the rotation to the default rotation level.
There are a number of constructors for this module but this is using the I2C constructor to access the LCD display using the standard I2C pins.
Then in the setup function, the communication with the OLED display module is started.
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); }
In the loop function, firstPage()
is called to start drawing of the OLED. The drawing code is inside a do while
loop so that the library can efficiently page the data across to the OLED display. By including your drawing code inside the while loop you are able to adjust how the speed and RAM tradeoff.
In fully buffered mode rendering the images onto the OLED display is much faster however it will use a lot more RAM. You can change the rendering mode based on the constructor to use less RAM. However reducing the amount of RAM used for rendering will slow down the render as it will render the images in several pages which is slower.
Inside the loop I configure which font I want to use and then draw the string “Hello World!”. The first two parameters to the drawStr
function are the x
and y
position on the screen.
Review for the 128×64 I2C SH1106 OLED Display Module
This little OLED display allows you to easily add a display to any small electronics project. One of the main advantages for projects is that this OLED screen is low power useful for battery projects.
The simple driver library for this means you dont need to worry about writing low level commands to write to the device. This paired with the high resolution of the display means you can use it to display complex information.
This is a really nice display which can be used for many different purposes. Going forward I will be writing some posts about how you can use this screens to draw more complex shapes and interfaces.
Would buy again!
surely your marked up photo is wrong you say 5 volts on SDA and not VCC?
Oooh yes you are right! I will fix that later this week. Many thanks!