Hello,
I'm struggling to write to an SD card. I'm using an official Arduino Uno and have tested with this SD card module.
I used the right formatter and have tested with following SD cards:
- Kingston sdhc micro SD 8GB class 4 in an adapter
- SanDisk Ultra SDHC 8GB class 10
- SanDisk Ultra Micro SDHC 16GB class 10 in an adapter
- Kodak by EMTEC SDHC 8GB class 10
The code I used is the "Datalogger" example of the SD.h library that comes with arduino 1.6.10
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
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...");
// 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() {
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
When i execute this code serial looks like this:
Initializing SD card...card initialized.
error opening datalog.txt
296,304,294
303,312,304
297,303,296
296,303,297
302,310,303
This goes on.
When I run it again it looks like this:
Initializing SD card...card initialized.
402,371,333
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
When I run it again it's the same as the last one.
On the first run the file gets created. And this is visible on the SD. But the file is empty.
I have ran other tests, and i can read files (i created .txt files on the computer). But as soon as i use write or print functions reading doesn't work anymore. (file object returns false).
Sometimes when writing to the SD cards I end up with 6 or more files, in various sizes (few GBs) and from different dates with names of messed up ascii characters. I am unable to delete them and have to format the card.
I've looked up a lot of things because I thought this would be a common issue but I did not find a solution.
I have tried including pinMode (10, OUTPUT); into the setup but I think it's already included in the library. It made no difference.(found it in this post)
I've also found this post which says:
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.
...
Writing requires much more power than creating files, deleting files, create folders, navigate into folders... That's why you're facing this issue.
But it's about a different SD card module which doesn't have a 5V pin like mine has.
I have no more ideas what to do. Is this the end?