Using the Adafruit Datalogging shield P1141 and UNO R3 running a program to record data to SD Card. Took it out last week and it recorded data and worked fine.
I have used the SD Formatter program program to format the cards. I have 2 8GB and 1 32GB SD Cards. Size is reported wrong – 3485 MB for the 8GB card.
I first got it to work using the SD datalogger program referred to at: Adafruit Assembled Data Logging shield for Arduino : ID 1141 : $13.95 : Adafruit Industries, Unique & fun DIY electronics and kits
The other day I was modifying the other part of the code and it quit initializing the SD card. I thought maybe it was broken so I tried a brand new datalogging shield #1141 and even a new UNO R3. No play - it will not open the file on the card. I have tried 2 or 3 of the SD reference sketches. They all fail at one point or another, now. Backing up to the examples. Example code and results below…
When it does see the card, it reports the size wrong (like 3.4GB for an 8 GB card – see printout).
That makes me think that I have something wrong in the driver calls. Maybe getting my libraries mixed up? Do I need to use a different libraey for the larger SD cards? 8 GB is the smallest I could buy new!
Call using: if (!card.init(SPI_HALF_SPEED, chipSelect)) does report the card present.
The initial call I used to int card_status = SD.begin(10) always fails.
Card.init() It initializes the card but gets the size wrong. Call to SD.open fails.
.. . . . . . . . . . . . .
OUTPUT to Serial Monitor:
//Sketch_dec18b
Initializing SD card...At volume.init(card): Wiring is correct and a card is present.
Card type: SDHC
Volume type is FAT32
Volume size (bytes): 3654471680
Volume size (Kbytes): 3568820
Volume size (Mbytes): 3485
At root.openRoot(volume) call.
Files found on the card (name, date and size in bytes):
New filename is: LOGGER00.CSV
At call to SD.exists(filename): file not there, open it.
After call to SD.open(filename), returns: 0
0Failed - couldnt create file: LOGGER00.CSV
CODE:
//Test_18b-121716 modified by Bill Allen
#include <SPI.h>
#include <SD.h>
File logfile;
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 10; // was 4
void setup() {
pinMode(chipSelect,OUTPUT);
Serial.begin(9600);
while (!Serial) {
}
Serial.println("Sketch_dec18b");
Serial.print("\nInitializing SD card...");
Serial.print("At volume.init(card): ");
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.");
}
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");
}
if (!volume.init(card)) {
Serial.print("At volume.init(card): ");
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
uint32_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster();
volumesize *= volume.clusterCount();
volumesize *= 512;
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("\nAt root.openRoot(volume) call. \nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);
root.ls(LS_R | LS_DATE | LS_SIZE);
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i / 10 + '0';
filename[7] = i % 10 + '0';
Serial.print("\nNew filename is: ");
Serial.println(filename);
Serial.print("At call to SD.exists(filename): ");
if (!SD.exists(filename)) {
// only open a new file if it doesn't exist
Serial.println("file not there, open it.");
delay(50);
logfile = SD.open(filename, FILE_WRITE);
Serial.print("After call to SD.open(filename), returns: ");
Serial.println(logfile);
break; // leave the loop!
}
}
if (!logfile) {
Serial.print(logfile);
Serial.print("Failed - couldnt create file: ");
Serial.println(filename);
}
else{
Serial.print("File successfully opened: ");
Serial.println(filename);
}
// Yes this should be behind other code but let's see if it will write?
for(int i=0; i<10; i++) {
logfile.print(analogRead(0));
logfile.print(",");
}
logfile.flush();
// logfile.close(); // When do I need to Close the file or does flush take care of the data?
}
void loop(void) {
}