Temperature datalogger problems


I am building a temperature datalogger based on 5 type k thermocouples. They are all both amplified and filtered. I am trying to log the data on my sd card using a 2GB Kingston sd. I am using Arduino Mega 2560 R3.
The problem is when i run my code it is only logging 3-7 dataseries. And then i crashes.
(I have no problems with reading data directly from the arduino and writing it to the com port without using my SD card).
Any one know what causes this problem when using my SD card.
This is my code: (note that i am only using 2 of the 5 thermocouples in this example)

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 53;
//Defines Analog inputs
int sensorPin = A0;
int sensorPin2 = A1;

void setup()
 // Open serial communications and wait for port to open:
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(53, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
  Serial.println("card initialized.");

void loop()
  File logFile = SD.open("LOG.csv", FILE_WRITE);
  //Exhaust Temperature
  int Thermo1 = analogRead(sensorPin);
  //Intake Temperature
  int Thermo2 = analogRead(sensorPin2);
  //Create Data string for storing to SD card  
  if (logFile)
    Serial.println("Couldn't open log file");

I hope you can help me :slight_smile:

I'm not sure this helps but I don't see why you have the "if" condition, and it might be confusing things. You have established the initialisation in the setup, so get on with the measuring, open the file, write file, close the file, delay, and go round again. You are more or less doing that now and it is much the same as what I'm doing, I assume it compiles OK, so there can't be much wrong.

About the "if" it was just because i havde a Serialprintln("Couldn't write file") before and i hoped i would help removing it. but i did'nt :S. But could it be problem with the Kingston 2GB card?

Y guess you can really only tell by reformatting or trying another, but I believe the successful initialisation is sufficient to be sure the card is kosher. Kingston is a reputable grand.

Try using the SDfat library.


So i tried to get rid of the "if" statement. But still when i have the logger turned on for about 2 minuttes. I only have 12-15 reading where i was supposed to have 2*60 ?

I will try to use the SDfat later today.

Use Tools + Auto Format to properly indent your code.

Then, explain what the { after the File statement is for. Useless { and } do not improve the readability of the program.