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.


  String FileName = SDTransactionDirectory + "/" + RTCSDDate + RTCSDTime + ".txt";
  SD_File = SD.open(FileName, FILE_WRITE);
    digitalWrite(LED_SD, HIGH);

The SD_Initialize() is a user function:-

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


    digitalWrite(LED_SD, HIGH);
    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.

  • 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
    Commented Oct 11, 2018 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
    Commented Oct 11, 2018 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
    Commented Oct 11, 2018 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. Commented Oct 11, 2018 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? Commented Oct 11, 2018 at 12:24

1 Answer 1


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 Commented Oct 15, 2018 at 4:54
  • I have asked to ship the system from site and once i get it will check the serial monitor to see whats going around. Commented Oct 15, 2018 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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