SSD1306 OLED LCD Library

Now you can find a plenitude of cheap OLED displays online. One very common model is the 0.96″ 128×64 pixels OLED display, based on the SSD1306. This is a single-chip CMOS OLED/PLED driver with controller for organic / polymer light emitting diode dot-matrix graphic display system.

A common LCD module based on the SSD1306

This hardware driver can talk both via I2C and SPI, although the modules you will find are exposing the I2C pins more. This is why I designed the code to communicate via I2C. Changing that to SPI is easy, you just need to change the communication primitives:

void ssd1306_128x64_i2c::writeCommand(uint8_t c) {
    Wire.beginTransmission(m_i2caddr);
    Wire.write(0x00); // control
    Wire.write(c);
    Wire.endTransmission();
}

Although there are already many examples and fine libraries readily available, most of them are using a buffer, meaning all the drawing operations are saved to a memory array and them dumped to the LCD as one complete frame. This has certain advantages that I would normally go for, but it also uses a lot of memory, a no go for small microcontrollers. This is why I wanted to write a library that will directly write to the LCD hardware instead of using a buffer. It’s a bit slower but it saves considerable memory!
Using this library is straight forward:

#include 
#include 

ssd1306_128x64_i2c display;

void setup()   {
    display.init();
    
    for (int i=0;i<128;i++)
      display.drawPixel(i,i/2,1);
    
    display.drawString(0, 7, "pocketmagic.net");

    display.drawString(50, 1, "Hello World!");
}

bool invert = 1;

void loop() {
  delay(1000);
  display.invertDisplay(invert);
  invert = !invert;
}

And here are the results:

Example code results

Download the library on Github or here.

Leave a Reply