1

I connected the SD card to Arduino as it's explained here. The problem is that the Arduino writes only once to the SD card and I don't understand the reason.

#include <SD.h>
#include <SPI.h>
int CS_PIN = 4;
File file;
void setup() {
 Serial.begin(9600);
 pinMode(10, OUTPUT); 
 digitalWrite(10, HIGH);
 Serial.print("Initializing SD card...");
 if (!SD.begin(CS_PIN)) {
 Serial.println("Card failed, or not present");
 return;
 }
 Serial.println("card initialized.");
}
void loop() {
 String dataString = "TEST";
 File dataFile = SD.open("datalog.txt");
 if (dataFile) {
 dataFile.println(dataString);
 dataFile.close();
 Serial.println(dataString);
 } else {
 Serial.println("error opening datalog.txt");
 }
 delay(1000);
}

The output on Serial Monitor:

Arduino write only one time on SD card error opening datalog.txt error opening datalog.txt error opening datalog.txt error opening datalog.txt error opening datalog.txt error opening datalog.txt error opening datalog.txt error opening datalog.txt error opening datalog.txt error opening datalog.txt error opening datalog.txt error opening datalog.txt error opening datalog.txt

I also tried to use:

SD.open("file.txt", O_RDWR | O_APPEND);
SD.open("file.txt", O_RDWR | O_CREAT);

As I saw Adding data to an SD card file, but it doesn't work.

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Aug 12, 2017 at 21:27
2
  • don't you need a mode when opening? Commented Aug 13, 2017 at 11:17
  • open the file in setup. then write data in loop. after all data has been written, close the file Commented Nov 30, 2017 at 8:02

3 Answers 3

2

You are opening the file in read mode but should be opening it in write mode. Change File dataFile = SD.open("datalog.txt"); to File dataFile = SD.open("datalog.txt", FILE_WRITE);.

You can remove this line of code: digitalWrite(10, HIGH);.

The arduino.cc website is also an excellent source of information regarding the SD library.

dda
1,5951 gold badge12 silver badges17 bronze badges
answered Aug 31, 2017 at 18:45
1

File dataFile = SD.open("datalog.txt");

Repeatedly opening the same file is probably not a good practice, if you want to retain previously written contents.

dda
1,5951 gold badge12 silver badges17 bronze badges
answered Aug 31, 2017 at 20:49
-1

For people having the same problem. I had the same problem where the SD Card would only run the if statement once and then not read it anymore. For some reason, after I ran the CardInfo, DumpFile, and ReadWrite example scripts in the SD Library (changing the title of the SD.open() command to the one that I was using), my code began to work and it started running the if statement each time. I would recommend trying this if you're experiencing the same problem.

answered Mar 13, 2022 at 21:20
2
  • Interestingly enough, I was having a problem opening a file on my SD Card using the SD Card Reader Sensor and my Elegoo Mega2560 and running the CardInfo example ( on the SD Library) solved it for me. The first time the CardInfo example gave an error, and then (just saying what I did) I popped my SD Card and put it back in and then ran the CardInfo example again and then it worked. So... If you guys are having any problems. Try using the CardInfo example from the SD Library and re-insert your SD Card. Commented Mar 13, 2022 at 21:37
  • this indicates problems with the CS pin Commented Mar 14, 2022 at 5:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.