Arduino Uno SSD1306 Allocation Failed - Need help downsizing

Hi everyone,

i'm relatively new to programming arduinos and right now i need some help to get my 128x32 OLED display working.
It works fine in a standalone solution, but when i implement it in my whole project code, it shows "Allocation failed". Research on here showed me that the problem is most likely caused by not having enough available RAM memory.
I've improved the things i could, but most topics solutions are very specific, so i hope you can help me downsize my code.

I am using an Arduino Uno.

Thank you and have a great day.

-Maik

unsigned long currentTime;

//Button
  byte buttonPin = 9;
  int butRead;

//Wasserpumpe
  const byte pumpPin=4;

//Temperatur- & Feuchtigkeitssensor DHT22:
  #include <DHT.h>
  #define DHTTYPE DHT22
  const byte DHTPin=8;
  DHT dht(DHTPin, DHTTYPE);
  const unsigned long eventTime_1=1000;
  unsigned long previousTime_1 = 0;
  float h;
  float t;

//Bodenfeuchtigkeit:
  const byte moistPin=A0;
  int sensValMoist;
  int dry=615;
  int wet=241;
  int moistPercentage;
  const unsigned long eventTime_2=1000;
  unsigned long previousTime_2 = 0;

//Wasserzerstaeuber
  const byte waterPin=1;

//RTC (Real-Time-Clock)
  #include <Wire.h>
  #include <RTClib.h>
  RTC_DS1307 rtc;
  DateTime now;
//SD Card
  #include <SD.h>
  #include <SPI.h>
  File myFile;
  const byte pinCS=10;
  const unsigned long eventTime_3=10000;
  unsigned long previousTime_3 = 0;

//Display 128X32
  #include <Adafruit_GFX.h>
  #include <Adafruit_SSD1306.h>
  #define SCREEN_WIDTH 128          // OLED display width, in pixels
  #define SCREEN_HEIGHT 32          // OLED display height, in pixels 
  #define OLED_RESET     -1         // Reset pin # (or -1 if sharing Arduino reset pin)
  #define SCREEN_ADDRESS 0x3C       ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  
void setup() {
  Serial.begin(9600);
  analogReference(EXTERNAL);
  dht.begin();
  pinMode(moistPin,INPUT);
  pinMode(buttonPin,INPUT);
  pinMode(DHTPin,INPUT);
  pinMode(waterPin,OUTPUT);
  pinMode(pumpPin,OUTPUT);
  pinMode(pinCS,OUTPUT);
    digitalWrite(pinCS,HIGH);
  //RTC (Real-Time-Clock)
    rtc.begin();
    Wire.begin();
    if(!rtc.begin()){
      Serial.println("Couldn't find RTC");
    }
    if(!rtc.isrunning()){
      Serial.println("RTC is NOT running");
    }
    //rtc.adjust(DateTime(__DATE__, __TIME__));         //Zeit/Datum neu justieren
  
  //SD Card
    if (SD.begin()){
      Serial.println("SD card is ready to use.");
      } else {
        Serial.println("SD card initialization failed");
        }
    // SD.remove("datalog.txt");                           //Deleting Textfile

  //Display 128x32
   if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);                            // Don't proceed, loop forever
   }

}

