TroubleshootingArduino

Arduino compiling error (not enough memory)

userHead Pavlos98cy 2023-04-10 16:26:21 163 Views1 Replies

I bought the KnowFlow Basic Kit ffrom DFRobot plus temperature and turbidity sensors for my thesis "Automatic drinking water monitoring device". Also, I inserted 2 solenoid valves to control the volume of the water in a metal box (the place where I will take measurements with sensors).  I think that the code is ready (I checked each part separate) but the problem is that this KIT included an Arduino UNO and I don't have enough memory for the process.

 

Here are the error I see.

''Global variables use 2212 bytes (108%) of dynamic memory, leaving -164 bytes for local variables. Maximum is 2048 bytes.''

"Compilation error: data section exceeds available space in board."

 

My code:

#include <OneWire.h>

#include "DFRobot_EC.h"

#include <DFRobot_PH.h>

#include <EEPROM.h>

#include <Arduino.h>

#include <Wire.h>

#include <SD.h>

#include "GravityRtc.h"


 

#define chipSelect 7

#define EC_PIN A1  // EC sensor i/o

#define PH_PIN A1  // PH sensor i/o



 

// Temperature sensor i/o

int DS18S20_Pin = 2;          // DS18S20 Signal pin on digital 2

const int solenoidPinIn = 3;  // set pin number for solenoid valve 2

const int solenoidPinOut = 4; // set pin number for solenoid valve 1


 

////////////////////////////////////////////////////////////////////////////

// Globals


 

float voltage, phValue, ecValue, temperature;


 

OneWire ds(DS18S20_Pin);  // on digital pin 2

DFRobot_EC ec;

DFRobot_PH ph;


 

GravityRtc rtc;     //RTC Initialization

// set up variables using the SD utility library functions:

Sd2Card card;

SdVolume volume;

SdFile root;

//const int chipSelect = 7;



 

void setup(void) {

  Serial.begin(9600);  // Baud rate: 9600

  ec.begin();

  ph.begin();

  // set pinMode

  pinMode(solenoidPinIn, OUTPUT);  // set the first valve pin as an output

  pinMode(solenoidPinOut, OUTPUT); // set the second valve pin as an output

{{

  rtc.setup();


 

//Set the RTC time automatically: Calibrate RTC time by your computer time

  rtc.adjustRtc(F(__DATE__), F(__TIME__));


 

  //Set the RTC time manually

  //rtc.adjustRt(2017,6,19,1,12,7,0);  //Set time: 2017/6/19, Monday, 12:07:00

}

 // Open serial communications and wait for port to open:

  Serial.begin(9600);

   while (!Serial) {

    ; // wait for serial port to connect. Needed for Leonardo only

  }



 

  Serial.print("\nInitializing SD card...");

  // On the Ethernet Shield, CS is pin 7. It's set as an output by default.

  // Note that even if it's not used as the CS pin, the hardware SS pin

  // (10 on most Arduino boards, 53 on the Mega) must be left as an output

  // or the SD library functions will not work.

  pinMode(10, OUTPUT);     // change this to 53 on a mega



 

  // we'll use the initialization code from the utility libraries

  // since we're just testing if the card is working!

  if (!card.init(SPI_HALF_SPEED, chipSelect)) {

    Serial.println("initialization failed. Things to check:");

    Serial.println("* is a card is inserted?");

    Serial.println("* Is your wiring correct?");

    Serial.println("* did you change the chipSelect pin to match your shield or module?");

    return;

  } else {

   Serial.println("Wiring is correct and a card is present.");

  }


 

  // print the type of card

  Serial.print("\nCard type: ");

  switch(card.type()) {

    case SD_CARD_TYPE_SD1:

      Serial.println("SD1");

      break;

    case SD_CARD_TYPE_SD2:

      Serial.println("SD2");

      break;

    case SD_CARD_TYPE_SDHC:

      Serial.println("SDHC");

      break;

    default:

      Serial.println("Unknown");

  }


 

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32

  if (!volume.init(card)) {

    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");

    return;

  }



 

  // print the type and size of the first FAT-type volume

  uint32_t volumesize;

  Serial.print("\nVolume type is FAT");

  Serial.println(volume.fatType(), DEC);

  Serial.println();


 

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks

  volumesize *= volume.clusterCount();       // we'll have a lot of clusters

  volumesize *= 512;                            // SD card blocks are always 512 bytes

  Serial.print("Volume size (bytes): ");

  Serial.println(volumesize);

  Serial.print("Volume size (Kbytes): ");

  volumesize /= 1024;

  Serial.println(volumesize);

  Serial.print("Volume size (Mbytes): ");

  volumesize /= 1024;

  Serial.println(volumesize);



 

  Serial.println("\nFiles found on the card (name, date and size in bytes): ");

  root.openRoot(volume);


 

  // list all files in the card with date and size

  root.ls(LS_R | LS_DATE | LS_SIZE);

  }

}


 

