I'm documenting this for myself and anyone else interested. Researching suggests that the Print function (Serial.print, display.print, lcd.print, etc.) has an issue with counts greater than 4GB or 4,294,967,296. If you are using an 8GB or 16GB or larger SD card and have formatted it with SDFormatter as per
https://www.sdcard.org/downloads/formatter_4/, referenced first in
https://learn.adafruit.com/adafruit-dat ... he-sd-card, the value achieved will be too big to print and it will "fail" with the wrong value. The reason is because we are multiplying the 'number of bytes per block or sector' by 'number of blocks per cluster' by 'cluster count'. Change your code such that 'bytes per block' is multiplied by 'cluster count' then immediately divided by 1MB, before the result is multiplied by 'blocks per cluster'. Example:
(512 x volume.clusterCount /1048576) and then multiply result by volume.blocksPerCluster. My experience has shown that if this is broken into two operations, the number cannot get larger than the Print limit.
Code: Select all
Serial.print("Bytes per block/sector: "); Serial.println(512); // 512.
Serial.print("volume.blocksPerCluster: "); Serial.println(volume.blocksPerCluster()); // 64
Serial.print("volume.clusterCount: "); Serial.println(volume.clusterCount()); // Large value.
uint32_t CCx512 = ((512 * volume.clusterCount()) / 1048576); // Divide by 1MB to keep the number low for next operation.
Serial.print("CCx512: "); Serial.println(CCx512);
uint32_t bPCxCCx512_GB = ((CCx512 * volume.blocksPerCluster()) / 1024); // Divide by 1KB to convert the MB value to GB.
Serial.print("bPCxCCx512_GB: "); Serial.println(bPCxCCx512_GB);
Serial.print("Formatted SD card size: "); Serial.print(bPCxCCx512_GB); Serial.println("GB");
Serial.println();