void loop() {
  //Temperatur- & Feuchtigkeitssensor DHT22:
    currentTime = millis();
    if (currentTime - previousTime_1 >= eventTime_1){
      h = dht.readHumidity();
      t = dht.readTemperature();
      Serial.print("Humidity: ");
      Serial.print(h);
      Serial.print("%   ");
      Serial.print("Temperature: ");
      Serial.print(t);
      Serial.print("°C    ");
      previousTime_1 = currentTime;
    }

  //Bodenfeuchtigkeit:
    currentTime = millis();
    if(currentTime - previousTime_2 >= eventTime_2){
      sensValMoist = analogRead(moistPin);
      moistPercentage=map(sensValMoist, dry, wet, 0, 100);
      if(moistPercentage<0){
        moistPercentage=0;
        }
      Serial.print("Moisture: ");  
      Serial.print(moistPercentage);
      Serial.println("%   ");
      previousTime_2 = currentTime;
    }
  
  //ButtonPin
    butRead = digitalRead(buttonPin);
    if(butRead==1){
      // digitalWrite(waterPin,HIGH);}                //Wasserzerstaeuber
      // else{
      //   digitalWrite(waterPin,LOW);
      digitalWrite(pumpPin,HIGH);}                    //Pumpe
    else{
        digitalWrite(pumpPin,LOW);
        }
  //RTC
    now=rtc.now();
    // if(now.day()<10){                              //Uhrzeit anzeigen
    //   Serial.print("0");
    //   }
    // Serial.print(now.day(), DEC);
    // Serial.print("/");
    // if(now.month()<10){
    //   Serial.print("0");
    //   }
    // Serial.print(now.month(), DEC);
    // Serial.print("/");
    // Serial.print(now.year(), DEC);
    // Serial.print("  ");
    // if(now.hour()<10){
    //   Serial.print("0");
    //   }
    // Serial.print(now.hour(),DEC);
    // Serial.print(":");
    // if(now.minute()<10){
    //   Serial.print("0");
    //   }
    // Serial.print(now.minute(),DEC);
    // Serial.print(":");
    // if(now.second()<10){
    //   Serial.print("0");
    //   }
    // Serial.print(now.second(),DEC);
    // Serial.println();

  //SD Card
    currentTime = millis();
    if (currentTime - previousTime_3 >= eventTime_3){
      myFile = SD.open("datalog.txt",FILE_WRITE);         //Create/Open file
      if (myFile){                                        //if the file opened okay, write to it
        if(now.day()<10){ 
          myFile.print("0");
        }
        myFile.print(now.day(),DEC);
        myFile.print("  ");
        if(now.month()<10){ 
          myFile.print("0");
        }
        myFile.print(now.month(),DEC);
        myFile.print("  ");
        myFile.print(now.year(),DEC);
        myFile.print("  ");
        if(now.hour()<10){ 
          myFile.print("0");
        }
        myFile.print(now.hour(),DEC);
        myFile.print("  ");
        if(now.minute()<10){ 
          myFile.print("0");
        }
        myFile.print(now.minute(),DEC);
        myFile.print("  ");
        if(now.second()<10){ 
          myFile.print("0");
        }
        myFile.print(now.second(),DEC);
        myFile.print("  ");
        myFile.print(t);
        myFile.print("°C");
        myFile.print("  ");
        myFile.print(h);
        myFile.print("%");
        myFile.print("  ");
        myFile.print(moistPercentage);
        myFile.print("%");
        myFile.println();
        
        myFile.close();
        }
        else{                                            //if the file didn't open, print an error
          Serial.println("Error opening file");
        }
        previousTime_3=currentTime;
    }
    // // myFile=SD.open("datalog.txt");                   //Reading the file
    // // if(myFile){
    // // Serial.println("Read: ");
    // // while (myFile.available()){                      //Reading the whole file
    // //  Serial.write(myFile.read());
    // //  }
    // // myFile.close();
    // // }
    // // else{
    // // Serial.println("Error opening file");
    // // }
  
  //Display 128x32
    display.clearDisplay();         // Clear display buffer

    display.drawLine(40, 0, 40, display.height()-1, SSD1306_WHITE);
    display.drawLine(86, 0, 86, display.height()-1, SSD1306_WHITE); 
    display.setTextColor(SSD1306_WHITE); // Draw white text
    display.cp437(true);          // Use full 256 char 'Code Page 437' font
    
    display.setTextSize(1);       // Normal 1:1 pixel scale
    display.setCursor(6, 0);
    display.write("Temp");
    display.setTextSize(2);      
    display.setCursor(7, 15);    
    display.write("22");
    display.setTextSize(1);
    display.write(248);
     
    display.setTextSize(1);
    display.setCursor(54, 0);
    display.write("Hum");
    display.setTextSize(2);     
    display.setCursor(51, 15);     
    display.write("70");
    display.setTextSize(1);
    display.write(37);

    display.setTextSize(1);
    display.setCursor(96, 0);
    display.write("Moist");
    display.setTextSize(2);      
    display.setCursor(97, 15);    
    display.write("30");
    display.setTextSize(1);
    display.write(37);
    
    display.display();                // Update screen with each newly-drawn line


delay(500);
}

You forgot to say what type of Arduino you are using, which is important to know, especially when RAM memory might be the problem. I will assume you are using an Uno.

