Arduino Mega - OLED 128x64 Display
This guide shows you how to use an Arduino Mega with a 128x64 I2C OLED display. Here we will learn in detail:
- How to connect a 128x64 OLED screen to Arduino Mega.
- How to program the Arduino Mega to show text and numbers on the OLED.
- How to center text and numbers on the OLED (vertical and horizontal).
- How to draw on the OLED with Arduino Mega.
- How to show images on the OLED with Arduino Mega.

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 | × | SSD1306 I2C OLED Display 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) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of OLED Display
There are many kinds of OLED screens. They differ in how they work, their sizes, and their colors.
- Ways to connect: I2C, SPI
- Size: 128 by 64, 128 by 32
- Colors: white, blue, two-tone

SPI is usually faster than I2C, but it needs more pins on the Arduino Mega. I2C, on the other hand, uses only two pins and these pins can connect to many I2C devices. So you must choose: fewer pins or faster communication. For I2C OLED screens, there are different drivers like SSD1306 and SH1106. This guide uses the 128x64 SSD1306 I2C OLED Display.
I2C OLED Display Pinout
- GND pin: connect to Arduino Mega ground.
- VCC pin: power for the display; connect to the Arduino Mega 5V pin.
- SCL pin: I2C clock line.
- SDA pin: I2C data line.

※ NOTE THAT:
Pin layout on an OLED module can be different depending on who makes it and which model you have. Always check and follow the labels on the OLED module. Pay attention!
This guide is for an OLED display that uses the SSD1306 I2C driver. We tested it with an OLED display from DIYables. It works well, with no problems.
Wiring Diagram
- Wiring diagram for Arduino Mega with a 128x64 OLED screen

This image is created using Fritzing. Click to enlarge image
If you use a different Arduino Mega, the pin layout won’t be the same as the Uno. Check the table below for information about other Arduino Mega models.
| OLED Module | Arduino Mega |
|---|---|
| Vin | 5V |
| GND | GND |
| SDA | A4 |
| SCL | A5 |
How To Use OLED with Arduino Mega
Install SSD1306 OLED library
- Go to the Libraries icon on the left side of the Arduino IDE.
- Type SSD1306 in the search box and look for the SSD1306 library by Adafruit.
- Click the Install button to add the library.

- You need to install some extra libraries.
- Click the Install All button to install all the needed libraries.

How to program for OLED
- Add a library.
- Set the screen size to OLED 123 by 64 pixels.
- Make an SSD1306 OLED display.
- In the setup() function, get the OLED display ready.
- You can then show words, pictures, and draw lines.
Arduino Mega Code - Display Text on OLED
Here are some simple functions you can use to display text on the OLED screen:
- Oled.clearDisplay(): turns off all the pixels on the screen.
- Oled.drawPixel(x, y, color): draws one small dot at position x, y.
- Oled.setTextSize(n): sets text size from 1 (small) to 8 (big).
- Oled.setCursor(x, y): sets where the text starts, at x, y.
- Oled.setTextColor(WHITE): text becomes white.
- Oled.setTextColor(BLACK, WHITE): text is black and the background is white.
- Oled.println("message"): show the text.
- Oled.println(number): show a number.
- Oled.println(number, HEX): show the number in hexadecimal.
- Oled.display(): show the changes on the screen.
- Oled.startscrollright(start, stop): start moving text from left to right.
- Oled.startscrollleft(start, stop): start moving text from right to left.
- Oled.startscrolldiagright(start, stop): move text diagonally from bottom-left to top-right.
- Oled.startscrolldiagleft(start, stop): move text diagonally from bottom-right to top-left.
- Oled.stopscroll(): stop any scrolling text.
How to vertical and horizontal center align text/number on OLED
Arduino Mega Code - Drawing on OLED
Arduino Mega Code – Display Image
To display a picture on an OLED screen, first turn the picture (any format) into a bitmap array. You can use this online tool to convert it: https://javl.github.io/image2cpp/. Look at the image below to see how to turn a picture into a bitmap array. I turned the Arduino icon into a bitmap array.

Copy the new array code and paste it into the Arduino icon array in the code below.
※ NOTE THAT:
- The image should be no bigger than the screen.
- To use the code with an OLED 128x32, resize the image and set the width and height in the oled.drawBitmap(); function.
OLED Troubleshooting
If the OLED screen is blank, please do these steps:
- Make sure the wiring is correct.
- Check that your I2C OLED uses an SSD1306 driver.
- Find the I2C address of your OLED using the I2C Address Scanner code for Arduino Mega.
What you see in the Serial Monitor: