Global variables uses 110% of memory

Hello guys,

i' struggling with to much memory for global variables. So the compiling runs to an error. But my scretch is not that big (see below). The global variables are using 2264 Bytes (110%). My language skills are either beginner level but where is all the memory used to hide?

My hardware: Arduino Uno, a data logger with real time clock (Shield), a DHT11 and a OLED. At least the hardware is not important because the IDE is displaying the error before flashing.

My code is:


 //Bibliotheken für das Display
#include <GyverOLED.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// Bibliotheken für den TempFechte Sendor
#include "DHT.h" 

// Bibliotheken für das Shield
#include "RTClib.h" // Einbinden der Adafruit DS1307 RTC Bibliothek
#include <SD.h> // Einbinden der SD Bibliothek
#include <SPI.h> // Einbinden der SD Bibliothe
#include <Wire.h> // Einbinden der SPI Bibliothek

// Konfiguration des DHT11 sensors
#define DHTPIN 2 
#define DHTTYPE DHT11    
DHT dht(DHTPIN, DHTTYPE);


// Konfigurieren der OLED
GyverOLED<SSH1106_128x64> oled;


// Konfigurieren vom Shield
RTC_DS1307 rtc;
DateTime now;
File dataFile;
const int shield_pin = 10; // Für Datenlogger Modul am Arduino Uno so lassen

void setup() {
  
  Serial.begin(9600);
  oled.init();            // initialize the OLED
  dht.begin();     // initialize the sensor
  rtc.begin();            // initialize real time clock

  // Check if RTC is working and ready to run
  Serial.println(F("Initializing Real time clock....."));
  if (!rtc.isrunning()) {
    Serial.println(F("RTC is NOT running!"));
    // RTC is adjusted to current time
    rtc.adjust(DateTime(__DATE__, __TIME__));
  }

  // Check if SD is working and ready to run
  Serial.print(F("Initializing SD Card....."));
  if (!SD.begin(shield_pin)) {
    Serial.println("SD card failed or not present!");
    return;
  }
  // Create or open log file and set header
  dataFile = SD.open("datalog.csv", FILE_WRITE);
  dataFile.print("Datum");
  dataFile.print(",");
  dataFile.println("Temperatur");

  Serial.println(F("SD card initialized."));
  delay(2000);         // wait for initializing
}

void loop() {
  int hum = dht.readHumidity(); 
  float temp = dht.readTemperature();
  

  
  Serial.print(temp);
  Serial.print(",");
  Serial.println(hum);                     
  
  
  display_on_oled(temp);                                 

  delay(1000);
}

// Show the temperature on OLED display
void display_on_oled(float temp){
  oled.clear();
  oled.home();
  oled.setScale(3);
  oled.setCursor(0, 3);
  oled.print(temp);
  oled.circle(100, 27, 3);
  oled.setScale(3);
  oled.setCursor(108, 3);
  oled.print("C");
  oled.update();
}

Presumably, your libraries (and you use quite a few) are consuming too much memory.

in the libraries:

and

How much does the GyverOled take?
Does it have a 126x64 int buffer to store what should be printed to the oled?
That would need more than 2400 byte...
If so, for what controller was it written?(esp like?)

I agree with above that SD library is also quite heavy on UNO like boards. But I think it is more heavy on PROGMEM than on globals.

you have thousents of librarys that has high number of variables

You seem to be only printing ASCII text, consider the "SSD1306Ascii" text only library from Bill Greiman. It is very small and will eliminate you need for:

#include <GyverOLED.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

read about the F-Makro and adopt all your .prints with longer fix text.
Using the F-Makro and you will free some SRAM - in your case not enough, but it is always a good idea to apply the F-Makro consequently.

Further more: something like

dataFile.print(',');

instead of

dataFile.print(",");

for a single character will save one byte.

Hi guys,
I am realy thankful for your quick responses. I took the Gyer lib out of code and compiling is now possible. Thanks for the hint. I didn't know that the libs belong to the global variable.

My OLED is not responding. I guess I have to stack the wires in another way. But I will research a little more in order the solve the current issue.

The SSD1306Ascii libary sounds realy nice but the example scratches are quite hard to understand to me. If I understand it correctly than I have to configure the I2C adress with 0x3D because I have a 128x64 size. What means the RST_PIN = -1? Does that mean something important to the Arduino?

Have a nice day.

OK, both better than the dumb "sketch" name for an Arduino program…

 What means the RST_PIN = -1?

I
That just means you aren't using the reset pin. It may be that a physical reset
in on the device needs to be tied high or low .logically, or that there is no such pin.

a7

:joy::rofl: That gave me a good laugh for today.

no, you have configure real address that your OLED used. To determine it, you can use i2c-scanner sketch from IDE examples.

Attached is my "device prove out code" for a 128 x 64 OLED.
If you load it alone, it should work.

I've never had to use the RST_PIN as non of my displays have one. All my displays are 4 pin devices.

// Simple I2C test for ebay 128x64 oled.
// Uses the standard Wire.h class as we think the BME680 will already use wire. was: Use smaller faster AvrI2c class in place of Wire.
// Edit AVRI2C_FASTMODE in SSD1306Ascii.h to change the default I2C frequency.
// 2019-05-17 added Bill Greiman's comment regarding the addition of "Wire.begin()"  now working at 100Khz

#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"

#define I2C_ADDRESS 0x3C	// 0x3C alt add = 0x3D
SSD1306AsciiWire oled;		// create an "oled" object

//------------------------------------------------------------------------------
// Setup displays variable names and units.
//  We still have to identify the location and length for variable values.
//  We should in the future create functions to do the length and position calc.

void setup() {

  Wire.begin();
  Wire.setClock(100000);
  
  oled.begin(&SH1106_128x64, I2C_ADDRESS);
  oled.setFont(ZevvPeep8x16);  // results in 4 lines X 16 characters
  oled.clear();

// Fill display so we can see what is being cleared.
  oled.println("----------------");
  oled.println("----------------");
  oled.println("----------------");
  oled.println("----------------");
  delay(1000);

//							  (C,R)
  oled.SSD1306Ascii::setCursor(0,0);
  oled.print("Temp");				// leaves cursor at next char in row.
  oled.SSD1306Ascii::setCursor(8*14,0);
  oled.print("$F");
  oled.clearField(8*4,0,10);

  oled.SSD1306Ascii::setCursor(0,2);	// note rows are 8 pixels and our font is 16 pixels
  oled.print("Humidity");				// leaves cursor at next char in row.
  oled.SSD1306Ascii::setCursor(8*15,2);
  oled.print("%");
  oled.clearField(8*8,2,7);
  
  oled.SSD1306Ascii::setCursor(0,4);
  oled.print("Pressure");				// leaves cursor at next char in row.
  oled.SSD1306Ascii::setCursor(8*13,4);
  oled.print("hPa");
  oled.clearField(8*8,4,5);
 
  oled.SSD1306Ascii::setCursor(0,6);
  oled.print("IAQ   both");				// leaves cursor at next char in row.
  oled.clearField(8*3,6,13);
} 

void loop() {}


@JohnRob Thanks for the scratch :slight_smile: Display is working. The last sketches I flashed on display I had what https://iotexpert.com/debugging-ssd1306-display-problems/ called a "Speckled Screen". With your sketch the display is ready to go.

Thanks to all supporters.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.