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);
}