Let's Workshop: 128x64 4pin OLED Module

Posted by Sebastian Karam on

Here is a quick introduction to using 128x64 4pin OLED modules. This will provide you with an entry point to using OLED modules and help give you the confidence to begin using them in your projects.

This example will demonstrate the use of an Arduino UNO in selecting the module from the i2c bus, before loading a picture on to the screen. This will use the universal 8bit graphics library created by Oliver Kraus to interface with the display chipset and provide functions to operate it.

Components

  • 1pcs Arduino UNO or Compatible - LCAA100005
  • 1pcs 0.96" 128x64 White OLED Display Module - BCAA100007
  • 4pcs Male to Female Jumper Cables - GBAA100002

Wiring

Wire the two boards together as can be seen in the image below, taking care to match the pin numbers.

Coding

The code consists of an include, initialisation, a definition and loop. First the U8glib.h library is linked to the code. This is followed by the initialisation of the display object, taking note of screen size and driving chipset. Next is a large definition, this is the byte data for a bitmap image taken from a xbm file. You can generate these types of files using a number of different imaging software. We use GIMP, ensuring the image is the right size. The file can then be opened in a text editor and the image values extracted. In the loop we first call the draw function from the library and pass it the screen coordinates, size of the image and the array the image data is held in. A final while catch is put in to restrict the loop to a single run.

Load the code below into the Arduino IDE and upload it to your board.

/*
  A simple program designed to setup and demonstrate the u8glib library and 
  a 128x64 OLED 4 bit OLED module.
 
  The program uses the u8glib library to initialise a 128x64 monochrome display module and
  displays an bitmap (xbm) image on it.
  
  modified 22 April 2019
  by Sebastian Karam - Flux Workshop
  
  The Universal 8bit Graphics Library created by Oliver Kraus
  https://github.com/olikraus/u8glib
*/ 

#include "U8glib.h" // include the universal graphcs library

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);	// initialise a u8g display object