Uno has very limited RAM memory, only 2KB. OLED displays and SD card library both require large amounts of RAM memory. I agree RAM memory is probably the problem.

You could try a different library for the OLED. U8G2 can use significantly less RAM memory than the Adafruit library.

If you are OK with displaying only text on the OLED (no graphics) then you can consider using U8X8 library (which is installed with U8G2). This will require even less RAM memory.

Sorry, i am using an Uno.

Text is mostly done. Do simple lines count as text or graphic?

Lines count as graphic. Only text counts as text.

However, you maybe could print a line of underscores like "______________".

Another way to save some RAM is to make more use of the F() macro. Your code uses the F() macro in some places but not others. For example it is used here

Serial.println(F("SSD1306 allocation failed"));

but not here

Serial.println("SD card initialization failed");

So i have changed quite a few lines to the F() macro version. Unfortunately it is still not enough.
For visual reason i need some vertical lines, so i don't want to use the text-only libraries.
I've tested to //comment the SD-card section in the void loop function, and with that the display works fine!
So i guess we need to make a little more space.

Arduino IDE tells me:
Without SD-function: 1307 bytes of dynamic memory (63%).
With SD-function: 1353 bytes of dynamic memory (66%).

So i guess we are not too far off.

Any more ideas?

Updated code:

unsigned long currentTime;

//Button
  byte buttonPin = 9;
  int butRead;

//Wasserpumpe
  const byte pumpPin=4;

//Temperatur- & Feuchtigkeitssensor DHT22:
  #include <DHT.h>
  #define DHTTYPE DHT22
  const byte DHTPin=8;
  DHT dht(DHTPin, DHTTYPE);
  const unsigned long eventTime_1=1000;
  unsigned long previousTime_1 = 0;
  int h;
  char hStr[5];
  int t;
  char tStr[5];

//Bodenfeuchtigkeit:
  const byte moistPin=A0;
  int sensValMoist;
  int dry=615;
  int wet=241;
  int moistPercentage;
  char moistStr[5];
  const unsigned long eventTime_2=1000;
  unsigned long previousTime_2 = 0;

//Wasserzerstaeuber
  const byte waterPin=1;

//RTC (Real-Time-Clock)
  #include <Wire.h>
  #include <RTClib.h>
  RTC_DS1307 rtc;
  DateTime now;
//SD Card
  #include <SD.h>
  #include <SPI.h>
  File myFile;
  const byte pinCS=10;
  const unsigned long eventTime_3=10000;
  unsigned long previousTime_3 = 0;

//Display 128X32
  #include <Adafruit_GFX.h>
  #include <Adafruit_SSD1306.h>
  #define SCREEN_WIDTH 128          // OLED display width, in pixels
  #define SCREEN_HEIGHT 32          // OLED display height, in pixels 
  #define OLED_RESET     -1         // Reset pin # (or -1 if sharing Arduino reset pin)
  #define SCREEN_ADDRESS 0x3C       ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  
void setup() {
  Serial.begin(9600);
  analogReference(EXTERNAL);
  dht.begin();
  pinMode(moistPin,INPUT);
  pinMode(buttonPin,INPUT);
  pinMode(DHTPin,INPUT);
  pinMode(waterPin,OUTPUT);
  pinMode(pumpPin,OUTPUT);
  pinMode(pinCS,OUTPUT);
    digitalWrite(pinCS,HIGH);
  //RTC (Real-Time-Clock)
    rtc.begin();
    Wire.begin();
    if(!rtc.begin()){
      Serial.println(F("Couldn't find RTC"));
    }
    if(!rtc.isrunning()){
      Serial.println(F("RTC is NOT running"));
    }
    //rtc.adjust(DateTime(__DATE__, __TIME__));         //Zeit/Datum neu justieren
  
  //SD Card
    if (SD.begin()){
      Serial.println("SD card is ready to use.");
      } else {
        Serial.println("SD card initialization failed");
        }
    // SD.remove("datalog.txt");                           //Deleting Textfile

  //Display 128x32
   if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);                            // Don't proceed, loop forever
   }

}

