SD Logging Trouble

Hi All

I’m busy with a data logging project and seem to be have a some funny problems :cold_sweat:

Hardware is an Uno with a W5100 Ethernet / SD Shield.

That I’ve been experiecing is the following:

  • Freezing, perticularly when analog input is near low or high limits
  • Freezing, after a short duration - less than a couple minutes
  • Starts showing jumble on LCD and Serial monitor
  • Error saying couldn’t open file
  • Seems to more unstable if I use “:” inplace of the “,” in the String dataString line.

I suspect its the shield that is faulty, but not sure?

Please see attached code file.

#include <SD.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // For I2C LCD
#include <RTClib.h> // For DS1307 RTC 


LiquidCrystal_I2C lcd(0x27,16,2);  // Sets I2C LCD address is 0x27, some differ, for a 16 chars and 2 line display, 
                                   //"lcd" in command is LCD name, use "lcd2" for multile LCDs.  
RTC_DS1307 RTC;


// 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;
  int an3_pin = A3;

void setup()

{
  Serial.begin(9600);
  Wire.begin();
  
  lcd.init();                // initialize the lcd  
  lcd.backlight();
  lcd.print("Hello, world!"); // Print a "startup" message to the LCD.
  delay(2000);
  lcd.clear();

  RTC.begin();                 // Startup RTC

  if (!RTC.isrunning()) {      // Checks if RTC is running
    Serial.println("RTC is NOT Running");    // Prints line to serial monitor for debugging
    RTC.adjust(DateTime(__DATE__,__TIME__)); // Line sets the RTC to the date & time this sketch was compiled
  }                                          // RTC setup subroutine?
  
  Serial.print("Initializing SD card...");
  lcd.print("Init SD Card");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  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.");
  lcd.clear();
  lcd.print ("card initial");
  
  File logFile = SD.open("TEST.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(", , , ,"); //Just a leading blank line, incase there was previous data
    String header = "Hour, Minute, Second, AN3";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
    lcd.print("problem card");   
  }
}



void loop(){

  
  //From JB 
  int Analog_three = analogRead(an3_pin);
  
  DateTime now = RTC.now();

  
  
  
  String dataString = String(now.hour(), DEC) + ": " + String(now.minute(), DEC) + ": " + String(now.second(), DEC) + ", " + String(Analog_three);
   
  //Open a file to write to
  //Only one file can be open at a time
  File logFile = SD.open("TEST.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(dataString);
    logFile.close();
    Serial.println(dataString);
    lcd.clear();
    lcd.setCursor(0, 1);
    lcd.print(dataString);
  }
  else
  {
    Serial.println("Couldn't open log file");
    lcd.setCursor(0, 1);
    lcd.print("problem");
  }
  
  //Increment ID number
  
  delay(1000);
  
}

Datalogger4_with_LCD_RTC.ino (3.16 KB)

The usage of the String class is strongly discouraged as it is a memory hog according to many post on this forum.
Try to rewrite the sketch with char..

Thanks for the Rob

I actually picked up people that where having similar problems on the forum also linked to the String class, subsequently I have rewritten it just printing each value to file, seems to be running fine, will leave it for a while :grin: