SD card help - duplicate data

Basic SD card issue. I'm writing data to the microSD card, but when I view the card on my pc, it shows the data has been written twice.

Hardware:

  • UNO
  • Arduino Wireless SD Shield

Any ideas why and how to resolve?

/*
  SD card test

  This example shows how use the utility libraries on which the'
  SD library is based in order to get info about your SD card.
  Very useful for testing a card when you're not sure whether its working or not.

  The circuit:
    SD card attached to SPI bus as follows:
 ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
 ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
 ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
 ** CS - depends on your SD card shield or module.
 		Pin 4 used here for consistency with other Arduino examples
*/


/***************************************************************************************


- SD card Library:  https://www.arduino.cc/reference/en/libraries/sd/
   --> includes links to Datalogging examlpes
- communication between the microcontroller and the SD card uses SPI (pins 11,12,13)


microSD card and using Arduino Pins 


*Adafruit, test (micro)SD card
https://learn.adafruit.com/adafruit-data-logger-shield/using-the-sd-card


*Arduino Wireless SD Shield
https://docs.arduino.cc/retired/shields/arduino-wireless-sd-shield
https://docs.arduino.cc/retired/getting-started-guides/ArduinoWirelessShield
https://learn.adafruit.com/adafruit-data-logger-shield/using-the-sd-card

*Library:  SD Library

- Pin 4: is CS, cannot be used otherwise
- Pins 11,12,13: used for SPI (11,12), and 13 for COMMS





***************************************************************************************/

// include the SD library:
#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = 4;

// STRING
  /*  Make a string to log data  */
String dataString = "Data entered to microSD card:";
String this_String = "{1,3,5,7,9}";

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


  Serial.print("\nInitializing SD card...");

  // 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)
{
  Serial.println();
  Serial.println("Hello World!");


// OPEN FILE
/* microSD card 
   - only one file can be open at a time, thus must close current file before opening another.
      >> ignore if you only have 1 card
*/

// CREATE FILE
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
                       //(file name, function)
                         
// WRITE TO FILE
  // if the file is available, write to it:
  if (dataFile) 
  {
   dataFile.println(dataString); //print to microSD card
   dataFile.print("array 1 = "); 
   dataFile.println(2);

   dataFile.print("array 2 = ");
   dataFile.println(this_String);


// CLOSE FILE
    dataFile.close();
    
  }
  
  // if the file isn't open, pop up an error:
  else 
  {
    Serial.println("error opening datalog.txt");
  }

    //SD.remove("datalog.txt"); // delete the file if existed

  //Serial.flush();
  exit(0);
}

I assume that you mean "the file contains two sets of the output lines".

If you reset the Arduino (press the reset button) once does the data show up 3 times? If so, the problem is likely that the sketch runs for about 180 milliseconds before Serial Monitor connects and resets the board. If the file gets written in that 180 milliseconds, the data after the reset will be appended to the file.

I always put 'delay(200);' right after 'Serial.begin()' to avoid the double-reset issues.

Here's what I observed using different test cases when file compiled:

  1. not opening Serial monitor = 1 line written to card.
  2. Opening Serial monitor = 2 lines written to card (initially observed issue)
  3. Opening Serial monitor, then resetting board = 3 lines written to card.

Adding delay(200) after Serial.begin() yielded no difference for any of the cases.

I did notice that the serial monitor was inconsistent with printing Serial.println("Hello World!"); being written to it when I opened it.

These links, one and two, discuss that the Serial Monitor auto-resets and addresses methods to resolve, but aren't' currently within scope of work I'm doing as they're addressing extra hardware for the most part, not simple code.

So it appears that if I don't open the serial monitor, then the program only writes 1 time to the card, which is what I want. It doesn't explain why I don't always see the printout Hello World! when I open the serial monitor, however, but program does work.

Interesting update. When I position:

SD.remove("datalog.txt"); // delete the file if existed

prior to the line

File dataFile = SD.open(file1, FILE_WRITE);

I only get 1 instance of data on the card. It even works when I open the serial monitor, and it also resolves the Hello World! printing issue.

By deleting the file each time, and then recreating it, all issues are apparently resolved when the code is structured like this.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.