SD stop after reading and writing a few data

Hello everyone,

I'm new to Arduino and I'm using an Arduino UNO to read and write current & volt using max471 and reading temperature using three sensors of DS18B20. My code works to read and write on the SD card, but after recording the data, it stops. I tried modifying the code and changing the SD card and SD card reader, but without results.

I leave my code here to see if you can help me with this.

Thanks!


float A;
float AA;

////////////////////////
const int vPIN = A0;      //Arduino Analog Pin
float vOUT = 0.0;
float vIN = 0.0;
float R1 = 10000;       //Resistor 1 value in ohms (10000 ohms = 10Kohms) 
float R2 = 1000;        //Resistor 2 value in ohms (1000 ohms  =  1Kohms)
int value = 0;

/////////////////////////
const int vPINz = A2;      //Arduino Analog Pin
float vOUTz = 0.0;
float vINz = 0.0;
float R1z = 9860;       //Resistor 1 value in ohms (10000 ohms = 10Kohms) 
float R2z = 990;        //Resistor 2 value in ohms (1000 ohms  =  1Kohms)
int valuez = 0;


///////////////////////////////
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);	
DallasTemperature sensors(&oneWire);
int deviceCount = 0;
float tempC;


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

#include <SD.h> //for the SD card module
#include <SPI.h> //for the SD card module
int chipSelect = 4; //chipSelect pin for the SD card Reader
File mySensorData; //Data object you will write your sesnor data to




void setup () {
Serial.begin (9600);
pinMode (A0, INPUT);
pinMode (A1, INPUT);
pinMode (A2,INPUT);
pinMode (A3,INPUT);
sensors.begin();
Serial.print("Locating devices...");
Serial.print("Found ");
deviceCount = sensors.getDeviceCount();
Serial.print(deviceCount, DEC);
Serial.println(" devices.");
Serial.println("");

///////////////////////////////////
Serial.print("Temp1");
Serial.print("\t");
Serial.print("Temp2");
Serial.print("\t");
Serial.print("Temp3");
Serial.print("\t");
Serial.print("Volt1");
Serial.print("\t");
Serial.print("Curr1");
Serial.print("\t");
Serial.print("Volt2");
Serial.print("\t");
Serial.println("Curr2");
////////////////////////////////////
pinMode(10, OUTPUT); //Must declare 10 an output and reserve it
SD.begin(4); //Initialize the SD card reader 
}

void loop () {
  
mySensorData = SD.open("Data.txt", FILE_WRITE);
if (mySensorData) {


 sensors.requestTemperatures(); 
  
  
for (int i = 0;  i < deviceCount;  i++)
  {
 
tempC = sensors.getTempCByIndex(i);
Serial.print(tempC);
Serial.print("\t");
  }

///////////////////////////
vOUT = (value * 5.0) / 1023.0;
  vIN = vOUT / ( R2 / (R1 + R2) );
   value = analogRead(vPIN);
  Serial.print(vIN*0.24922); 
  Serial.print("\t");

A= analogRead (A1)/183.3; //-------calibrate
Serial.print (A*0.791366,3);
Serial.print ("\t");

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

vOUTz = (valuez * 5.0) / 1023.0;
  vINz = vOUTz / ( R2z / (R1z + R2z) );
   valuez = analogRead(vPINz);
  Serial.print(vINz*0.257); 
  Serial.print("\t");

AA =analogRead (A3)/183.3;
Serial.print (AA*0.761,3);
Serial.println ("");

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


for (int i = 0;  i < deviceCount;  i++)
  {
tempC = sensors.getTempCByIndex(i);
mySensorData.print(tempC);      
mySensorData.print("\t"); 
  }


mySensorData.print(vIN*0.24922);      
mySensorData.print("\t"); 


mySensorData.print(A*0.791366,3);      
mySensorData.print("\t"); 


mySensorData.print(vINz*0.257);      
mySensorData.print("\t"); 

mySensorData.print(AA*0.761,3);      
mySensorData.println(""); 

  mySensorData.close();                                
}
delay(1000);
}

The data file should be opened only once in setup(), and closed when you are finished collecting data.

Opening the file, writing one line of data and closing it again in loop() drastically increases the power draw, the SD card error rate and greatly slows down data collection.

To prevent data loss when the power is cut, issue a mySensorData.flush(); command every 15 minutes, or hour, etc.

I intialized opening the SD card at void setup, and I used mySesnorData.flush(); instead of mySensorData.close();

shall I do extra modifications to the code?

float A;
float AA;

////////////////////////
const int vPIN = A0;      //Arduino Analog Pin
float vOUT = 0.0;
float vIN = 0.0;
float R1 = 10000;       //Resistor 1 value in ohms (10000 ohms = 10Kohms) 
float R2 = 1000;        //Resistor 2 value in ohms (1000 ohms  =  1Kohms)
int value = 0;

/////////////////////////
const int vPINz = A2;      //Arduino Analog Pin
float vOUTz = 0.0;
float vINz = 0.0;
float R1z = 9860;       //Resistor 1 value in ohms (10000 ohms = 10Kohms) 
float R2z = 990;        //Resistor 2 value in ohms (1000 ohms  =  1Kohms)
int valuez = 0;


