Go Down

Topic: SD card with examples not working (Read 1 time) previous topic - next topic


I've got a new micro SD 4GB class 4 card and adapter for an arduino SD card module. When running the datalogger arduino example, the serial monitor shows;

Initializing SD card...card initialized.
error opening datalog.txt
error opening datalog.txt

When running the arduino example   cardinfo  serial monitor shows:

Initializing SD card...Wiring is correct and a card is present.

Card type: SDHC

Volume type is FAT32

Volume size (bytes): 3952607232
Volume size (Kbytes): 3859968
Volume size (Mbytes): 3769

Files found on the card (name, date and size in bytes):
DATALOG.TXT   2000-01-01 01:00:00 0

Initializing SD card...Wiring is correct and a card is present.
Card type: SDHC

Volume type is FAT16

Volume size (bytes): 3960733696
Volume size (Kbytes): 3867904
Volume size (Mbytes): 3777

Files found on the card (name, date and size in bytes):
DATALOG.TXT   2000-01-01 01:00:00 0

I think is something wrong with the microSD and adapter to work but not sure. Am I missing something? If is a problem with the SD card, which one would you recommend?
Thank you,


I've got a new micro SD 4GB class 4 card and adapter for an arduino SD card module.

The Arduino doesn't have an SD card module. Perhaps you added one. If that is the case, some information on which one and how you wired it would be useful.

Your code, too.
The art of getting good answers lies in asking good questions.


It seems your card is ok, but you do not close the file properly, therefore the 0 size there..


Sorry, I did it wrong and did another post saying that I have also tried with a 2GB SD card, mi mistake.

The 2GB SD card isn't working either.

The SD card module I am using is:


When running the cardinfo example says that wiring is correct. The wiring is as follows:



What can I do to close the file properly?

The code used is the arduino example datalogger, this is:

  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 <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;

void setup()
// Open serial communications and wait for port to open:
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  // 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:
  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) {
    // print to the serial port too:
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");


Sep 14, 2013, 05:07 pm Last Edit: Sep 14, 2013, 05:24 pm by pito Reason: 1
It does not work on my system either (my system works fine with SdFat lib).. Card Init failed.. CS = SS = 10
UECIDE, 1.0.5, fresh formatted 4gb fat32 card.
PS: go with SdFat library, similar demos there as well..


I too am having difficulty with SD library examples.  I can use CARDINFO with out difficulty.  Then i use the DATAlogger Example, Which seems to capture appropriate lines of data, however previous files on SD are scrambled. The Datalog.txt file is present on SD. All other files on SD are un-readable.  Kinda a bummer. No modifications are made to either example.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131