Codey OnlineArduino & ESP32 component library › OLED Display 0.96 inch
OLED Display 0.96 inch
Sensors

OLED Display 0.96 inch

OLED Display 0.96 inch 128x64 pixels

Width: 27 mm

Pinout — OLED Display 0.96 inch

Pin Signal Description
GND ground top header hole labeled GND
VCC power top header hole labeled VCC
SCL SCL top header hole labeled SCL
SDA SDA top header hole labeled SDA

Example wiring diagram — OLED Display 0.96 inch

From To Wire
board:5V x1:VCC red
board:GND x1:GND black
board:A5 x1:SCL green
board:A4 x1:SDA blue

Example Arduino code — OLED Display 0.96 inch

This example was written and compile-checked for the arduino-uno. Codey Online adapts it to any other supported board for you.

#include <Wire.h>

// Most 0.96" OLED modules use the SSD1306 controller at I2C address 0x3C.
const uint8_t OLED_ADDR = 0x3C;

// 128x64 display memory size.
const uint8_t OLED_WIDTH = 128;
const uint8_t OLED_HEIGHT = 64;
const uint16_t OLED_BUFFER_SIZE = (OLED_WIDTH * OLED_HEIGHT) / 8;
uint8_t buffer[OLED_BUFFER_SIZE];

void oledCommand(uint8_t cmd) {
  Wire.beginTransmission(OLED_ADDR);
  Wire.write(0x00); // Command mode
  Wire.write(cmd);
  Wire.endTransmission();
}

void oledCommand2(uint8_t cmd, uint8_t value) {
  Wire.beginTransmission(OLED_ADDR);
  Wire.write(0x00); // Command mode
  Wire.write(cmd);
  Wire.write(value);
  Wire.endTransmission();
}

void clearBuffer() {
  for (uint16_t i = 0; i < OLED_BUFFER_SIZE; i++) {
    buffer[i] = 0x00;
  }
}

void drawPixel(uint8_t x, uint8_t y, bool on) {
  if (x >= OLED_WIDTH || y >= OLED_HEIGHT) return;
  uint16_t index = x + (y / 8) * OLED_WIDTH;
  uint8_t bitMask = 1 << (y % 8);
  if (on) buffer[index] |= bitMask;
  else buffer[index] &= ~bitMask;
}

// Very small built-in 5x7 font for the demo text.
// Each character is 5 columns wide, 7 rows tall.
const uint8_t font[][5] = {
  // ' ' (32)
  {0x00,0x00,0x00,0x00,0x00},
  // 'A' (65)
  {0x7E,0x11,0x11,0x11,0x7E},
  // 'C' (67)
  {0x3E,0x41,0x41,0x41,0x22},
  // 'D' (68)
  {0x7F,0x41,0x41,0x22,0x1C},
  // 'E' (69)
  {0x7F,0x49,0x49,0x49,0x41},
  // 'L' (76)
  {0x7F,0x40,0x40,0x40,0x40},
  // 'O' (79)
  {0x3E,0x41,0x41,0x41,0x3E},
  // 'S' (83)
  {0x26,0x49,0x49,0x49,0x32},
  // 'T' (84)
  {0x01,0x01,0x7F,0x01,0x01},
  // '0' (48)
  {0x3E,0x45,0x49,0x51,0x3E},
  // '9' (57)
  {0x06,0x49,0x49,0x29,0x1E},
  // '.' (46)
  {0x00,0x60,0x60,0x00,0x00},
  // '1' (49)
  {0x00,0x21,0x7F,0x01,0x00},
  // '6' (54)
  {0x1E,0x29,0x49,0x49,0x06},
  // '2' (50)
  {0x42,0x61,0x51,0x49,0x46},
};

