Reading from SD card

Hi guys!

I have a problem reading number from SD card.
I have .txt file o SD card with numbers 123 in file.

I would like to read this number and write it to variable.

#include <SPI.h>
#include <SD.h>

File myFile;
unsigned long g;
void setup() {

Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

myFile = SD.open("test.txt", FILE_WRITE);

g = myFile.read();

myFile.close();
Serial.println(g);
}

void loop() {

}

Whit this code i get number 4294967295 every time.

Can someone help me please?
Thanks.

gapl:
Hi guys!

I have a problem reading number from SD card.
I have .txt file o SD card with numbers 123 in file.

I would like to read this number and write it to variable.

#include <SPI.h>

#include <SD.h>

File myFile;
unsigned long g;
void setup() {

Serial.begin(9600);
 while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only
 }

myFile = SD.open("test.txt", FILE_WRITE);

g = myFile.read();

myFile.close();
Serial.println(g);
}

void loop() {
 
}




Whit this code i get number 4294967295 every time.

Can someone help me please?
Thanks.

look at the example code, you never initialized the SD card

/*
  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
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#include <SPI.h>
#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...");

  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());
    }
    // 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
}

SD.begin(CSpin);

Chuck.

Chuck thanks for the quick replay.

I use arduino uno and use pins as is described in tutorial, because of that they didn't initialized at beginning.

gapl:
Chuck thanks for the quick replay.

I use arduino uno and use pins as is described in tutorial, because of that they didn't initialized at beginning.

Your code NEVER initialized the SD card. you must use SD.begin(CSpin) to tell the SD library which Arduino pin is connected to your SDcard Shield's CS pin. Some SD adapters directly use Pin10, others Pin4, and many are not laid out as a 'shield', they just have bare pins.

Also, The UNO is a 5V module. all SDcards are 3.3V. Many of the cheaper SDcard adapters are designed to work with 3.3V CPU's. Unless they have builtin voltage level shifters 5V->3.3V you have to provide a circuit to convert the voltages.

Hopefully your SDcard adapter has the 5V signal converters installed.

Chuck.

I have this SD card reader http://www.ebay.de/itm/272186427852 which work with 5V.

Must say that ReadWrite tutorial Works perfect.

I add initialization to my code but still doesn’t work.....

Gasper

gapl:
I have this SD card reader http://www.ebay.de/itm/272186427852 which work with 5V.

Must say that ReadWrite tutorial Works perfect.

I add initialization to my code but still doesn’t work.....

Gasper

Post your non working code.

Place [code] before and [/code] after your code. It makes it easier to read and test.

Chuck.

I add initialization to my code but still doesn't work.....

Pay attention to the differences between your code and the ReadWrite example.

Your code

myFile = SD.open("test.txt", FILE_WRITE);

    g = myFile.read();

    myFile.close();

I have .txt file o SD card with numbers 123 in file. You only read the first character with

    g = myFile.read();