SD Card creates file but doesnt't write in it ! ! !
The funny thing is when I use the same code with UNO it writes.
But in Arduino Mega it doesn't write in it.
The code is
/*
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:
** UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
and pin #10 (SS) must be an output
** Mega: MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
and pin #52 (SS) must be an output
** Leonardo: Connect to hardware SPI via the ICSP header
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>
File myFile;
// 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
const int chipSelect = 53;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(53, OUTPUT);
digitalWrite(53, HIGH);
if (!SD.begin(chipSelect)) {
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");
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
}
#include <SD.h>
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 Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(53, OUTPUT);
if (!SD.begin(53)) {
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");
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
}
Or you have a problem with the connections or you have a problem with the SD card it self. As you said that in the UNO works, I think it only lefts the problem with the connections.
The wiring is right. I had the exact same problem. It's a voltage problem. Well, in fact it is a current problem.
You should NOT power up the SD module with 5V.
Instead, use Arduino's 3.3V pin to power up the SD module, so you will bypass it's voltage regulator. That voltage regulator is the source of the problem as it can't drive the current needed for writing. Maybe it can work in some SD cards. But in some cards it won't work.
That is, connect the 3.3V Arduino's pin into the 3.3V SD module's pin.
However you'll be facing a second problem:
MOSI, MISO SCK and CS should be working at 3.3V or you will kill the SD card. Not today but in a near future.
You have two options:
Use a voltage conversion chip in MOSI, MISO SCK and CS.
Operate the Arduino at 3.3V
Writing requires much more power than creating files, deleting files, create folders, navigate into folders... That's why you're facing this issue.
I know the topic is out of date, but I had exactly the same problems and would like to share my solution, I hope it will help people:
Standard SD cards require 3.3V power, but also use 3.3V logic for the MISO, MOSI, SCK, CS pins.
Arduino (UNO/MEGA) uses 5V logic on GPIO pins. So the breakout board or circuitry you use to control the SD card must not only supply the SD card with 3.3V, but also convert the 5V logic of Arduino to 3.3V. Some breakout boards do this, but some do not (those that are designed for 3.3V systems).
I once used a breakout board that did not convert the voltage levels and it resulted in exactly the faults that the OP described. I could open and read files, create new files, but not write in any file. It was very confusing! Occasionally the whole card would scramble, requiring a format. This was all the result of using 5V logic directly on the SD card pins. It somehow worked to some extend, but not really.
I managed to make it work by using simple voltage dividers (1K and 2K resistors) between the Arduino GPIO pins and the MISO, MOSI, SCK, CS pins of the breakout board. Not a very elegant or definitive solution, but it confirmed the problem all along.