void loop() {
  //Temperatur- & Feuchtigkeitssensor DHT22:
    currentTime = millis();
    if (currentTime - previousTime_1 >= eventTime_1){
      h = dht.readHumidity();
      t = dht.readTemperature();
      Serial.print(F("Humidity: "));
      Serial.print(h);
      Serial.print(F("%   "));
      Serial.print(F("Temperature: "));
      Serial.print(t);
      Serial.print(F("°C    "));
      previousTime_1 = currentTime;
    }

  //Bodenfeuchtigkeit:
    currentTime = millis();
    if(currentTime - previousTime_2 >= eventTime_2){
      sensValMoist = analogRead(moistPin);
      moistPercentage=map(sensValMoist, dry, wet, 0, 100);
      if(moistPercentage<0){
        moistPercentage=0;
        }
      Serial.print(F("Moisture: "));  
      Serial.print(moistPercentage);
      Serial.println(F("%   "));
      previousTime_2 = currentTime;
    }
  
  //ButtonPin
    butRead = digitalRead(buttonPin);
    if(butRead==1){
      // digitalWrite(waterPin,HIGH);}                //Wasserzerstaeuber
      // else{
      //   digitalWrite(waterPin,LOW);
      digitalWrite(pumpPin,HIGH);}                    //Pumpe
    else{
        digitalWrite(pumpPin,LOW);
        }
  //RTC
    now=rtc.now();
    // if(now.day()<10){                              //Uhrzeit anzeigen
    //   Serial.print("0");
    //   }
    // Serial.print(now.day(), DEC);
    // Serial.print("/");
    // if(now.month()<10){
    //   Serial.print("0");
    //   }
    // Serial.print(now.month(), DEC);
    // Serial.print("/");
    // Serial.print(now.year(), DEC);
    // Serial.print("  ");
    // if(now.hour()<10){
    //   Serial.print("0");
    //   }
    // Serial.print(now.hour(),DEC);
    // Serial.print(":");
    // if(now.minute()<10){
    //   Serial.print("0");
    //   }
    // Serial.print(now.minute(),DEC);
    // Serial.print(":");
    // if(now.second()<10){
    //   Serial.print("0");
    //   }
    // Serial.print(now.second(),DEC);
    // Serial.println();

  //SD Card
    currentTime = millis();
    if (currentTime - previousTime_3 >= eventTime_3){
      myFile = SD.open("datalog.txt",FILE_WRITE);         //Create/Open file
      if (myFile){                                        //if the file opened okay, write to it
        if(now.day()<10){ 
          myFile.print("0");
        }
        myFile.print(now.day(),DEC);
        myFile.print("  ");
        if(now.month()<10){ 
          myFile.print("0");
        }
        myFile.print(now.month(),DEC);
        myFile.print("  ");
        myFile.print(now.year(),DEC);
        myFile.print("  ");
        if(now.hour()<10){ 
          myFile.print("0");
        }
        myFile.print(now.hour(),DEC);
        myFile.print("  ");
        if(now.minute()<10){ 
          myFile.print("0");
        }
        myFile.print(now.minute(),DEC);
        myFile.print("  ");
        if(now.second()<10){ 
          myFile.print("0");
        }
        myFile.print(now.second(),DEC);
        myFile.print("  ");
        myFile.print(t);
        myFile.print("°C");
        myFile.print("  ");
        myFile.print(h);
        myFile.print("%");
        myFile.print("  ");
        myFile.print(moistPercentage);
        myFile.print("%");
        myFile.println();
        
        myFile.close();
        }
        else{                                            //if the file didn't open, print an error
          Serial.println("Error opening file");
        }
        previousTime_3=currentTime;
    }
    // // myFile=SD.open("datalog.txt");                   //Reading the file
    // // if(myFile){
    // // Serial.println("Read: ");
    // // while (myFile.available()){                      //Reading the whole file
    // //  Serial.write(myFile.read());
    // //  }
    // // myFile.close();
    // // }
    // // else{
    // // Serial.println("Error opening file");
    // // }
  
  //Display 128x32
    display.clearDisplay();         // Clear display buffer

    display.drawLine(40, 0, 40, display.height()-1, SSD1306_WHITE);
    display.drawLine(86, 0, 86, display.height()-1, SSD1306_WHITE); 
    display.setTextColor(SSD1306_WHITE); // Draw white text
    display.cp437(true);          // Use full 256 char 'Code Page 437' font
    
    itoa(t, tStr, 10);
    display.setTextSize(1);       // Normal 1:1 pixel scale
    display.setCursor(6, 0);
    display.write("Temp");
    display.setTextSize(2);      
    display.setCursor(7, 15);    
    display.write(tStr);
    display.setTextSize(1);
    display.write(248);

    itoa(h, hStr, 10);
    display.setTextSize(1);
    display.setCursor(54, 0);
    display.write("Hum");
    display.setTextSize(2);     
    display.setCursor(51, 15);     
    display.write(hStr);
    display.setTextSize(1);
    display.write(37);

    itoa(moistPercentage, moistStr, 10);
    display.setTextSize(1);
    display.setCursor(96, 0);
    display.write("Moist");
    display.setTextSize(2);      
    display.setCursor(97, 15);    
    display.write(moistStr);
    display.setTextSize(1);
    display.write(37);
    
    display.display();                // Update screen with each newly-drawn line


delay(500);
}

