Arduino library · Displays

OLED_I2C: an example that runs

Yes. You can use the OLED_I2C library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (OLED display, 128x64). The example below, "Text, a number and a frame on an OLED with OLED_I2C", runs on the Arduino Uno R3.

Built in on Mokxi: #include <OLED_I2C.h> and it builds, with nothing to install. Free, and no account needed.

What OLED_I2C does

An SSD1306 OLED drawn in a frame buffer: begin(SSD1306_128X64), setFont(SmallFont), print(), printNumI(), printNumF(), drawBitmap(), drawLine(), drawRect(), drawCircle(), invert() and update(). That is what OLED_I2C, by Henning Karlsen (Rinky-Dink Electronics), covers. The sketch below uses it on the Arduino Uno: “Text, a number and a frame on an OLED with OLED_I2C”.

The OLED_I2C library's interface (Henning Karlsen, Rinky-Dink Electronics), text, numbers, bitmaps and shapes on an SSD1306 OLED over I2C, drawn in a frame buffer and sent with update().

Drawing changes the buffer only, as upstream; update() sends it.

The calls follow release 2.21 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameOLED_I2C
AuthorHenning Karlsen (Rinky-Dink Electronics)
Include#include <OLED_I2C.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesOLED display, 128x64, OLED display, 128x32

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <OLED_I2C.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "OLED_I2C" by Henning Karlsen (Rinky-Dink Electronics) from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add OLED_I2C to lib_deps in PlatformIO. Mokxi follows the calls of release 2.21, so a sketch written here builds there too.

What to know in the simulator: The two pins can be any two here; upstream wants the board's I2C pins on most boards. The fonts are Mokxi's own drawings in the library's font format.

The calls you will use

In the order a sketch meets them.

OLED myOLED(2, 3)

Makes myOLED, the OLED object the calls below are made on.

myOLED.begin(SSD1306_128X64)

Starts it up and returns true if it started, false if it did not. Call it once in setup(). Arguments: model.

myOLED.setFont(SmallFont)

Sets the font to the value you pass in.

myOLED.print("OLED_I2C", CENTER, 0)

Prints it.

myOLED.drawRect(0, 16, 127, 47)

Draws the rect.

myOLED.update()

Sends what has been drawn to the screen. Nothing appears until you call it.

Example: Text, a number and a frame on an OLED with OLED_I2C

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <OLED_I2C.h>

OLED myOLED(2, 3);  // SDA, SCL

extern uint8_t SmallFont[];
extern uint8_t MediumNumbers[];

void setup() {
  Serial.begin(9600);
  if (!myOLED.begin(SSD1306_128X64)) {
    Serial.println("no OLED");
    while (1);
  }
  myOLED.setFont(SmallFont);
  myOLED.print("OLED_I2C", CENTER, 0);
  myOLED.setFont(MediumNumbers);
  myOLED.printNumI(2026, CENTER, 24);
  myOLED.drawRect(0, 16, 127, 47);
  myOLED.update();
  Serial.println("updated");
}

void loop() {}

Wiring

  • Arduino Uno R3 2 to OLED display, 128x64 SDA
  • Arduino Uno R3 3 to OLED display, 128x64 SCL
  • Arduino Uno R3 5V to OLED display, 128x64 VCC
  • Arduino Uno R3 GND to OLED display, 128x64 GND

Open it in the editor, wired and ready to run

Try OLED_I2C without installing anything

Open the example, change the sketch, and keep your version in a free account.