int fontIndex(char c) {
  switch (c) {
    case ' ': return 0;
    case 'A': return 1;
    case 'C': return 2;
    case 'D': return 3;
    case 'E': return 4;
    case 'L': return 5;
    case 'O': return 6;
    case 'S': return 7;
    case 'T': return 8;
    case '0': return 9;
    case '9': return 10;
    case '.': return 11;
    case '1': return 12;
    case '6': return 13;
    case '2': return 14;
    default:  return 0;
  }
}

void drawChar(uint8_t x, uint8_t y, char c) {
  int idx = fontIndex(c);
  for (uint8_t col = 0; col < 5; col++) {
    uint8_t line = font[idx][col];
    for (uint8_t row = 0; row < 7; row++) {
      drawPixel(x + col, y + row, line & (1 << row));
    }
  }
}

void drawText(uint8_t x, uint8_t y, const char *text) {
  while (*text) {
    drawChar(x, y, *text++);
    x += 6;
  }
}

void displayBuffer() {
  // Set page addressing mode and address range.
  oledCommand(0x20); // Set memory addressing mode
  oledCommand(0x00); // Horizontal addressing mode
  oledCommand2(0x21, 0x00); // Column address start
  oledCommand2(0x21, 0x7F); // Column address end
  oledCommand2(0x22, 0x00); // Page address start
  oledCommand2(0x22, 0x07); // Page address end

  for (uint16_t i = 0; i < OLED_BUFFER_SIZE; i++) {
    Wire.beginTransmission(OLED_ADDR);
    Wire.write(0x40); // Data mode
    Wire.write(buffer[i]);
    Wire.endTransmission();
  }
}

void oledInit() {
  delay(100);
  oledCommand(0xAE); // Display off
  oledCommand2(0xD5, 0x80); // Display clock divide ratio / oscillator frequency
  oledCommand2(0xA8, 0x3F); // Multiplex ratio for 128x64
  oledCommand2(0xD3, 0x00); // Display offset
  oledCommand(0x40); // Start line
  oledCommand2(0x8D, 0x14); // Charge pump ON
  oledCommand2(0x20, 0x00); // Horizontal addressing mode
  oledCommand(0xA1); // Segment remap
  oledCommand(0xC8); // COM scan direction remapped
  oledCommand2(0xDA, 0x12); // COM pins hardware config
  oledCommand2(0x81, 0xCF); // Contrast
  oledCommand2(0xD9, 0xF1); // Pre-charge period
  oledCommand2(0xDB, 0x40); // VCOMH deselect level
  oledCommand(0xA4); // Resume display from RAM
  oledCommand(0xA6); // Normal display
  oledCommand(0xAF); // Display on
}

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

  oledInit();
  clearBuffer();
  drawText(0, 0, "OLED 0.96");
  drawText(0, 16, "ARDUINO UNO");
  drawText(0, 32, "I2C TEST");
  drawText(0, 48, "SDA A4 SCL A5");
  displayBuffer();

  Serial.println("OLED initialized and text drawn.");
}

void loop() {
  // Simple blinking dot to show the sketch is running.
  static bool on = false;
  on = !on;

  clearBuffer();
  drawText(0, 0, "OLED 0.96");
  drawText(0, 16, "ARDUINO UNO");
  drawText(0, 32, "I2C TEST");
  drawText(0, 48, "SDA A4 SCL A5");
  drawPixel(120, 0, on);
  displayBuffer();

  delay(500);
}

Related components

18650 Battery Shield with USB-C Charger and 5V Boost Converter

18650 Battery Shield with USB-C Charger and 5V Boost Converter

2.4 inch OLED Display I2C 3.3V

2.4 inch OLED Display I2C 3.3V

3.5 inch TFT LCD Shield

3.5 inch TFT LCD Shield

50kg Load Cell Weight Sensor

50kg Load Cell Weight Sensor

Build your Arduino or ESP32 project with AI

Describe what you want to build. Codey Online writes the code, draws the wiring diagram, compiles it and flashes your board straight from the browser.

Start for free