More like you are a long way off, the compiler is just showing the amount of memory the static program uses. When the display and SD starts up they allocate a large amount of memory for buffers.

I would suggest that the chances of getting that particular OLED library and the SD library working together on a UNO are slim.

As I recall that Adafruit library needs maybe a 1024byte buffer, the SD needs a 512 byte buffer, so at run time the buffers need 75% of the Unos RAM.

Use a different Arduino.

That is for a 128 x 64 display, the OP has a 128 x 32, so it would be 512 bytes.
The U8g2 library, using a paged buffer, generally uses much less ram (although the compiler will show more, because the buffer is allocated at compile-time).

Thanks for the correction.

So based on that, and assuming you keep to the 25% free memory for stability reasons, the static program would need to reduce memory use from 66% down to 25% .........................

Thank you for the answers, i didn't know it takes up so much space.
I will try to use the text-only library later (and try to workaround vertical lines with the letter "I").
I'll keep you updated.

Not all of them; the file.prints can be modified, you missed some in setup() and the ones like below as well

Instead of writing text as below

display.write("Temp");

print text as below

  display.print(F("Temp"));

The above saves 5 bytes

In total I can save about 100 bytes; possibly not enough.

Sometimes you have to :wink: Underscores were already mentioned, pipe signs might be OK as well for columns? And else a different Arduino.

1 Like

If you can find a text-only library that implements the cp437 font you are using, that has numerous characters for vertical and horizontal lines, box corners, etc, that were used for rudimentary graphics on the old text-only monitors.

I think you need to make a decision about what type of Arduino to use. There are many other types, and almost all of them have more RAM than Uno, in some cases, much much more. One of these may be the easiest solution for you, even though there may be costs and delays.

If you choose to continue with Uno, there will be no extra costs, but there could be even longer delays because optimising your code to use less and less RAM will become more and more difficult.

For example, switching from the Adafruit OLED library to the U8G2 library could reduce the RAM taken by the OLED by half or even 75%. But it will also involve re-writing your code to be able to update the OLED in a different way. U8G2 has the ability to update the OLED one section at a time, meaning less RAM is needed, but your code can no longer update any part of the screen at any time. Instead, it must completely update the whole screen each time even when only a small change is needed.

Since this the last part of my project, i will try to make it work with the Uno.
For future projects i will definitely upgrade to another board.

I worked out a solution with the u8g2 library, which is less visually pleasing, but fine enough.
The display works fine in a standalone sketch. But as soon as i try to integrate it into my whole sketch, it runs into issues.
I did some trial&error testing and noticed, that as soon as i include "#include <Arduino.h>, it won't let me compile anymore. There are tons of errors like not recognizing the data type "byte" anymore and many many more. If i //comment this library, the code will upload (but the display won't work)...

unsigned long currentTime;

//Button
  byte buttonPin = 9;
  int butRead;

//Wasserpumpe
  const byte pumpPin=4;

//Temperatur- & Feuchtigkeitssensor DHT22:
  #include <DHT.h>
  #define DHTTYPE DHT22
  const byte DHTPin=8;
  DHT dht(DHTPin, DHTTYPE);
  const unsigned long eventTime_1=1000;
  unsigned long previousTime_1 = 0;
  int h;
  char hStr[5];
  int t;
  char tStr[5];

