I am an electronics graduate and an embedded design engineer, working in this field for about two years apart from the hobby time during my graduation. I am currently working on a system that has an Atmega2560 as the core controller with an arduino bootloader, we are using an 8GB memory card to save the log data from our system and send it to our server at regular intervals of time. The system worked fine however some of them are facing issues with the writing of data, some garbage value are getting stored in the SD card. I have attached the schematic and code snippet for the same, please let me know if there is any problem with the system and how can i resolve it, please let me know if any other things are required for understanding the problem correctly.

Thank you in advance.

     SD_Initialize();
  if(!SD.exists(SDTransactionDirectory)) 
  {
    if(SD.mkdir(SDTransactionDirectory)) 
    {

    }
  }
  RTC_Date_Time();
  String FileName = SDTransactionDirectory + "/" + RTCSDDate + RTCSDTime + ".txt";
  SD_File = SD.open(FileName, FILE_WRITE);
  if(SD_File)
  {
    delay(100);
    digitalWrite(LED_SD, HIGH);
    SD_File.println(GSMData);
    SD_File.close();
  }

The SD_Initialize() is a user function:-

void SD_Initialize()          //initialise SD card
{
  digitalWrite(SD_EN, HIGH);
  digitalWrite(SD_CS, LOW);

  delay(500);

  if(SD.begin(SD_CS))
  {
    digitalWrite(LED_SD, HIGH);
  }
  else
  {
    digitalWrite(LED_SD, LOW);
  }
}

enter image description here

The data stored in the filename is as follows:- CLEnT|T|M008|\00Ÿûmï°]ŸëÜ£·zoòTÏæ{Œü[ŸýUô~ÛcCWY®ÞgÙë×<;wBÄgnìÇFÿÉ=XoÎ.êžÄf·®ö¯³ç~åeÝ]1_ÏÀyÿÌÍ?ô"zBõ¬ñ

As you can see the initial values till "M008" is fine but its garbage after that, and the CLEnt|T|M008 is the start of the string after that a lot of other parameter information is to be stored which turns out to be garbage.

New contributor
shwetank vishnu is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Are the filenames garbage as well or only the content of the files? Is the garbage only on certain sd cards of a certain brand or type? What does the garbadge look like? – Jot Oct 11 at 8:02
  • Is GSMData a String? You should avoid String and all String-based concatenation operations. Even on a Mega2560 with that little extra bit of SRAM it's still "iffy". – Majenko Oct 11 at 8:28
  • 1
    You said: "Its not dependent on brand and type since we are using the SD card for all the systems.". Did you mean to say you are using "different SD cards for each system"? Also, check your power supply current capabilities and dependability. Consider the possibility of voltage fluctuations causing corruption in the processor, communications between the processor and the SDCard and in the SDCard. – st2000 Oct 11 at 12:01
  • 2
    The example data starts CLEnT|T|M008|\0; is the \0 an actual sequence of the two characters "\" and "0"? It may be coincidence, but it looks suspicious to see the escaped null char just before the "corrupt" data. Makes one think String termination problems. – jose can u c Oct 11 at 12:22
  • 1
    Can you confirm exactly what should be saved? Should the M008| be the last value saved? In other words, what do you expect to see? – jose can u c Oct 11 at 12:24

There are many comments, but no answer yet. This answer has only a few basic rules.

The first thing to do is to see if the problem can be replicated with a small sketch and to check the voltages.

The arduino mega 2560 works best at 5.0V, or maybe a little less. It is not reliable at 4V. When powering it with a 5V power supply, the usb connecter is the best option to power the arduino board. Powering the arduino board via the 5v pin is not advised. When a strong power supply is connected to the 5v pin, the voltage regulator can be blown.

When something is not working, fall back to something that is reliable. For example a SD module with a HC4050 to shift the voltage levels. The resistors make the signal weaker.

Make a small test sketch without the use of the String object. The more experienced someone is with arduino, the more he or she tells to avoid the String object with avr microcontrollers.

There are counterfeit SD cards. One of those might turn up in your hands someday. Beside that, not every SD card is compatible with the arduino SD library.

In your situation it is suspicious that the file system is okay, the filenames are okay, the first text is okay, but when data is written it suddenly goes wrong. That is a strong indication for a memory problem.

When data to a display or SD card is wrong, then send it also to the serial monitor. You have to know what is actually written to the SD card.

  • hi @jot thank you for concluding the comments and pointing out the possible issues, 1. The power supply is well regulated and filtered using a DC-DC buck convertor which is getting an input from an 18-36Volt SMPS with upto 5A current rating. And I am using the Atmega2560 chip with an arduino bootloader, not the arduino board itself. 2. I am looking forward for making change in the hardware to use the HC4050 to check the response 3. Checked the sample sketch and worked fine like most the systems. Note this problem is with few systems not alll 4. I formatted all the cards with a FAT b4 using – shwetank vishnu 2 days ago
  • I have asked to ship the system from site and once i get it will check the serial monitor to see whats going around. – shwetank vishnu 2 days ago

Your Answer

shwetank vishnu is a new contributor. Be nice, and check out our Code of Conduct.
 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.