0

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() {
  Serial.begin(9600);

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

  display.clearDisplay();
  display.display();
  testdrawchar();      
}

void loop() {
}

void testdrawchar(void) {
  display.clearDisplay();
  display.setTextSize(1);      
  display.setTextColor(WHITE); 
  display.setCursor(0, 0);     
  display.cp437(true);         

  for(int16_t i=0; i<256; i++) {
    if(i == '\n') display.write(' ');
    else          display.write(i);
  }
  display.display();
}
1

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!

0

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.

  • Is there a way to know exactly how much memory I am over by at runtime? – cds333 Mar 21 at 22:12
  • Not really, no. All you know is that trying to allocate the memory failed. – Majenko Mar 21 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 Mar 21 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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