//Bodenfeuchtigkeit:
  const byte moistPin=A0;
  int sensValMoist;
  int dry=615;
  int wet=241;
  int moistPercentage;
  char moistStr[5];
  const unsigned long eventTime_2=1000;
  unsigned long previousTime_2 = 0;

//Wasserzerstaeuber
  const byte waterPin=1;

//RTC (Real-Time-Clock)
  #include <Wire.h>
  #include <RTClib.h>
  RTC_DS1307 rtc;
  DateTime now;
  
//SD Card
  #include <SD.h>
  #include <SPI.h>
  File myFile;
  const byte pinCS=10;
  const unsigned long eventTime_3=10000;
  unsigned long previousTime_3 = 0;

//Display 128X32
  // #include <Arduino.h>
  #include <U8x8lib.h>
  #ifdef U8X8_HAVE_HW_SPI
  #include <SPI.h>
  #endif
  U8X8_SSD1306_128X32_UNIVISION_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
  
void setup() {
  Serial.begin(9600);
  analogReference(EXTERNAL);
  dht.begin();
  pinMode(moistPin,INPUT);
  pinMode(buttonPin,INPUT);
  pinMode(DHTPin,INPUT);
  pinMode(waterPin,OUTPUT);
  pinMode(pumpPin,OUTPUT);
  pinMode(pinCS,OUTPUT);
    digitalWrite(pinCS,HIGH);
  //RTC (Real-Time-Clock)
    rtc.begin();
    Wire.begin();
    if(!rtc.begin()){
      Serial.println(F("Couldn't find RTC"));
    }
    if(!rtc.isrunning()){
      Serial.println(F("RTC is NOT running"));
    }
    //rtc.adjust(DateTime(__DATE__, __TIME__));         //Zeit/Datum neu justieren
  
  //SD Card
    if (SD.begin()){
      Serial.println(F("SD card is ready to use."));
      } else {
        Serial.println(F("SD card initialization failed"));
        }
    // SD.remove("datalog.txt");                           //Deleting Textfile

  //Display 128x32
    u8x8.begin();
    u8x8.setPowerSave(0);

}

