*I am not a native English speaker. If you are confused about my expression, please let me know in the comments. Thx!
Today I'm trying to write some data to my sd card, but there's something wrong.
I'm using the example code named ReadWrite which provided by Arduino IDE,contains two write operations to the SD card
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("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// The first time,no error,the file was created but no content was written to it
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// The second,here comes the error
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
I have confirmed my wiring is correct, I have really no idea how to fix this right now.
Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.
Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.
Repeated duplicate posting could result in a temporary or permanent ban from the forum.
Could you take a few moments to Learn How To Use The Forum. It will help you get the best out of the forum in the future.
➜ do you want to close the Chinese topic or this one ?
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("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// 第一次写入,没有报错,但文件只是被创建但没有被写入“testing 1,2,3”
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// 第二次写入,报错
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
Serial Monitor:
Initializing SD card...initialization done.
Writing to test.txt...done.
error opening test.txt
Circuit:
An SD module with SPI interface.
My SD module has eight pins, namely GND, MISO, SCK, MOSI, CS, 5V, 3x3, and GND (arranged in order of pins). Among them, I did not wire the 3x3 pin and the GND next to it according to the online tutorial (it is said that there is an AMS117-3.3 module on my SD card, and if it is powered by 3x3, it will make it unstable).
Here is my wiring:
(SD Module) -- (UNO R3)
GND -- GND
5V -- 5V
CS -- 4
MOSI -- 11
SCK -- 12
MISO -- 13
BTW, My SD card should be fine. I have formatted it multiple times as FAT and read and write it in the computer's card reader (with the write protector turned off). Ican get the information about the card by using the example named CardInfo.
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
//#include <SD.h>
#include "SdFat.h"
SdFat SD;
#define SD_CS_PIN SS
File myFile;
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.println(SS);
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS_PIN)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
Here is the code provided by sdfat library and I've tested this code. But I still get the error:
"Initializing SD card...initialization done.
Writing to test.txt...done.
error opening test.txt
"
Before I saw your comment I have tried many times using sd library to fix this problem.
For many times, it returned error when it opened the file for the second time and the file was created but there is nothing in it.
But for once, there is something different. I tried using a different mode to open the file
myFile = SD.open("test.txt", O_WRITE);
I changed the mode from FILE_WRITE to O_WRITE. Then, when the program is ended and I'm checking out the file in the sd card. I found out that "12345678" was written in the file, but actually the result I expected was "123456789". Finally, I found out that in this mode, I can only write 8 characters to the file(encoded to ANSI).
So...eventually, I didn't make any progress in solving this problem
My Hardware:
Arduino UNO R3(ATmega328P)
An SD Card Module(Sry, I didn't get its specific name, but "MH-SD Card Module" was printed on it's back) with a 512MB SD card
I connect them with dupont lines
I tried your code and here is the result:
Type any character to start
Writing to test.txt...done.
error: opening test.txt for read failed
SdError: 0XD,0X7
(test.txt created but no data in it)