|
| 1 | +/* |
| 2 | + SD card test |
| 3 | + |
| 4 | + This example shows how use the utility libraries on which the' |
| 5 | + SD library is based in order to get info about your SD card. |
| 6 | + Very useful for testing a card when you're not sure whether its working or not. |
| 7 | + |
| 8 | + The circuit: |
| 9 | + * SD card attached to SPI bus as follows: |
| 10 | + ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila |
| 11 | + ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila |
| 12 | + ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila |
| 13 | + ** CS - depends on your SD card shield or module |
| 14 | +
|
| 15 | + |
| 16 | + created 28 Mar 2011 |
| 17 | + by Limor Fried |
| 18 | + */ |
| 19 | + // include the SD library: |
| 20 | +#include <SD.h> |
| 21 | + |
| 22 | +// set up variables using the SD utility library functions: |
| 23 | +Sd2Card card; |
| 24 | +SdVolume volume; |
| 25 | +SdFile root; |
| 26 | + |
| 27 | +// change this to match your SD shield or module; |
| 28 | +// Arduino Ethernet shield: pin 4 |
| 29 | +// Adafruit SD shields and modules: pin 10 |
| 30 | +// Sparkfun SD shield: pin 8 |
| 31 | +const int chipSelect = 8; |
| 32 | + |
| 33 | +void setup() |
| 34 | +{ |
| 35 | + Serial.begin(9600); |
| 36 | + Serial.print("\nInitializing SD card..."); |
| 37 | + // On the Ethernet Shield, CS is pin 4. It's set as an output by default. |
| 38 | + // Note that even if it's not used as the CS pin, the hardware SS pin |
| 39 | + // (10 on most Arduino boards, 53 on the Mega) must be left as an output |
| 40 | + // or the SD library functions will not work. |
| 41 | + pinMode(10, OUTPUT); // change this to 53 on a mega |
| 42 | + |
| 43 | + |
| 44 | + // we'll use the initialization code from the utility libraries |
| 45 | + // since we're just testing if the card is working! |
| 46 | + if (!card.init(SPI_HALF_SPEED, chipSelect)) { |
| 47 | + Serial.println("initialization failed. Things to check:"); |
| 48 | + Serial.println("* is a card is inserted?"); |
| 49 | + Serial.println("* Is your wiring correct?"); |
| 50 | + Serial.println("* did you change the chipSelect pin to match your shield or module?"); |
| 51 | + return; |
| 52 | + } else { |
| 53 | + Serial.println("Wiring is correct and a card is present."); |
| 54 | + } |
| 55 | + |
| 56 | + // print the type of card |
| 57 | + Serial.print("\nCard type: "); |
| 58 | + switch(card.type()) { |
| 59 | + case SD_CARD_TYPE_SD1: |
| 60 | + Serial.println("SD1"); |
| 61 | + break; |
| 62 | + case SD_CARD_TYPE_SD2: |
| 63 | + Serial.println("SD2"); |
| 64 | + break; |
| 65 | + case SD_CARD_TYPE_SDHC: |
| 66 | + Serial.println("SDHC"); |
| 67 | + break; |
| 68 | + default: |
| 69 | + Serial.println("Unknown"); |
| 70 | + } |
| 71 | + |
| 72 | + // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32 |
| 73 | + if (!volume.init(card)) { |
| 74 | + Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card"); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + // print the type and size of the first FAT-type volume |
| 80 | + long volumesize; |
| 81 | + Serial.print("\nVolume type is FAT"); |
| 82 | + Serial.println(volume.fatType(), DEC); |
| 83 | + Serial.println(); |
| 84 | + |
| 85 | + volumesize = volume.blocksPerCluster(); // clusters are collections of blocks |
| 86 | + volumesize *= volume.clusterCount(); // we'll have a lot of clusters |
| 87 | + volumesize *= 512; // SD card blocks are always 512 bytes |
| 88 | + Serial.print("Volume size (bytes): "); |
| 89 | + Serial.println(volumesize); |
| 90 | + Serial.print("Volume size (Kbytes): "); |
| 91 | + volumesize /= 1024; |
| 92 | + Serial.println(volumesize); |
| 93 | + Serial.print("Volume size (Mbytes): "); |
| 94 | + volumesize /= 1024; |
| 95 | + Serial.println(volumesize); |
| 96 | + |
| 97 | + |
| 98 | + Serial.println("\nFiles found on the card (name, date and size in bytes): "); |
| 99 | + root.openRoot(volume); |
| 100 | + |
| 101 | + // list all files in the card with date and size |
| 102 | + root.ls(LS_R | LS_DATE | LS_SIZE); |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +void loop(void) { |
| 107 | + |
| 108 | +} |
0 commit comments