///////////////////////////////
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);	
DallasTemperature sensors(&oneWire);
int deviceCount = 0;
float tempC;


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

#include <SD.h> //for the SD card module
#include <SPI.h> //for the SD card module
int chipSelect = 4; //chipSelect pin for the SD card Reader
File mySensorData; //Data object you will write your sesnor data to




void setup () {
Serial.begin (9600);
pinMode (A0, INPUT);
pinMode (A1, INPUT);
pinMode (A2,INPUT);
pinMode (A3,INPUT);
sensors.begin();
Serial.print("Locating devices...");
Serial.print("Found ");
deviceCount = sensors.getDeviceCount();
Serial.print(deviceCount, DEC);
Serial.println(" devices.");
Serial.println("");

pinMode(10, OUTPUT); //Must declare 10 an output and reserve it
SD.begin(4); //Initialize the SD card reader 


if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    Serial.println("");
    // don't do anything more:
    while (1) ;
  }
  Serial.println("card initialized.");
  Serial.println("");
  // Open up the file we're going to log to!
  mySensorData = SD.open("DATA.txt", FILE_WRITE);
  if (! mySensorData) {
    Serial.println("error opening DATA.txt");
    // Wait forever since we cant write data
    while (1) ;
  }


///////////////////////////////////
Serial.print("Temp1");
Serial.print("\t");
Serial.print("Temp2");
Serial.print("\t");
Serial.print("Temp3");
Serial.print("\t");
Serial.print("Volt1");
Serial.print("\t");
Serial.print("Curr1");
Serial.print("\t");
Serial.print("Volt2");
Serial.print("\t");
Serial.println("Curr2");
}
///////////////////////////



void loop () {


 sensors.requestTemperatures(); 
  
  
for (int i = 0;  i < deviceCount;  i++)
  {
 
tempC = sensors.getTempCByIndex(i);
Serial.print(tempC);
Serial.print("\t");
  }

///////////////////////////
vOUT = (value * 5.0) / 1023.0;
  vIN = vOUT / ( R2 / (R1 + R2) );
   value = analogRead(vPIN);
  Serial.print(vIN*0.24922); 
  Serial.print("\t");

A= analogRead (A1)/183.3; //-------calibrate
Serial.print (A*0.791366,3);
Serial.print ("\t");

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

vOUTz = (valuez * 5.0) / 1023.0;
  vINz = vOUTz / ( R2z / (R1z + R2z) );
   valuez = analogRead(vPINz);
  Serial.print(vINz*0.257); 
  Serial.print("\t");

AA =analogRead (A3)/183.3;
Serial.print (AA*0.761,3);
Serial.println ("");
////////////////////

for (int i = 0;  i < deviceCount;  i++)
  {
tempC = sensors.getTempCByIndex(i);
mySensorData.print(tempC);      
mySensorData.print("\t"); 
  }


mySensorData.print(vIN*0.24922);      
mySensorData.print("\t"); 


mySensorData.print(A*0.791366,3);      
mySensorData.print("\t"); 


mySensorData.print(vINz*0.257);      
mySensorData.print("\t"); 

mySensorData.print(AA*0.761,3);      
mySensorData.println(""); 

 mySensorData.flush();                             

delay(1000);
}

This should be done every so often, not every pass through loop. Choose an interval that makes sense to you.

If you don't close the file, you will lose only the data written since the last .flush() command.

Most people use a button or some other means to start and stop data collection.

The problem repeated again even with the modifocation of the code

Can you explain how to use a circuit with push button to start and close SD card manually I want to know the code that activates the push buttons for start and stop the SD card :innocent:

Thanks in advance

Then there is something wrong with the wiring, the Arduino or the power supply. The Arduino Uno is 5V and the SD card is 3.3V, so logic level shifters are required for the connections between them. If connected without them, the setup might work for a little bit, but one or both devices will eventually be destroyed.

Post a pic of a hand drawn wiring diagram, with pins and connections clearly labeled, and links to the modules.

I checked the SD card and it's written that it operates with 5V not 3.3 V, and I checked my connections but I think the problem is because of the buffer

Post a picture of it.
Some have onboard level-shifting.

Some manufacturers exaggerate their claims. Many SD modules have an onboard 5V to 3.3V regulator, so they "operate with 5V", but have no level shifters. Post a link to the product page for the module.



I think that's a good one. And it has onboard level shifting, too, so that can be checked off the list.

I think if there is a way to reset the buffer after each reading in the void loop this will avoid overflow

Is setup repeating?

Why do you think its an issue with the buffer? When the buffer is full, the library will physically write to the card.

What memory usage do you see for the sketch after compiling?

I have deleted your other cross-post @valeria711.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

In the IDE, A by itself, turns a color - like it's a keyword.
There's a variable 'A', though. Is that consequential (beyond bad form, a single-letter variable)?

There are several instances of writing to the SD the result of an operation, in parentheses, i.e. --
mySensorData.print(AA*0.761,3);

cf.
idx = AA * 0.761;
mySensorData.print(idx,3);

I will change it and see if thing go better

Each time the sd card stops writing when the file size is 80 KB

OK, sorry about that.
I've saved data to microSD cards and not run into any trouble such as this (but we have different approaches and practices).

In my code pin 10 is set as output and pin 4 is for chipselect shall I change any of above