void loop() {
  //Temperatur- & Feuchtigkeitssensor DHT22:
    currentTime = millis();
    if (currentTime - previousTime_1 >= eventTime_1){
      h = dht.readHumidity();
      t = dht.readTemperature();
      Serial.print(F("Humidity: "));
      Serial.print(h);
      Serial.print(F("%   "));
      Serial.print(F("Temperature: "));
      Serial.print(t);
      Serial.print(F("°C    "));
      previousTime_1 = currentTime;
    }

  //Bodenfeuchtigkeit:
    currentTime = millis();
    if(currentTime - previousTime_2 >= eventTime_2){
      sensValMoist = analogRead(moistPin);
      moistPercentage=map(sensValMoist, dry, wet, 0, 100);
      if(moistPercentage<0){
        moistPercentage=0;
        }
      Serial.print(F("Moisture: "));  
      Serial.print(moistPercentage);
      Serial.println(F("%   "));
      previousTime_2 = currentTime;
    }
  
  //ButtonPin
    butRead = digitalRead(buttonPin);
    if(butRead==1){
      // digitalWrite(waterPin,HIGH);}                //Wasserzerstaeuber
      // else{
      //   digitalWrite(waterPin,LOW);
      digitalWrite(pumpPin,HIGH);}                    //Pumpe
    else{
        digitalWrite(pumpPin,LOW);
        }
  //RTC
    now=rtc.now();
    // if(now.day()<10){                              //Uhrzeit anzeigen
    //   Serial.print("0");
    //   }
    // Serial.print(now.day(), DEC);
    // Serial.print("/");
    // if(now.month()<10){
    //   Serial.print("0");
    //   }
    // Serial.print(now.month(), DEC);
    // Serial.print("/");
    // Serial.print(now.year(), DEC);
    // Serial.print("  ");
    // if(now.hour()<10){
    //   Serial.print("0");
    //   }
    // Serial.print(now.hour(),DEC);
    // Serial.print(":");
    // if(now.minute()<10){
    //   Serial.print("0");
    //   }
    // Serial.print(now.minute(),DEC);
    // Serial.print(":");
    // if(now.second()<10){
    //   Serial.print("0");
    //   }
    // Serial.print(now.second(),DEC);
    // Serial.println();

  //SD Card
    // currentTime = millis();
    // if (currentTime - previousTime_3 >= eventTime_3){
    //   myFile = SD.open("datalog.txt",FILE_WRITE);         //Create/Open file
    //   if (myFile){                                        //if the file opened okay, write to it
    //     if(now.day()<10){ 
    //       myFile.print("0");
    //     }
    //     myFile.print(now.day(),DEC);
    //     myFile.print("  ");
    //     if(now.month()<10){ 
    //       myFile.print("0");
    //     }
    //     myFile.print(now.month(),DEC);
    //     myFile.print("  ");
    //     myFile.print(now.year(),DEC);
    //     myFile.print("  ");
    //     if(now.hour()<10){ 
    //       myFile.print("0");
    //     }
    //     myFile.print(now.hour(),DEC);
    //     myFile.print("  ");
    //     if(now.minute()<10){ 
    //       myFile.print("0");
    //     }
    //     myFile.print(now.minute(),DEC);
    //     myFile.print("  ");
    //     if(now.second()<10){ 
    //       myFile.print("0");
    //     }
    //     myFile.print(now.second(),DEC);
    //     myFile.print("  ");
    //     myFile.print(t);
    //     myFile.print("°C");
    //     myFile.print("  ");
    //     myFile.print(h);
    //     myFile.print("%");
    //     myFile.print("  ");
    //     myFile.print(moistPercentage);
    //     myFile.print("%");
    //     myFile.println();
        
    //     myFile.close();
    //     }
    //     else{                                            //if the file didn't open, print an error
    //       Serial.println("Error opening file");
    //     }
    //     previousTime_3=currentTime;
    // }
    // myFile=SD.open("datalog.txt");                   //Reading the file
    // if(myFile){
    // Serial.println("Read: ");
    // while (myFile.available()){                      //Reading the whole file
    //  Serial.write(myFile.read());
    //  }
    // myFile.close();
    // }
    // else{
    // Serial.println("Error opening file");
    // }
  
  //Display 128x32
        
    itoa(t, tStr, 10);
    itoa(h, hStr, 10);
    itoa(moistPercentage, moistStr, 10);

    u8x8.setFont(u8x8_font_courB18_2x3_r);  
    u8x8.drawString(1,1,tStr);                    //Für Zeichen: "\xb0" ASCII dez(176) = hex(b0) = Grad-Zeichen = °
    u8x8.drawString(7,1,hStr);
    u8x8.drawString(12,1,moistStr);

    u8x8.setFont(u8x8_font_chroma48medium8_r);
    u8x8.drawString(1,0,"Temp");
    u8x8.drawString(7,0,"Humi");
    u8x8.drawString(12,0,"Mois");

delay(500);
}

This is not the correct constructor. You are using hardware i2c and do not need to specify SDA and SCL.

I'm using this exact constructor in the working standalone sketch..
And it compiles fine when i //comment the Arduino library line.

In which, I assume, SDA and SCL are defined.

In a sketch where SDA and SCL are not defined?

SDA and SCL are already defined in pins_arduino.h, so the sketch is running software I2C on the same pins that would be used for hardware I2C.

1 Like

I learned something new, thanks.

@ma_wu sorry for doubting that your sketch compiled!

However, I still think that is not the correct constructor for your sketch. It uses software i2c and this may cause problems because you are using hardware i2c for RTC.

You should try this one:

U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8();

You could also try

U8X8_SSD1306_128X32_WINSTAR_HW_I2C u8x8();
1 Like

You`re not using , Arduino IDE?

After another several hours of trial and error, i finally got the SD together with the display working!

Things that worked, but which i do not understand:

  • Changed all "byte" types to "int". For some weird twisted reason, as soon as i #include <Arduino.h> in the sketch, it doesn't accept "byte" types anymore. I removed all of them.
  • I used the other constructor which PaulRB suggested. I cannot explain why the original constructor works in a standalone sketch, but not in my integrated one. The other one worked in both.

Thank you so much everyone, this was much more work than i anticipated. Great to have guys like you ready to help!

I will be upgrading to a bigger board for my next project.
Which MCU would you suggest? Arduino Mega?

Have a great day.

-Maik