Sub topic 11
How much RAM the OLED frame buffer uses
Prerequisites and the memory model
This chapter assumes you can upload an Arduino sketch, use setup() and loop(), and install the Adafruit GFX and SSD1306 libraries.
An OLED frame buffer is a block of RAM that stores the pixels to be shown. The Arduino draws into this block first. The SSD1306 library then sends the block to the OLED when your code calls display.display().
For a monochrome OLED, each pixel needs one bit:
0means the pixel is off.1means the pixel is on.- Eight pixels fit in one byte.
The basic formula is:
frame buffer bytes = width × height ÷ 8For a 128×64 display:
128 × 64 ÷ 8 = 1024 bytesThat is 1 KB of RAM for pixels alone. The frame buffer does not include the Arduino sketch stored in flash memory, the display object, the serial buffer, other variables, or the program stack.
Common OLED sizes
| OLED resolution | Calculation | Frame buffer |
|---|---|---|
| 128×32 | 128 × 32 ÷ 8 | 512 bytes |
| 128×64 | 128 × 64 ÷ 8 | 1024 bytes |
| 64×48 | 64 × 48 ÷ 8 | 384 bytes |
The Arduino Uno has 2 KB, or 2048 bytes, of SRAM. A 128×64 buffer uses half of that memory before your own variables and library state are counted. A 128×32 buffer uses one quarter.
The buffer size depends on resolution, not on the number of pixels currently lit. A nearly empty screen still needs the complete buffer because the library must know the state of every pixel.
What happens when the display starts
For a typical SSD1306 sketch, the display object reserves a buffer for the selected width and height. Calls such as setCursor(), print(), and drawLine() change data in that buffer. They do not immediately update the OLED.
The display.display() call transfers the buffer to the display. This design makes complex drawings possible, but it requires enough SRAM for the entire image.
flowchart TD
A[Arduino SRAM] --> B[SSD1306 frame buffer]
B --> C[Text and shapes are drawn]
C --> D[display.display()]
D --> E[OLED controller]
E --> F[Pixels appear on screen]caption: How the OLED frame buffer moves from Arduino RAM to the display alt: A flowchart showing Arduino SRAM holding a frame buffer, drawing operations changing it, and display.display sending it to the OLED controller
You can calculate the pixel memory before choosing a display:
const uint16_t OLED_WIDTH = 128;
const uint16_t OLED_HEIGHT = 64;
void setup() {
Serial.begin(9600);
const uint32_t bufferBytes =
static_cast<uint32_t>(OLED_WIDTH) * OLED_HEIGHT / 8;
Serial.print(F("Frame buffer bytes: "));
Serial.println(bufferBytes);
}
void loop() {}This sketch prints Frame buffer bytes: 1024. The uint32_t calculation avoids a narrow intermediate type if you later use a larger display.
Keeping enough RAM available
A display buffer is only one part of the memory budget. Also account for:
- Global and static variables.
- Local variables created while functions run.
- The call stack, which stores temporary function data.
- Library buffers, including communication buffers.
- Strings and arrays used by your application.
A program can compile and upload successfully while still running out of SRAM. Symptoms include random resets, corrupted text, strange sensor readings, or behavior that changes when you add an unrelated variable.
On AVR-based boards such as the Uno, keep large text and constant messages in flash with the F() macro:
Serial.println(F("This text stays in flash memory"));This reduces the copy of that constant text stored in SRAM. It does not reduce the OLED frame buffer.
Estimating before wiring
Use the resolution and buffer formula as an early design check. For a 128×64 monochrome OLED on an Uno, begin with 1024 bytes for the frame buffer. Reserve additional space for the sketch's variables and libraries. Leave a safety margin instead of planning to use every remaining byte.
Exercise
How many bytes does a 128×32 monochrome OLED frame buffer require, and what fraction of an Arduino Uno's 2048 bytes of SRAM is that?
Show answer
It requires 128 × 32 ÷ 8 = 512 bytes, which is one quarter of the Uno's SRAM.