SD card device stops writing after 5Kb of data

I'm trying to do a weather station project that logs the soil humidity, pressure, and temperature in a SD card, it uses a Arduino nano (ATmega328P) using the old bootloader config, the SD card is this one:


formatted FAT32

the data written in the SD looks like this:
ET:(Millis since start),T:(temperature), M:(a relative moisture%),P:(relative Pressure%)
the problem is that passed some write cycles the SD card disconnects, and is not able to connect back, at least until i pop the SD into my pc and erase the txt file, the max amount of data it stores before the disconnect happens is about 5Kb and 8kb of data, or 130 and 190 lines of text in the txt file, it is very random and that is only the bottomline. I suspect some library shenanigans,
here is the horrible code i wrote:


#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <SD.h>

#define sensorPin A1
#define ONE_WIRE_BUS 2
const int chipSelect = 4;
const int PIn =7;
const int POut =5;

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x3F, 16, 2);

void setup() 
{
  pinMode(PIn, INPUT);
  pinMode(POut, OUTPUT);

  Serial.begin(9600);

  sensors.begin();

  lcd.init();
  lcd.clear();
  lcd.backlight();

  Serial.print("Initializing SD card");
  if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed to be detected");
    while (!SD.begin(chipSelect));
  }
  
  Serial.println("Card initialized");
  File dataFile = SD.open("data.txt",FILE_WRITE);

  if (dataFile) 
  {
    dataFile.println("**Data session separator**");
    dataFile.close();
  }
  else {Serial.println("Error opening data.txt");}

  lcd.setCursor(0, 0);
  lcd.print("Weather<><><>");
  lcd.setCursor(0, 1);
  lcd.print("<>Station<><>");

  delay(1000);
  
}

int ReadDelay = 20;
int LoopDelay = 100;


unsigned long StartTime = millis();

void loop() 
{
  lcd.clear();
  lcd.noBacklight();

  float Temp =0;
  for(int i=1;i<7;i++)
  {
    sensors.requestTemperatures();
    Temp =(Temp*(i-1)+sensors.getTempCByIndex(0))/i;
    delay(10); 
  }

  float Moist =0;
  for(int i=1;i<7;i++)
  {
  Moist =(Moist*(i-1)+analogRead(A0))/i;
  delay(10);
  }
  Moist =map(Moist,500,250,0,100);

  float P =0;
  for(int i=1;i<7;i++)
  {
    while (digitalRead(PIn));
    long result =0;
    for (int i=0;i<24;i++) 
    {
      digitalWrite(POut,HIGH);
      digitalWrite(POut,LOW);
      result =result<<1;
      if (digitalRead(PIn)) {result++;}
    }
    result =result^0x800000;
    for (char i=0;i<3;i++) 
    {
      digitalWrite(POut,HIGH);
      digitalWrite(POut,LOW);
    }
    P =(P*(i-1)+result)/i;
    delay(10);
  }
  P =map(P,7755716,12582911,0,100);

  lcd.backlight();
  Serial.print("0:");
  Serial.print(0);
  Serial.print(",");
  Serial.print("100:");
  Serial.print(100);
  Serial.print(",");
  Serial.print("25:");
  Serial.println(25);


  unsigned long ElapsedTime =millis();-StartTime;

  String dataString ="";
  dataString +="ET:";
  dataString +=String(ElapsedTime);
  dataString +=",";
  dataString +="T:";
  dataString +=String(Temp);
  dataString +=",";
  dataString +="M:";
  dataString +=String(Moist);
  dataString +=",";
  dataString +="P:";
  dataString +=String(P);
  dataString +=",";


  File dataFile =SD.open("data.txt",FILE_WRITE);

  if (dataFile) 
  {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }
  else 
  {
    Serial.println("error opening data.txt");
    lcd.setCursor(1,0);
    lcd.print("sd error");
    SD.end();
    while(!SD.begin(chipSelect));
  }

  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("temperature C");
  lcd.setCursor(2,1);
  lcd.print(Temp);

  delay(ReadDelay);

  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("Moisture R%");
  lcd.setCursor(2,1);
  lcd.print(Moist);

  delay(ReadDelay);

  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("Pressure %");
  lcd.setCursor(2,1);
  lcd.print(P);

  delay(ReadDelay);

  lcd.clear();
  lcd.noBacklight();

  delay(LoopDelay);
}

kinda new to it all so not as neat as it could be, i think most of the code describes itself, please help me solve this since it is for a school project and it needs to work for like 10 days straight of datalogging. thanks in advance

The question I have is whether the SD card is shutting down, or the Nano is running out of ram and crashing. I see repeated warnings here not to use String functions at all with Arduinos. Can you change your code to simply save fake data without using String functions, and see if it still shuts down?

Check your SD card reader have a datasheet for power use. Add that to the Dallas Temperature/humidity/pressure sensor. Do you find the total greater than 100mA?

I had a similar problem, data becoming corrupted with another board using a MicroSD card. Turned out that the voltage regulator has a thermal protection which cuts off at random times depending on use. You need to power the MicroSD card separately. The Arduino Nano has LM1117IMPX-5.0 voltage regulator, which features "Current Limiting and Thermal Protection", and maximum 800 mA. My board had 500 mA max for the 3.3V regulator. Good luck!

I dont think is likely the arduino crashing, since even restarting it wont allow me to write onto the same sd card, it just straight up does not reconize the sd card, not even on the card info example sketch in some ocations, also the arduino continues to work after the card disconect, as if i clear the sd and plug it back in, even while the program is runing after a sd card error, it just gets back into normal functioning

Everything meinus the sd card module sums up to about 10mA, yet the sd card module can consum up to 200mA, i am using 4 AA batteries in series as a power source btw

For the use i am giving my board 800mA would be unlikely, even 500mA,since everything is either a sensor or the sd card module, and everything combined acording to thier respective datasheet would only sum up a current of about 210mA at the very most, and the sd card is not being used often enough for termal concerns.

Thanks for the help

Make that ~30mA for a classic Nano with ATMega 328, because the 3.3volt supply of that board is 'stolen' from the USB chip. Definitely not enough to power an SD card.

There are two main versions of SD card modules. One with and one without built-in 3.3volt regulator and logic level shifter. For a classic Nano v3 you need the version with onboard regulator and level shifter, powered from the 5volt pin. Example here (click).
Leo..

Not random. When thermal limit has been reached.

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