Skip to content

OLED display

For many applications it is vital to have a way of providing instructions and feedback to the user. Many types of display are available including LCD screens and e-paper. OLED technology offers a good compromise between features and power consumption, and units that use the I2C communications interface are very easy to control.

128x32 OLED display module Figure 1: 128x32 OLED display module

Hardware

Modules that use the I2C protocol have four pins as shown in the table below.

Pin code Description
VCC Power input - usually 3.3V
GND Ground
SCL I2C clock pin - used to coordinate communications between the module and the microprocessor
SDA I2C data pin

The hardware setup is shown in Fig. 2. Please check the pin labels on your particular display module to see whether they are in the same order as those shown in Fig. 1.

Breadboard layout Figure 2: Breadboard layout

Software

The OLED display requires some complex data handling routines in order to work correctly. There are several libraries available that implement the detailed code and provide you with a simple API. The ADAFRUIT_SSD1306 library is recommended and Fig. 3 shows its information in the Particle Web IDE. You should choose the I2C example labelled (1) in the figure and then click Use this example, labelled (2), to load it into the code area ready for flashing to the Argon.

AdaFruit library in the Particle Web IDE Figure 3: AdaFruit library in the Particle Web IDE

When it runs, the example code runs through a series of demonstrations which use text of different sizes, graphics and dynamic effects. To make use of these features in your own code, make a copy of the example, locate the section(s) of the code you want to keep and remove the parts you are not interested in. For example, the code below uses an adaptation of the testscrolltext() function to display the number of seconds that have passed since startup in intervals of 10s. The displayed text start at the top-left of the display and scrolls to the right for 2s.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Variables to manage time
unsigned long seconds = 0;
unsigned long  previous = 0;
char buffer[10];

void setup() {
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Clear the buffer
  display.clearDisplay();
  display.display();
}

void loop() {
  seconds = millis() / 10000;
  Serial.println(seconds);
  if (seconds > previous) {
      previous = seconds;
      display.clearDisplay();

      display.setTextSize(2); // Draw 2X-scale text
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      // Convert the unsigned interger to a char array
      sprintf( buffer, "%ld s", seconds * 10 );
      display.println(buffer);
      display.display();      // Show initial text
      display.startscrollright(0x00, 0x0F);
      delay(2000);
      display.stopscroll();
  }
}

Things to note

Line 1: The SPI.h header file is not required

Lines 69-72: Add variables as needed for your particular application

Line 89: Divide the value returned by millis() by 10,000 to get ten-second intervals

Line 90: Use the serial monitor for debugging if needed

Line 99: Convert numerical values to text before displaying them

Technical documentation for the AdaFruit SSD1306 library Technical documentation for the AdaFruit SSD1306 library

AdaFruit Arduino library and examples AdaFruit Arduino library and examples