void loop(void) {


 

{ // RTC loop

rtc.read();

//*************************Time********************************

  Serial.print("   Year = ");//year

  Serial.print(rtc.year);

  Serial.print("   Month = ");//month

  Serial.print(rtc.month);

  Serial.print("   Day = ");//day

  Serial.print(rtc.day);

  Serial.print("   Week = ");//week

  Serial.print(rtc.week);

  Serial.print("   Hour = ");//hour

  Serial.print(rtc.hour);

  Serial.print("   Minute = ");//minute

  Serial.print(rtc.minute);

  Serial.print("   Second = ");//second

  Serial.println(rtc.second);

  delay(1000);

}


 

  //turbidity

  int sensorValue = analogRead(A0);              // read the input on analog pin 0:

  float voltage = sensorValue * (5.0 / 1024.0);  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):


 

  float NTU = -1120.4 * pow(voltage, 2) + 5742.3 * voltage - 4353.8;

  Serial.print("TURBIDITY: ");

  Serial.println(NTU);  // print out the value you read:


 

  // Read temperature

  float temp = getTemp();

  Serial.print("Temperature: ");

  Serial.print(temp, 2);

  Serial.println(" °C");


 

  // Read EC

  voltage = analogRead(EC_PIN) / 1024.0 * 5000;  // read the voltage

  ecValue = ec.readEC(voltage, temp);            // convert voltage to EC with temperature compensation

  Serial.print("EC: ");

  Serial.print(ecValue, 2);

  Serial.println(" ms/cm");


 

  // EC calibration process by Serial CMD

  ec.calibration(voltage, temp);



 

  {

    static unsigned long timepoint = millis();

    if (millis() - timepoint > 1000U) {  //time interval: 1s

      timepoint = millis();

      voltage = analogRead(PH_PIN) / 1024.0 * 5000;  // read the voltage

      phValue = ph.readPH(voltage, temperature);     // convert voltage to pH with temperature compensation

      Serial.print("pH:");

      Serial.println(phValue, 2);

      Serial.print("\n");

    }

    ph.calibration(voltage, temperature);  // calibration process by Serail CMD

  }


 

  // pH sensor i/o

  const int analogInPin = A3;

  int sensorValuePH = analogRead(analogInPin);

  float voltagePH = sensorValuePH * (5.0 / 1024.0);

  float pHValue = 7 - (voltagePH - 2.5) / 0.18;  // convert voltage to pH


 

  delay(2000);  // Wait for 2 second

}


 

float getTemp() {

  // Returns the temperature from one DS18S20 in DEG Celsius


 

  byte data[12];

  byte addr[8];


 

  if (!ds.search(addr)) {

    // No more sensors on chain, reset search

    ds.reset_search();

    return -1000;

  }


 

  if (OneWire::crc8(addr, 7) != addr[7]) {

    Serial.println("CRC is not valid!");

    return -1000;

  }

  if (addr[0] != 0x10 && addr[0] != 0x28) {

    Serial.print("Device is not recognized");

    return -1000;

  }


 

  ds.reset();

  ds.select(addr);

  ds.write(0x44, 1);  // start conversion, with parasite power on at the end


 

  byte present = ds.reset();

  ds.select(addr);

  ds.write(0xBE);  // Read Scratchpad


 

  for (int i = 0; i < 9; i++) {  // we need 9 bytes

    data[i] = ds.read();

  }


 

  ds.reset_search();


 

  byte MSB = data[1];

  byte LSB = data[0];


 

  float tempRead = ((MSB << 8) | LSB);  // using two's complement

  float TemperatureSum = tempRead / 16;


 

  return TemperatureSum;


 

  // open solenoid valves

  digitalWrite(solenoidPinOut, HIGH);

  digitalWrite(solenoidPinIn, HIGH);

  delay(1000);

  // close solenoid 1 in 10 seconds

  digitalWrite(solenoidPinOut, LOW);

  delay(10000);

  // close solenoid 2 in 20 seconds (gemisma doxiou)

  digitalWrite(solenoidPinIn, LOW);

  delay(20000);

  // wait 4 minutes

  delay(240000);

  // open solenoid valves

  digitalWrite(solenoidPinOut, HIGH);

  digitalWrite(solenoidPinIn, HIGH);

  delay(1000);

  // close solenoid 1 in 10 seconds

  digitalWrite(solenoidPinOut, LOW);

  delay(10000);

  // close solenoid 2 in 5 seconds

  digitalWrite(solenoidPinIn, LOW);

  delay(5000);

  // wait 8 hours

  delay(2880000);

}

 

I checked OUTPUT for each part separately and the memory is almost full just by RTC, microSD module, pH and EC sensors. So generally memory is full even from the main KIT parts. What should I do? Shouldn't the company had to provide a stronger Arduino board for this kind of projects, like Arduino Mega? please give me some advices  of some tips to decrease the memory which I spend in this program. I inlcude my code. Thank you in advance for the help!

2023-04-11 14:10:25

Pls check this forum: https://forum.arduino.cc/t/data-section-exceeds-available-space-in-board/873305/5

You can also consider replacing the controller with larger memory, such as mega2560:https://www.dfrobot.com/product-655.html

userHeadPic jenna