Data Logger didnt work when serial print active, help im stupid

hi im really new at arduino, im making project to data logging voltage and current on my system, im using data logger and rtc module, the issues are here:
when im using the serial monitor to know the state of my system i cant get any reading on my sd card. but when i delete the coe for serial print it works fine, i dont understand where did i go wrong on my code.

int AVS1 = analogRead(A0);// Voltage Sensor // add 0.37 V offset
const int ACS1 = A1; // Current Sensor

#include <SPI.h>
#include <SD.h>

#include <Wire.h>
#define DS3231_I2C_ADDRESS 0x68

const int chipSelect = 10;   // Set the chip select pin for the SD card module
const float VCC = 5.04; // supply voltage Arduino
const int MODEL = 2;   // enter the model for robojax libary

#include <Robojax_AllegroACS_Current_Sensor.h>
Robojax_AllegroACS_Current_Sensor v_cur_sense(MODEL,ACS1);

float v_vol_sense;
float Vol_meas;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  pinMode(v_vol_sense, INPUT);

  if (!SD.begin(chipSelect)) {
    Serial.println("SD card initialization failed!");
    while (1);
  }

  Serial.println("SD card initialized successfully!");

  // Create and write the headers to the .csv file
  File dataFile = SD.open("data_log.csv", FILE_WRITE);
  if (dataFile) {
    dataFile.println("TIME|VOLTAGE|CURRENT|REL_STAT_A1|REL_STAT_A12|REL_STAT_A2|REL_STAT_B1");
    dataFile.close();
  } else {
    Serial.println("Error opening data_log.csv!");
  }
}

void loop() {

 int second, minute, hour, dayOfWeek, dayOfMonth, month, year;
 getRTCDateTime(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

 float Cur_meas = v_cur_sense.getCurrentAverage(300);// new float for current sensing
 float Print_Cur = Cur_meas;
 float Print_Vol = Vol_meas;
 
 voltage_sensor();

   Serial.print(year);
   Serial.print('/');
   Serial.print(month);
   Serial.print('/');
   Serial.print(dayOfMonth);
   Serial.print(' ');
   Serial.print(hour);
   Serial.print(':');
   Serial.print(minute);
   Serial.print(':');
   Serial.print(second);
   Serial.print("| Tegangan : ");
   Serial.print(Print_Vol, 3);
   Serial.print(" Arus : ");
   Serial.println(Print_Cur, 3);
 
 String dataString = String(hour) + ":" + String(minute) + ":" + String(second) + "|" + String(Vol_meas) + "|" + String(Cur_meas) + "\n";
 saveDataToSDCard(dataString);
 
}

void voltage_sensor(){
  v_vol_sense = analogRead(AVS1);
  Vol_meas  = map(v_vol_sense, 0, 1023, 0, 2500);
  Vol_meas  = Vol_meas/100;
   //if (Vol_meas < 9.7) { // noise??
   //Vol_meas = 0;
   //}
}

void setRTCDateTime(int year, int month, int day, int hour, int minute, int second) {
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // Set alamat pointer ke register 0
  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(getDayOfWeek(day, month, year))); // Hari dalam seminggu (0-6, 0=Sunday)
  Wire.write(decToBcd(day));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year - 2000)); // Karena DS3231 hanya mendukung tahun 2000-2099
  Wire.endTransmission();
}

void getRTCDateTime(int& second, int& minute, int& hour, int& dayOfWeek, int& dayOfMonth, int& month, int& year) {
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // Set alamat pointer ke register 0
  Wire.endTransmission();

  Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // Mintalah 7 byte data

  second = bcdToDec(Wire.read() & 0x7F);
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read() & 0x3F);
  dayOfWeek = bcdToDec(Wire.read());
  dayOfMonth = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read()) + 2000;
}

byte decToBcd(byte val) {
  return ((val / 10 * 16) + (val % 10));
}

byte bcdToDec(byte val) {
  return ((val / 16 * 10) + (val % 16));
}

int getDayOfWeek(int day, int month, int year) {
  // Algoritma Zeller's Congruence untuk mendapatkan hari dalam seminggu
  if (month < 3) {
    month += 12;
    year--;
  }
  int k = year % 100;
  int j = year / 100;
  int h = (day + 13 * (month + 1) / 5 + k + k / 4 + j / 4 + 5 * j) % 7;
  return (h + 5) % 7; // Karena Zeller's Congruence menetapkan 0 = Saturday, 1 = Sunday, ..., 6 = Friday
}

String getDayOfWeekName(int dayOfWeek) {
  switch (dayOfWeek) {
    case 6: return "Minggu";
    case 5: return "Monday";
    case 4: return "Tuesday";
    case 3: return "Wednesday";
    case 2: return "Thursday";
    case 1: return "Friday";
    case 0: return "Sabtu";
    default: return "Unknown";
  }
}

void saveDataToSDCard(String data) {
  File dataFile = SD.open("data_log.csv", FILE_WRITE);
  if (dataFile) {
    dataFile.print(data);
    dataFile.close();
  } 
}


these are the message on my ide does the capacity effect the arduino ?

Global variables use 1639 bytes (80%) of dynamic memory, leaving 409 bytes for local variables. The maximum is 2048 bytes.
Low memory available, stability issues may occur.

Yes, in this case particularly, as you use a lot of String manipulation. If you're on an Uno or Nano, as I expect you are, those manipulations will chew through available memory.
See

for more information.
Ultimately, the solution is to allocate a buffer for your string, and write the various contents to that buffer, then send that to screen and SD. Then, next iteration, re-use the buffer. That results in no new memory allocation, and therefore no crashes.

1 Like

oh i can see the problem here, im trying to use other uno board to distribute the memorry issuses, probably will be using 2 uno after this, one is for control and other for data logging. thx for the information

Well, your problem is easily solvable with one Uno, and you will find that doing such a simple task with two Unos introduces more complexity and problems than it has solved. But, since you seem to have made a decision, I wish you luck!

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