I'm using the ssd1306_128x64_i2c example file from the 1306's library.

It works perfectly until I add the #include SD.h line, after which it gives the allocation failed error.

Please advise.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SD.h>

Adafruit_SSD1306 display(128, 64, &Wire, 4);

void setup() {

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));


void loop() {

void testdrawchar(void) {
  display.setCursor(0, 0);     

  for(int16_t i=0; i<256; i++) {
    if(i == '\n') display.write(' ');
    else          display.write(i);
  • I had this exact same problem and was glad to see someone had already asked this question. It is quite shocking that you cannot use SD.h and the adafruit oled library at the same time.
    – raddevus
    Commented Jun 8, 2021 at 12:16

2 Answers 2


I was able to make it all fit by using the text-only library for the OLED.

SSD1306Ascii Library

Much thanks to Majenko for explaining the problem and greiman for writing the library!


Both the SD library and the SSD1306 libraries want big buffers. That means lots of memory. You don't have lots of memory.

  • The SD wants at least 512 bytes for a sector buffer (which is allocated at compile time).
  • The SSD1306 wants at least 1kiB for a display buffer (which is allocated at runtime).
  • Serial wants at least 128 bytes for TX and RX buffers (which is allocated at compile time).
  • The Wire library needs 32 bytes for a TX buffer.

That's 1696 bytes of a total 2048, and that's not taking into account any of the other variables and things that your program, the various libraries, and the Arduino core itself needs, plus the stack (and its specified minimum size safety zone).

There's nothing much you can do about it, short of upgrading your Arduino to one with more memory, or splitting your design across multiple Arduinos.

  • 1
    Is there a way to know exactly how much memory I am over by at runtime?
    – cds333
    Commented Mar 21, 2019 at 22:12
  • 1
    Not really, no. All you know is that trying to allocate the memory failed.
    – Majenko
    Commented Mar 21, 2019 at 22:15
  • There is somewhere a "free memory" example / library that can tell you how much free memory there is, though, which could help.
    – Majenko
    Commented Mar 21, 2019 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.