Writing to SD card errors.

Hello,
I am still new to this so please deal with me. I am used to PLC logic so this is kinda different…

I made a board that can read three temp probes and four amp probes. I would like to be able to put the data on the onboard SD card on the Ethernet shield. Ideally I would like to put it as an xml file. Not sure if that is totally feasible. I am sure I am missing coding for some of that but the code does work for the temp readings and the amp readings. It does not log anything and gives me an error message for the SD card. Error message states “Initializing SD card…Card failed, or not present” It does open the file and put “343,343344” over and over on the SD card.

Is there a way to put the date and time? I am sure that I will need to include a RTC or some other code in it for this.

I have the Arduino Mega2560 with the arduino ethernet sheild.

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.

DeviceAddress probe1 = { 0x28, 0xCC, 0xA2, 0x97, 0x05, 0x00, 0x00, 0x1E };
DeviceAddress probe2 = { 0x28, 0x8A, 0xF8, 0x97, 0x05, 0x00, 0x00, 0xF1 };
DeviceAddress spare = { 0x28, 0x59, 0xBE, 0xDF, 0x02, 0x00, 0x00, 0x9F };

#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon11, emon12, emon13, emon14;          // Create an instance

#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 = 4;

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(probe1, 10);
  sensors.setResolution(probe2, 10);
  sensors.setResolution(spare, 10);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
{
  emon11.current(1, 111.1);             // Current: input pin, calibration.
  emon12.current(2, 111.1);             // Current: input pin, calibration.
  emon13.current(3, 111.1);             // Current: input pin, calibration.
  emon14.current(4, 111.1);             // Current: input pin, calibration.
}
// SD Card Datalogger ;
{
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:
    return;
  }
  Serial.println("card initialized.");
}}


void loop(void)
{ 
  delay(2000);
  Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();
  
  Serial.print("Probe 1 is: ");
  printTemperature(probe1);
  Serial.print("\n\r");
  Serial.print("probe 2 is: ");
  printTemperature(probe2);
  Serial.print("\n\r");

  Serial.print("\n\r\n\r");

  delay(2000);
  double Irms1 = emon11.calcIrms(1480);  // Calculate Irms only
  double Irms2 = emon12.calcIrms(1480);  // Calculate Irms only
  double Irms3 = emon13.calcIrms(1480);  // Calculate Irms only
  double Irms4 = emon14.calcIrms(1480);  // Calculate Irms only
  
  Serial.print("CT 1: ");
  Serial.print(Irms1*230.0);	       // Apparent power
  Serial.print(" ");
  Serial.println(Irms1);		       // Irms
  
  Serial.print("CT 2: ");
  Serial.print(Irms2*230.0);	       // Apparent power
  Serial.print(" ");
  Serial.println(Irms2);		       // Irms

  Serial.print("CT 3: ");
  Serial.print(Irms3*230.0);	       // Apparent power
  Serial.print(" ");
  Serial.println(Irms3);		       // Irms
  
  Serial.print("CT 4: ");
  Serial.print(Irms4*230.0);	       // Apparent power
  Serial.print(" ");
  Serial.println(Irms4);		       // Irms
  
// SD Card Datalogger ;
{
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 1; analogPin < 4; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ","; 
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}
}

All and any help is greatly appreciated!

I set pin 10 to output instead of 53. See if that works.

// SD Card Datalogger ;
  {
    Serial.print("Initializing SD card...");
    // make sure that the default chip select pin is set to
    // output, even if you don't use it:
 /*
  * Instead of pin 53, try pin 10
  */
   // pinMode(53, OUTPUT);
    pinMode(10, 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:
      return;
    }
    Serial.println("card initialized.");
  }

I don’t think my last reply will work.

However, I had this script laying around and it seems to work fine…

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 
 */

#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 = 4;

void setup()
{
  // 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("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:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ","; 
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}

I used the following hardware.

Arduino Mega: https://www.sparkfun.com/products/11061

Ethernet Shield: https://www.sparkfun.com/products/9026

Try this and if it works, modify it for your use.

In general, I have experienced so many issues with data logging using Arduino (hope I don’t get in trouble for saying that here) I would recommend finding another solution. The data seems to randomly corrupt. The shield over-heats and stops responding.

I do not have any suggestions (that are comparable in price to Arduino) for alternative data logging systems.

If you are using a Mega, you must disable the w5100 SPI by setting the slave select (D10) as OUTPUT and HIGH, or the w5100 may prevent the SD from starting correctly by trashing up the SPI bus.

  Serial.print("Initializing SD card...");
  // make sure that the w5100 chip select pin is set to OUTPUT and HIGH
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);

  // 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:
    return;
  }
  Serial.println("card initialized.");

You should run SD.begin(4) only once, and that should be in the setup() function.

Hello, Well I tried what was suggested.

SurferTim: If you are using a Mega, you must disable the w5100 SPI by setting the slave select (D10) as OUTPUT and HIGH, or the w5100 may prevent the SD from starting correctly by trashing up the SPI bus.

  Serial.print("Initializing SD card...");
  // make sure that the w5100 chip select pin is set to OUTPUT and HIGH
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);

  // 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:     return;   }   Serial.println("card initialized.");




You should run SD.begin(4) only once, and that should be in the setup() function.

Unfortunately it did not work and still get the weird 343,343344 number.

Any other suggestions.

Thanks!