The organic light-emitting diode (OLED) display that we’ll use in this tutorial is the SSD1306 model: a monocolor, 0.96-inch display with 128×64 pixels
The OLED display doesn’t require backlight, which results in a very nice contrast in dark environments. Additionally, its pixels consume energy only when they are on, so the OLED display consumes less power when compared with other displays.
The model we’re using here has only four pins and communicates with the Arduino using I2C communication protocol. There are models that come with an extra RESET pin. There are also other OLED displays that communicate using SPI communication.
To control the OLED display we need to download and install U8glib from Sketch -> Include Library -> Manage Libraries. There are numerous libraries written for OLED, but this one seems to fit our requirement. Adafruit's GFX library can also be used in a similar way.
Upload the below program from GitHub and watch the display come to life.
/** 0.96 OLED Program : Basic Program to display logo and then follows a set of text ** At VEEROBOT, we invest time and resources providing this open source code,* Please support VEEROBOT and open-source hardware by purchasing products* from us @ http://veerobot.com* -----------------------------------------------------------------------------* You are free to redistribute it and/or modify it under the terms of the GNU* Lesser General Public License as published by the Free Software Foundation,* either version 3 of the License, or (at your option) any later version.** This Code is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU Lesser General Public License for more details.** See <http://www.gnu.org/licenses/>*/#include"U8glib.h"U8GLIB_SSD1306_128X64u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI #include"logo.h"voidu8g_prepare(void) {u8g.setFont(u8g_font_6x10);u8g.setFontRefHeightExtendedText();u8g.setDefaultForegroundColor();u8g.setFontPosTop();}voidu8g_box_frame(uint8_t a) {u8g.drawStr( 0,0,"drawBox");u8g.drawBox(5,10,20,10);u8g.drawBox(10+a,15,30,7);u8g.drawStr( 0,30,"drawFrame");u8g.drawFrame(5,10+30,20,10);u8g.drawFrame(10+a,15+30,30,7);}voidu8g_disc_circle(uint8_t a) {u8g.drawStr( 0,0,"drawDisc");u8g.drawDisc(10,18,9);u8g.drawDisc(24+a,16,7);u8g.drawStr( 0,30,"drawCircle");u8g.drawCircle(10,18+30,9);u8g.drawCircle(24+a,16+30,7);}voidu8g_r_frame(uint8_t a) {u8g.drawStr( 0,0,"drawRFrame/Box");u8g.drawRFrame(5,10,40,30, a+1);u8g.drawRBox(50,10,25,40, a+1);}voidu8g_string(uint8_t a) {u8g.drawStr(30+a,31," 0");u8g.drawStr90(30,31+a," 90");u8g.drawStr180(30-a,31," 180");u8g.drawStr270(30,31-a," 270");}voidu8g_line(uint8_t a) {u8g.drawStr( 0,0,"drawLine");u8g.drawLine(7+a,10,40,55);u8g.drawLine(7+a*2,10,60,55);u8g.drawLine(7+a*3,10,80,55);u8g.drawLine(7+a*4,10,100,55);}voidu8g_triangle(uint8_t a) {uint16_t offset = a;u8g.drawStr( 0,0,"drawTriangle");u8g.drawTriangle(14,7,45,30,10,40);u8g.drawTriangle(14+offset,7-offset,45+offset,30-offset,57+offset,10-offset);u8g.drawTriangle(57+offset*2,10,45+offset*2,30,86+offset*2,53);u8g.drawTriangle(10+offset,40+offset,45+offset,30+offset,86+offset,53+offset);}voidu8g_ascii_1() {chars[2] =" ";uint8_t x, y;u8g.drawStr( 0,0,"ASCII page 1");for( y =0; y <6; y++ ) {for( x =0; x <16; x++ ) {s[0] = y*16+ x +32;u8g.drawStr(x*7, y*10+10, s); } }}voidu8g_ascii_2() {chars[2] =" ";uint8_t x, y;u8g.drawStr( 0,0,"ASCII page 2");for( y =0; y <6; y++ ) {for( x =0; x <16; x++ ) {s[0] = y*16+ x +160;u8g.drawStr(x*7, y*10+10, s); } }}voidu8g_extra_page(uint8_t a){if ( u8g.getMode() == U8G_MODE_HICOLOR ||u8g.getMode() == U8G_MODE_R3G3B2) { /* draw background (area is 128x128) */u8g_uint_t r, g, b; b = a <<5;for( g =0; g <64; g++ ) {for( r =0; r <64; r++ ) {u8g.setRGB(r<<2, g<<2, b );u8g.drawPixel(g, r); } }u8g.setRGB(255,255,255);u8g.drawStr( 66,0,"Color Page"); }elseif ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {u8g.drawStr( 66,0,"Gray Level");u8g.setColorIndex(1);u8g.drawBox(0,4,64,32); u8g.drawBox(70,20,4,12);u8g.setColorIndex(2);u8g.drawBox(0+1*a,4+1*a,64-2*a,32-2*a);u8g.drawBox(74,20,4,12);u8g.setColorIndex(3);u8g.drawBox(0+2*a,4+2*a,64-4*a,32-4*a);u8g.drawBox(78,20,4,12); }else {u8g.drawStr( 0,12,"setScale2x2");u8g.setScale2x2();u8g.drawStr( 0,6+a,"setScale2x2");u8g.undoScale(); }}voidclearOLED(){u8g.firstPage();do {} while( u8g.nextPage() );}voiddrawLOGO() {u8g.firstPage();do {u8g.drawXBMP(0,0, logo_width, logo_height, veeler_logo); } while ( u8g.nextPage() );delay(2500); clearOLED(); // uncomment this to turn off always on logo // Use Gimp and save 2 point bitmap as xbm and get hex codes.}uint8_t draw_state =0;voiddraw(void) {u8g_prepare();switch(draw_state >>3) {case0:u8g_box_frame(draw_state&7); break;case1:u8g_disc_circle(draw_state&7); break;case2:u8g_r_frame(draw_state&7); break;case3:u8g_string(draw_state&7); break;case4:u8g_line(draw_state&7); break;case5:u8g_triangle(draw_state&7); break;case6:u8g_ascii_1(); break;case7:u8g_ascii_2(); break;case8:u8g_extra_page(draw_state&7); break; }}voidsetup(void) { // flip screen, if required //u8g.setRot180();drawLOGO();}voidloop(void) { // picture loop u8g.firstPage(); do {draw(); } while( u8g.nextPage() ); // increase the state draw_state++;if ( draw_state >=9*8 ) draw_state =0; // rebuild the picture after some delay //delay(150);}