Getting started with SD cards

I am getting started using SD cards with a Arduino UNO and Adafruit Data logging shield (both genuine).

I have tried and successfully run all of the examples from the Adafruit website, Arduino Library reference, and the Examples under SD in the IDE (even though some are redundant). None of the examples do exactly what I want. Some don't cover the functionality I hope to have. We'll get to that later, much later.

In trying to understand the example's function calls, I searched them via google and on Arduino.cc with much success. The results have also led to note there are two libraries.(?) Or perhaps more precisely, one library inside another. SDFat.H inside SD.H. Am I on the right track so far?

/*
  SD card test

 This example shows how use the utility libraries on which the'
 SD library is based in order to get info about your SD card.
 Very useful for testing a card when you're not sure whether its working or not.

 The circuit:
  * SD card attached to SPI bus as follows:
 ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
 ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
 ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
 ** CS - depends on your SD card shield or module.
        Pin 4 used here for consistency with other Arduino examples


 created  28 Mar 2011
 by Limor Fried
 modified 9 Apr 2012
 by Tom Igoe
 */
// include the SD library:
#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// 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
// MKRZero SD: SDCARD_SS_PIN
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("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);


  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);

  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}


void loop(void) {

}

Although the CARDFILE example above includes SD.H. The classes and function calls are actually from SDFat.h, and not SD.H? Still correct?

Where might one find documentation listing and describing functions available in the SD.H and SDFat.h libraries? (the links in threads here are all out of date)

SDFat.H inside SD.H. Am I on the right track so far?

Sorry, no. SDFat and SD are different libraries. They are mutually exclusive.

Although the CARDFILE example above includes SD.H. The classes and function calls are actually from SDFat.h, and not SD.H? Still correct?

No. There are classes in both libraries with the same name. The reason is that SD is actually an older version of the SDFat library.

Where might one find documentation listing and describing functions available in the SD.H and SDFat.h libraries?

The definitive source of information is the code itself.

PaulS:
Sorry, no. SDFat and SD are different libraries. They are mutually exclusive.

You're going to have to bear with me. This is new information, not correlating to what I have read. Since I seem to be on the wrong track, let's start at the top.

Everything I have read says SD.H is a wrapper for SDFAT.H. The CARDFILE example only includes SD.H. You are saying they are mutually. That says to me that sdfat.h is not needed (since they are mutually exclusive and not included in the sketch).

Everything I have read says SD.H is a wrapper for SDFAT.H

For an older version of SDFat, NOT for the current version of SDFat.

That says to me that sdfat.h is not needed (since they are mutually exclusive and not included in the sketch).

Then you heard correctly. Unless you want the newer-and-much-improved version...

PaulS:
For an older version of SDFat, NOT for the current version of SDFat.

Just as an FYI, I'm working with Arduino IDE v1.8.4, installed today.

PaulS:
Then you heard correctly. Unless you want the newer-and-much-improved version...

If I heard it correctly, then I am understanding what you wrote. So I went ahead deleted the SDFAT.H. CARDINFO will no longer compile. Here is the error(s) from the Arduino IDE.

Arduino: 1.8.4 (Windows 10), Board: "Arduino/Genuino Uno"

In file included from C:\Program Files (x86)\Arduino\libraries\SD\examples\CardInfo\CardInfo.ino:24:0:

C:\Program Files (x86)\Arduino\libraries\SD\src/SD.h:20:27: fatal error: utility/SdFat.h: No such file or directory

 #include <utility/SdFat.h>

                           ^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

No, PaulS said (Delete SdFat.h) Unless you want the newer-and-much-improved version...

Although you are correct that I did it backwards. There must be something more to the story.

If they are mutually exclusive as PaulS said, then I should have been able to delete SDFAT.H and still be able to compile the example program. It shows that SD.H and SDFAT.H are related. Taking PaulS' third response in post #1 to heart, they are related.

/*

 SD - a slightly more friendly wrapper for sdfatlib

 This library aims to expose a subset of SD card functionality
 in the form of a higher level "wrapper" object.

 License: GNU General Public License V3
          (Because sdfatlib is licensed with this.)

 (C) Copyright 2010 SparkFun Electronics

 */

#ifndef __SD_H__
#define __SD_H__

#include <Arduino.h>

#include <utility/SdFat.h>
#include <utility/SdFatUtil.h>

There is the include in SD.H that rolls in SDFAT.H. They are not mutually exclusive. There's even a comment about it being a wrapper.

How do I swap out:

#include <SD.h>

for

#include <utility/SdFat.h>
#include <utility/SdFatUtil.h>

Without getting the errors:

Arduino: 1.8.4 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\anthony\AppData\Local\Temp\arduino_modified_sketch_225090\CardInfo.ino:24:27: fatal error: utility/SdFat.h: No such file or directory

 #include <utility/SdFat.h>

                           ^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

They are not mutually exclusive.

They ARE mutually exclusive. Is there some part of "SD is a wrapper for (an old version of) SdFat" that you don't understand?

The NEW version of SdFat is NOT the same as the old version that is wrapped by SD.

You need to decide whether you want to use SD which wraps an old version of SdFat OR the new-and-(much)improved SdFat.

How do I swap out:

You don't. There is no need to.

PaulS:
They ARE mutually exclusive. Is there some part of "SD is a wrapper for (an old version of) SdFat" that you don't understand?

Yes, obviously. I found a file called SD.H, and in that there is an include of SDFAT.H. Standard logic dictates they are not mutually exclusive.

PaulS:
The NEW version of SdFat is NOT the same as the old version that is wrapped by SD.

You need to decide whether you want to use SD which wraps an old version of SdFat OR the new-and-(much)improved SdFat.

It sounds like this would be a good place to start the explanation. How about "old" versus "new". How much newer can it be if I just installed IDE 1.8.4 yesterday?

How much newer can it be if I just installed IDE 1.8.4 yesterday?

I could post a picture of my smiling face, taken 20 years ago. The timestamp on the post being today's date would NOT mean that that is how I look today.

The SD library "took a picture" of the SdFat library some time ago. SdFat has matured since then. The SD library? Not so much.

So there are two SDFat libraries? I don't see a second one available in the Arduino List (Sketch->Include Library->Manage Libraries). I see the one titled 'SD Built-In by Arduino, Sparkfun'. What would it be called?

I don't see a second one available in the Arduino List (Sketch->Include Library->Manage Libraries

That really appears to be a useless tool.

PaulS:
That really appears to be a useless tool.

GitHub - greiman/SdFat: Arduino FAT16/FAT32 exFAT Library

That explains two things at once. (A) there are two libraries with the same name and (B) why it wasn't apparent.

Wait until you get to LiquidCrystal_I2C. Lots of libraries all with the same name but they have different function and procedure names...

dannable:
Wait until you get to LiquidCrystal_I2C. Lots of libraries all with the same name but they have different function and procedure names...

Been there done that, BUT there is not a library or file pre-installed with the same name. The links and information "alerted" me multiple add-on libraries with the same name. Neither applies to the SDFAT library.