Go Down

Topic: Can´t write into file (Read 7913 times) previous topic - next topic

pgmartin

I am testing a New SD shield using my ARDUINO UNO and the SD.h library that comes wit IDE 022, using the example sketches.
Im accessing and reading text files from a 512MB SD card withou problem.
Trouble appears when trying to write. It will create files, but no content gets added. Files keep empty.
When I try to create a new directory, it also fails, it creates just another file. =(
The contents of the SD card I´m monitoring with a Windows Vista PC. I´ve formatted the card completely, and its using 16KB sectors.

Help will be very much appreciated.

PaulS

How about posting your code? Then, help will be easier to provide.

pgmartin

Sorry, I didn't post it because is just one of the exaple files of the library.



Code: [Select]
/*
  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
** CS - pin 4

created   Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe

This example code is in the public domain.

*/

#include <SD.h>

File myFile;

void setup()
{
  Serial.begin(9600);
  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(10, OUTPUT);
   
  if (!SD.begin(4)) {
    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());
    }

davekw7x

#3
Mar 21, 2011, 06:21 pm Last Edit: Mar 21, 2011, 06:25 pm by davekw7x Reason: 1

... exa[m]ple files of the library...

Except for a couple of missing '}' braces and the missing empty loop() function, that is the Arduino SD library ReadWrite example sketch, right?

That example sketch works for me on an "official" Arduino Duemilanove with an "official" Arduino Ethernet Shield Version 5 (the one with the microSD socket), using a 4 Gigabyte micro SDHC card formatted (as it came from the factory) as "FAT32"

I can read and write the card using various card readers on my Windows XP and Linux workstations.

Originally there was no file named "test.txt" but one was created the first time I ran the sketch.  The file contained the single line "testing 1, 2, 3."

Each time I run the sketch it appends another "testing 1, 2, 3."  line to the file.

So...

If you are, indeed, using the ReadWrite example sketch from the Arduino SD library, the question is: What is your hardware setup?  What Arduino? What Shield?  Is there anything else plugged into/onto your Arduino?

What sketch(es) did you run to verify that your setup can read from the microSD card?


Regards,

Dave

pgmartin

Yes, I've been testing the hardware using the example files.
I use an Arduino UNO clone with an SD shield (labeld lcsoft.net only, like this http://cgi.ebay.com/SD-Card-Slot-Socket-Reader-Module-Arduino-ARM-MCU-/270722127373?pt=LH_DefaultDomain_0&hash=item3f084bd60d), both of chinese manufacture.
The SD shield is pretty uncomplicated, it has a couple of resistors and the conectors to the SD card.
I've done the following tests:

  • After running the script, the file was created, with no content

  • If I connect the SD card into my computer using windows vista, create a file, with content, and the I read it with this same code (just comenting the writing part) the conthent of the file is shown on the serial monitor without a problem



I've also tested the example code that make a Dump of the entire file, and it also works. The problem is allway when trying to write content.
Is there any way to debug it, or maybe search with an hex editor if the info is in the wrong place in the sd card?

arduimat

Use a lower resistor to power it !

pgmartin


Use a lower resistor to power it !


I'm powering the shield with 3.3V. Are you saying that I need voltage dividers in the data pins?

GoForSmoke

I have LCSOFT SD modules, link: http://www.lctech-inc.com/Hardware/Detail.aspx?id=0c3b6f7a-d101-4a60-8b56-3abfb7fd818d

They take either 3.3V or 5V. I have only run them on 5V. The power level circuits are built in and raised some eyebrows but they work. Hardest part for me was getting up nerve to try but I got a good price on 3 at eBay.

Also make sure the SD card is formatted FAT-16, the most compatible. And IMO the SD-Fat library is better.

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

Go Up