const uint8_t flux_xbm[] PROGMEM = { // store the image due to be displayed as an array of bytes
   0x00, 0x00, 0x00, 0xfc, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x03, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x78, 0x0e, 0x38, 0x80, 0x73, 0x00, 0x07,
   0x00, 0x00, 0xfe, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x7e, 0x0e, 0x38, 0x80, 0x73, 0x00, 0x07, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
   0x00, 0x7f, 0x0e, 0x38, 0x80, 0x73, 0x00, 0x07, 0x00, 0xc0, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00, 0x80, 0x0f, 0x0e, 0x38, 0x80, 0xf3, 0x80, 0x07,
   0x00, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x80, 0x03, 0x0e, 0x38, 0x80, 0xe3, 0x80, 0x03, 0x00, 0xf8, 0xff, 0x07, 0xe0, 0xff, 0x1f, 0x00,
   0xc0, 0x01, 0x0e, 0x38, 0x80, 0xe3, 0xe3, 0x03, 0x00, 0xfc, 0xff, 0x00, 0x00, 0xfe, 0x3f, 0x00, 0xc0, 0x01, 0x0e, 0x38, 0x80, 0xc3, 0xff, 0x01,
   0x00, 0xfe, 0x1f, 0x00, 0x00, 0xf8, 0x7f, 0x00, 0xc0, 0x01, 0x0e, 0x38, 0x80, 0x83, 0xff, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0xe0, 0xff, 0x00,
   0xc0, 0x01, 0x0e, 0x38, 0x80, 0xc3, 0xff, 0x01, 0x80, 0xff, 0x03, 0x00, 0x00, 0x80, 0xff, 0x01, 0xc0, 0x1f, 0x1e, 0x78, 0x80, 0xe3, 0xe3, 0x03,
   0xc0, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, 0xc0, 0x1f, 0x1c, 0x70, 0xc0, 0xe1, 0x80, 0x03, 0xc0, 0x7f, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x07,
   0xc0, 0x1f, 0x7c, 0xf0, 0xf1, 0xf1, 0x80, 0x07, 0xe0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xc0, 0x01, 0xf8, 0xe1, 0xff, 0x70, 0x00, 0x07,
   0xf0, 0x1f, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x0f, 0xc0, 0x01, 0xf0, 0xc3, 0x7f, 0x70, 0x00, 0x07, 0xf0, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x1f,
   0xc0, 0x01, 0xc0, 0x01, 0x1f, 0x70, 0x00, 0x07, 0xf8, 0x0f, 0x30, 0x00, 0x00, 0x18, 0xe0, 0x1f, 0xc0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0xf8, 0x07, 0x7c, 0x00, 0x00, 0x7c, 0xe0, 0x3f, 0xc0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x7c, 0x00, 0x00, 0x7e, 0xc0, 0x3f,
   0xc0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0xfc, 0x00, 0x00, 0x7e, 0x80, 0x3f, 0xc0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0xfc, 0x01, 0xfc, 0x00, 0x00, 0x7e, 0x80, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x01, 0xfc, 0x00, 0x00, 0x7e, 0x80, 0x7f,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0xfc, 0x00, 0x00, 0x7e, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0xfe, 0x00, 0xfc, 0x01, 0x00, 0x3f, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0xf8, 0x01, 0x80, 0x3f, 0x00, 0xff,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0xf8, 0x03, 0x80, 0x3f, 0x00, 0xfe, 0xc0, 0x01, 0x1c, 0xfc, 0x00, 0x1c, 0x81, 0x01,
   0x7f, 0x00, 0xf0, 0x0f, 0xe0, 0x1f, 0x00, 0xfe, 0xc0, 0x01, 0x1c, 0xfe, 0x03, 0xbf, 0xc3, 0x01, 0x7f, 0x00, 0xe0, 0x3f, 0xf8, 0x0f, 0x00, 0xfe,
   0xc0, 0x01, 0x1c, 0xff, 0x87, 0x9f, 0xc3, 0x01, 0x7f, 0x00, 0xe0, 0xff, 0xff, 0x0f, 0x00, 0xfe, 0xc0, 0x01, 0x9c, 0x07, 0xc7, 0x83, 0xe3, 0x00,
   0x7f, 0x00, 0xc0, 0xff, 0xff, 0x07, 0x00, 0xfe, 0xc1, 0x01, 0x9c, 0x03, 0xce, 0x81, 0xf3, 0x00, 0x7f, 0x00, 0x80, 0xff, 0xff, 0x03, 0x00, 0xfe,
   0xc1, 0x01, 0xdc, 0x01, 0xee, 0x80, 0x73, 0x00, 0x7f, 0x00, 0x80, 0xff, 0xff, 0x03, 0x00, 0xfe, 0xc1, 0x71, 0xdc, 0x01, 0xec, 0x80, 0x7f, 0x00,
   0x7f, 0x00, 0xc0, 0xff, 0xff, 0x07, 0x00, 0xfe, 0xc1, 0xf9, 0xdc, 0x01, 0xec, 0x80, 0xff, 0x00, 0x7f, 0x00, 0xe0, 0xff, 0xff, 0x0f, 0x00, 0xfe,
   0xc0, 0xfd, 0xdd, 0x01, 0xee, 0x80, 0xff, 0x01, 0x7f, 0x00, 0xf0, 0x0f, 0xf0, 0x1f, 0x00, 0xfe, 0xc0, 0x8f, 0x9f, 0x03, 0xee, 0x80, 0xc3, 0x03,
   0x7f, 0x00, 0xf0, 0x07, 0xc0, 0x1f, 0x00, 0xfe, 0xc0, 0x07, 0x9f, 0x07, 0xe7, 0x80, 0x83, 0x03, 0xff, 0x00, 0xf8, 0x03, 0x80, 0x3f, 0x00, 0xfe,
   0xc0, 0x03, 0x1e, 0xff, 0xe7, 0x80, 0x03, 0x07, 0xff, 0x00, 0xf8, 0x01, 0x00, 0x3f, 0x00, 0xff, 0xc0, 0x03, 0x1e, 0xfe, 0xe3, 0x80, 0x03, 0x07,
   0xfe, 0x00, 0xfc, 0x00, 0x00, 0x7f, 0x00, 0xff, 0xc0, 0x01, 0x1c, 0xf8, 0xe0, 0x80, 0x03, 0x07, 0xfe, 0x00, 0xfc, 0x00, 0x00, 0x7e, 0x00, 0x7f,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0xfe, 0x01, 0xfc, 0x00, 0x00, 0x7e, 0x80, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0xfc, 0x01, 0xfc, 0x00, 0x00, 0x7e, 0x80, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0xfc, 0x00, 0x00, 0x7e, 0x80, 0x3f,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x7c, 0x00, 0x00, 0x7c, 0xc0, 0x3f, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00,
   0xf8, 0x07, 0x78, 0x00, 0x00, 0x3c, 0xe0, 0x3f, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x1f,
   0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x1f, 0x80, 0x1f, 0x1c, 0x00, 0xe0, 0x01, 0x7c, 0x00,
   0xf0, 0x1f, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x0f, 0xc0, 0x1f, 0xdc, 0x07, 0xf8, 0x07, 0xff, 0x01, 0xe0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07,
   0xe0, 0x1f, 0xfc, 0x1f, 0xfc, 0x8f, 0xff, 0x03, 0xc0, 0x7f, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x07, 0xe0, 0x00, 0xfc, 0x3f, 0x0e, 0x8e, 0x83, 0x03,
   0xc0, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, 0xe0, 0x00, 0x7c, 0x3c, 0x06, 0xdc, 0x81, 0x07, 0x80, 0xff, 0x03, 0x00, 0x00, 0x80, 0xff, 0x01,
   0xe0, 0x3f, 0x3c, 0x78, 0x07, 0xdc, 0x01, 0x07, 0x00, 0xff, 0x07, 0x00, 0x00, 0xe0, 0xff, 0x00, 0xc0, 0x7f, 0x1c, 0x70, 0x07, 0xd8, 0x01, 0x07,
   0x00, 0xfe, 0x1f, 0x00, 0x00, 0xf8, 0x7f, 0x00, 0x80, 0xff, 0x1c, 0x70, 0x07, 0xdc, 0x01, 0x07, 0x00, 0xfc, 0xff, 0x00, 0x00, 0xfe, 0x3f, 0x00,
   0x00, 0xe0, 0x1c, 0x70, 0x06, 0xdc, 0x01, 0x07, 0x00, 0xf8, 0xff, 0x07, 0xe0, 0xff, 0x1f, 0x00, 0x00, 0xe0, 0x1c, 0x70, 0x0e, 0xce, 0x83, 0x03,
   0x00, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0xe0, 0xff, 0x1c, 0x70, 0xbc, 0xcf, 0xef, 0x03, 0x00, 0xc0, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00,
   0xe0, 0x7f, 0x1c, 0x70, 0xf8, 0xc7, 0xff, 0x01, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xe0, 0x3f, 0x1c, 0x70, 0xf0, 0xc1, 0x79, 0x00,
   0x00, 0x00, 0xfe, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x1f, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x01, 0x00,
   0x00, 0x00, 0x00, 0xfc, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

void setup(void) {
}

void loop(void) {
  u8g.drawXBMP( 0, 0, 128, 64, flux_xbm); // send the xbm image to the display, specifying position and size
  while (1==1) {} // set the loop to only run once
}

Running

With the board loaded with the program and all the connections made the screen will produce an output like the one seen below. Do note that although there appears to be lines in the colour this is a result of refresh rates and cameras. To the human eye, it is uniform colour.

What to try next?

  • Investigate further display functions
  • Update the screen regularly to produce a digital dial
  • Use the cursor and fonts to build a visual output for a projects menu structure

Share this post



← Older Post Newer Post →


Leave a comment