Arduino Mega - SSD1309 OLED Display

This guide shows you how to use an Arduino Mega with a 2.42 inch (2.4") SSD1309 I2C OLED display (128×64 resolution). Here we will learn in detail:

Arduino Mega SSD1309 OLED I2C display

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×SSD1309 I2C OLED Display 2.42 inch 128x64
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of SSD1309 OLED Display

The SSD1309 is a monochrome OLED display driver chip. The 2.42 inch version provides a 128×64 pixel resolution. It communicates via I2C protocol, requiring only two pins (SDA and SCL) plus power connections. This makes it ideal for projects with limited pin availability.

Key features of the SSD1309 OLED:

  • Display Size: 2.42 inches (diagonal)
  • Resolution: 128×64 pixels
  • Communication: I2C (2-wire interface)
  • Default I2C Address: 0x3C
  • Operating Voltage: 3.3V or 5V compatible
  • Built-in hardware scrolling support
  • Adjustable contrast/brightness
  • Low power consumption

SSD1309 OLED Display Pinout

The 2.42 inch SSD1309 I2C OLED module typically has 4 pins:

  • GND: Ground pin - connect to Arduino Mega GND
  • VCC: Power supply pin - connect to Arduino Mega 5V
  • SDA: I2C data line - connect to Arduino Mega pin 20 (SDA)
  • SCL: I2C clock line - connect to Arduino Mega pin 21 (SCL)
SSD1309 OLED Pinout

※ NOTE THAT:

Pin layout on an OLED module can differ depending on the manufacturer and model. Always check and follow the labels printed on your specific OLED module. Pay attention!

This guide is for an OLED display that uses the SSD1309 I2C driver. We tested it with an SSD1309 OLED display from DIYables. It works well, with no problems.

Wiring Diagram

Connect the 2.42 inch SSD1309 OLED display to Arduino Mega following this wiring:

The wiring diagram between Arduino Mega SSD1309 OLED

This image is created using Fritzing. Click to enlarge image

SSD1309 OLED Arduino Mega
VCC 5V
GND GND
SDA 20 (SDA)
SCL 21 (SCL)

How To Use SSD1309 OLED with Arduino Mega

Install DIYables_OLED_SSD1309 Library

  • Go to the Libraries icon on the left side of the Arduino IDE.
  • Type DIYables_OLED_SSD1309 in the search box and look for the library by DIYables.
  • Click the Install button to add the library.
Arduino Mega SSD1309 OLED library

The DIYables_OLED_SSD1309 library depends on the Adafruit_GFX library. If Adafruit_GFX is not already installed, the Arduino IDE will automatically prompt you to install it when you install DIYables_OLED_SSD1309.

Arduino Mega Code - Display "Hello World" on SSD1309 OLED

The below code shows how to display text on the 2.42 inch SSD1309 OLED display:

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Hello World * Prints text in two sizes on the 2.42 inch 128x64 I2C OLED with Arduino Mega. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.setTextSize(1); // 6x8 pixels per character display.setTextColor(SSD1309_PIXEL_ON); // turn pixels on display.setCursor(0, 0); display.println(F("Hello, World!")); display.println(); display.setTextSize(2); // 12x16 pixels per character display.println(F("DIYables")); display.setTextSize(1); display.println(); display.println(F("SSD1309 OLED 128x64")); display.display(); // push buffer to screen } void loop() { }

Explanation

  • Include necessary libraries and define constants
  • Create display object with I2C address 0x3C
  • Initialize display in setup()
  • Set text size, color, and cursor position
  • Print text to display buffer using print() and println()
  • Call display.display() to push buffer content to the screen

※ NOTE THAT:

The F() macro stores strings in flash memory instead of RAM. This saves valuable RAM on Arduino Mega.

Arduino Mega Code - Display Text with Various Formats

This code demonstrates text display with different sizes and number formatting:

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Display Text * Displays text in various sizes and formats on the 2.42" SSD1309 OLED with Arduino Mega. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); // Various text sizes display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 0); display.println(F("Size 1 - DIYables")); display.setTextSize(2); display.println(F("Size 2")); display.setTextSize(3); display.println(F("Sz3")); display.display(); delay(3000); // Number formatting display.clearDisplay(); display.setTextSize(1); display.setCursor(0, 0); int value = 42; float pi = 3.14159; display.print(F("Integer: ")); display.println(value); display.print(F("Float: ")); display.println(pi, 2); // 2 decimal places display.print(F("Hex: 0x")); display.println(value, HEX); display.print(F("Binary: 0b")); display.println(value, BIN); display.display(); } void loop() { }

Arduino Mega Code - Draw Shapes on SSD1309 OLED

The below code shows how to draw pixels, lines, rectangles, circles, and triangles:

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Draw Shapes * Demonstrates drawing pixels, lines, rectangles, circles, and triangles on the 2.42" OLED with Arduino Mega. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.display(); } void loop() { // Draw pixels display.clearDisplay(); for (int i = 0; i < 20; i++) { display.drawPixel(random(SCREEN_WIDTH), random(SCREEN_HEIGHT), SSD1309_PIXEL_ON); } display.display(); delay(2000); // Draw lines display.clearDisplay(); for (int y = 0; y < SCREEN_HEIGHT; y += 8) { display.drawLine(0, 0, SCREEN_WIDTH - 1, y, SSD1309_PIXEL_ON); } display.display(); delay(2000); // Draw rectangles display.clearDisplay(); display.drawRect(10, 10, 40, 30, SSD1309_PIXEL_ON); // outline display.fillRect(60, 10, 40, 30, SSD1309_PIXEL_ON); // filled display.drawRoundRect(10, 45, 40, 15, 5, SSD1309_PIXEL_ON); // rounded corners display.display(); delay(2000); // Draw circles display.clearDisplay(); display.drawCircle(32, 32, 20, SSD1309_PIXEL_ON); // outline display.fillCircle(96, 32, 20, SSD1309_PIXEL_ON); // filled display.display(); delay(2000); // Draw triangles display.clearDisplay(); display.drawTriangle(20, 10, 10, 50, 30, 50, SSD1309_PIXEL_ON); // outline display.fillTriangle(90, 10, 70, 50, 110, 50, SSD1309_PIXEL_ON); // filled display.display(); delay(2000); }

Available drawing functions:

  • drawPixel(x, y, color) - Draw a single pixel
  • drawLine(x0, y0, x1, y1, color) - Draw a line
  • drawRect(x, y, w, h, color) - Draw rectangle outline
  • fillRect(x, y, w, h, color) - Draw filled rectangle
  • drawRoundRect(x, y, w, h, radius, color) - Draw rounded rectangle
  • drawCircle(x, y, radius, color) - Draw circle outline
  • fillCircle(x, y, radius, color) - Draw filled circle
  • drawTriangle(x0, y0, x1, y1, x2, y2, color) - Draw triangle outline
  • fillTriangle(x0, y0, x1, y1, x2, y2, color) - Draw filled triangle

Arduino Mega Code - Hardware Scrolling on SSD1309 OLED

The SSD1309 supports hardware-based scrolling. This code demonstrates all four scroll directions:

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Scroll Text * Demonstrates all four hardware scroll directions on the 2.42" OLED with Arduino Mega. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.setTextSize(2); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(10, 24); display.println(F("DIYables")); display.display(); delay(2000); } void loop() { // Scroll right across all pages display.startscrollright(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); // Scroll left display.startscrollleft(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); // Diagonal scroll right display.startscrolldiagright(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); // Diagonal scroll left display.startscrolldiagleft(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); }

Scrolling functions:

  • startscrollright(start, stop) - Scroll right
  • startscrollleft(start, stop) - Scroll left
  • startscrolldiagright(start, stop) - Diagonal scroll right
  • startscrolldiagleft(start, stop) - Diagonal scroll left
  • stopscroll() - Stop scrolling

The start and stop parameters define the page range (0x00 to 0x07 for full screen).

Arduino Mega Code - Display Bitmap Image on SSD1309 OLED

This code shows how to display bitmap images stored in program memory:

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Display Bitmap Image * Shows a 16x16 heart icon and 128x64 DIYables logo on the 2.42" OLED with Arduino Mega. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // 16x16 heart icon bitmap const unsigned char heart_icon[] PROGMEM = { 0x00, 0x00, 0x0c, 0x30, 0x1e, 0x78, 0x3f, 0xfc, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xfe, 0x3f, 0xfc, 0x1f, 0xf8, 0x0f, 0xf0, 0x03, 0xc0, 0x00, 0x00 }; // 128x64 DIYables logo bitmap const unsigned char diyables_logo[] PROGMEM = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x07, 0xff, 0xc1, 0xff, 0xe0, 0x7f, 0xf0, 0x1f, 0xfc, 0x03, 0x00, 0x7f, 0xff, 0xff, 0xfc, 0x00, 0x1f, 0xff, 0xf7, 0xff, 0xf9, 0xff, 0xfc, 0x7f, 0xff, 0x0f, 0x80, 0x3f, 0xff, 0xff, 0xf8, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xfd, 0xff, 0xfe, 0xff, 0xff, 0x9f, 0xc0, 0x1f, 0xff, 0xff, 0xf8, 0x00, 0x7f, 0x80, 0xff, 0x00, 0x7f, 0xc0, 0x3f, 0xf0, 0x0f, 0xfc, 0x00, 0x1f, 0xff, 0xff, 0xf0, 0x00, 0xfe, 0x00, 0x7f, 0x00, 0x3f, 0x80, 0x1f, 0xe0, 0x07, 0xf8, 0x00, 0x0f, 0xff, 0xff, 0xe0, 0x01, 0xfc, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x0f, 0xff, 0xff, 0xe0, 0x03, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x07, 0xff, 0xff, 0xc0, 0x07, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x03, 0xff, 0xff, 0xc0, 0x0f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x03, 0xff, 0xff, 0x80, 0x0f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x01, 0xff, 0xff, 0x80, 0x1f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x01, 0xff, 0xff, 0x00, 0x3f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xff, 0x00, 0x3f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x7f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x7f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xfc, 0x00, 0xff, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xfc, 0x00, 0xff, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xfc, 0x01, 0xff, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xf8, 0x01, 0xff, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xf8, 0x03, 0xff, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xf0, 0x03, 0xff, 0xfc, 0x00, 0x7e, 0x00, 0x1f, 0x80, 0x1f, 0xe0, 0x07, 0xf8, 0x00, 0x00, 0xff, 0xf0, 0x07, 0xff, 0x7f, 0x80, 0xff, 0x00, 0x7f, 0xc0, 0x3f, 0xf0, 0x0f, 0xfc, 0x00, 0x00, 0xff, 0xf0, 0x07, 0xfe, 0x3f, 0xff, 0xff, 0xff, 0xfd, 0xff, 0xfe, 0xff, 0xff, 0x9f, 0xc0, 0x00, 0xff, 0xe0, 0x0f, 0xfc, 0x1f, 0xff, 0xf7, 0xff, 0xf9, 0xff, 0xfc, 0x7f, 0xff, 0x0f, 0x80, 0x00, 0xff, 0xe0, 0x0f, 0xf8, 0x07, 0xff, 0xc1, 0xff, 0xe0, 0x7f, 0xf0, 0x1f, 0xfc, 0x03, 0x00, 0x00, 0xff, 0xe0, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc0, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc0, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc0, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x80, 0x7f, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x7f, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xfe, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xf0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xe0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x07, 0xc0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x0f, 0x80, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x3e, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x7c, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0xf0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0xe0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.display(); } void loop() { // Display small heart icon centered display.clearDisplay(); display.drawBitmap((SCREEN_WIDTH - 16) / 2, (SCREEN_HEIGHT - 16) / 2, heart_icon, 16, 16, SSD1309_PIXEL_ON); display.display(); delay(3000); // Display full-screen DIYables logo display.clearDisplay(); display.drawBitmap(0, 0, diyables_logo, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1309_PIXEL_ON); display.display(); delay(3000); // Invert display display.invertDisplay(true); delay(2000); display.invertDisplay(false); delay(1000); }

To display images:

  • Convert your image to a monochrome bitmap array (use online tools or image2cpp)
  • Store the array in PROGMEM to save RAM
  • Use drawBitmap(x, y, bitmap, width, height, color) to display

Arduino Mega Code - Adjust Brightness/Contrast

Control the brightness and contrast of the SSD1309 OLED display:

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Contrast & Dim * Sweeps contrast from 0→255→0 then toggles dim mode on the 2.42" OLED with Arduino Mega. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } // Draw a test pattern so the contrast changes are visible display.clearDisplay(); display.fillRect(0, 0, 64, 32, SSD1309_PIXEL_ON); display.fillRect(64, 32, 64, 32, SSD1309_PIXEL_ON); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_INVERSE); display.setCursor(16, 28); display.println(F("Contrast Demo")); display.display(); delay(2000); } void loop() { // Ramp up for (int c = 0; c <= 255; c += 5) { display.setContrast((uint8_t)c); delay(30); } delay(1000); // Ramp down for (int c = 255; c >= 0; c -= 5) { display.setContrast((uint8_t)c); delay(30); } delay(1000); // Quick dim toggle display.dim(true); // minimum brightness delay(2000); display.dim(false); // restore delay(2000); }

Brightness control functions:

  • setContrast(value) - Set contrast level (0-255, higher = brighter)
  • dim(true/false) - Quick toggle between dim and normal brightness
  • invertDisplay(true/false) - Invert all pixels (black↔white)

Arduino Mega Code - Use External Fonts

The Adafruit_GFX library includes FreeFont typefaces. This code demonstrates three different fonts:

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – External Fonts * Cycles through three FreeFont typefaces on the 2.42" SSD1309 OLED with Arduino Mega. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #include <Fonts/FreeSerif9pt7b.h> #include <Fonts/FreeSansBold12pt7b.h> #include <Fonts/FreeMono9pt7b.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.display(); } void loop() { // ── Built-in 5×7 font ── display.clearDisplay(); display.setFont(NULL); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 0); display.println(F("Built-in 5x7 font")); display.println(); display.setTextSize(2); display.println(F("DIYables")); display.display(); delay(3000); // ── FreeSerif 9pt ── display.clearDisplay(); display.setFont(&FreeSerif9pt7b); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 14); // Y = baseline display.println(F("FreeSerif 9pt")); display.setCursor(0, 38); display.println(F("DIYables OLED")); display.setCursor(0, 58); display.println(F("Hello World!")); display.display(); delay(3000); // ── FreeSansBold 12pt ── display.clearDisplay(); display.setFont(&FreeSansBold12pt7b); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 20); display.println(F("SansBold")); display.setCursor(0, 52); display.println(F("DIYables")); display.display(); delay(3000); // ── FreeMono 9pt ── display.clearDisplay(); display.setFont(&FreeMono9pt7b); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 14); display.println(F("FreeMono 9pt")); display.setCursor(0, 34); display.println(F("0123456789")); display.setCursor(0, 54); display.println(F("!@#$%^&*()")); display.display(); delay(3000); }

※ NOTE THAT:

FreeFont files are large. Arduino Mega has 256KB flash memory, which is sufficient for multiple fonts. However, if you need to save space, use only the fonts you need.

Font usage:

  • Include font header: #include <Fonts/FreeSans9pt7b.h>
  • Set font: display.setFont(&FreeSans9pt7b)
  • Reset to default: display.setFont(NULL)
  • When using external fonts, Y coordinate represents the baseline, not the top

Troubleshooting

If the OLED display is not working, try these steps:

Check I2C Address

The default I2C address is 0x3C, but some modules use 0x3D. Run this I2C scanner to find the correct address:

#include <Wire.h> void setup() { Serial.begin(9600); Wire.begin(); Serial.println("I2C Scanner"); byte count = 0; for (byte i = 1; i < 127; i++) { Wire.beginTransmission(i); if (Wire.endTransmission() == 0) { Serial.print("Found I2C device at 0x"); if (i < 16) Serial.print("0"); Serial.println(i, HEX); count++; } } Serial.print("Found "); Serial.print(count); Serial.println(" device(s)"); } void loop() {}

If your display uses 0x3D, change SCREEN_ADDRESS to 0x3D in the code.

Check Wiring

Verify connections:

  • VCC → 5V
  • GNDGND
  • SDA → Pin 20 (SDA)
  • SCL → Pin 21 (SCL)

Check Power Supply

Make sure the Arduino Mega is receiving adequate power through the USB cable or external power supply.

Video Tutorial

...VIDEO LINK_VIDEO_ARDUINO_MEGA_SSD1309_OLED_DISPLAY

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!