The AnalogBinLogger.ino file of SdFat library says the following:

Each 512 byte data block in the file has a four byte header followed by up to 508 bytes of data. (508 values in 8-bit mode or 254 values in 10-bit mode). Each block contains an integral number of samples with unused space at the end of the block.

I was wondering why only 254 values can be stored in 10-bit mode instead of (508*8 bits/ 10 bits ~ 406 values ).

  1. Is this because of the memory being byte-addressable and hence does a 10-bit value take up 2 bytes?
  2. If so, how will a file-reader differentiate between a 10-bit value taking 2 bytes and two 8-bit values?
  3. The AnalogBinLogger.ino says:

The logger will use SdFat's buffer plus BUFFER_BLOCK_COUNT additional buffers.

BUFFER_BLOCK_COUNT is 1 for the Arduino Uno since it has 2KB of SRAM. Each buffer is 512-byte long. Is SdFat's internal buffer also in SRAM? If I am not wrong, the libraries the program uses and the program code reside in the Flash memory, can SdFat write into a buffer in Flash memory?

  • yes it is because it takes 2 bytes. "10 bit mode" is the 10 bit mode of the ADC, the source of the data, not some 10 bit mode of the file
    – Juraj
    Commented Jun 25, 2019 at 4:48
  • @Juraj If the ADC converts the analog values to a 10-bit binary value, why does the value take 16 bits when written to the file?
    – Harini
    Commented Jun 25, 2019 at 7:11
  • @Harini: Because you cannot store bits in a file. A file is a sequence of bytes. Commented Jun 25, 2019 at 7:15
  • it is only an example, not an exercise in optimization or compression
    – Juraj
    Commented Jun 25, 2019 at 7:28
  • @EdgarBonet Sure, in that case, referring to the second part of my question: how does a file-reader differentiate between a 10-bit value and two 8-bit values? You can post it as an answer so that I can mark it accepted for this question.
    – Harini
    Commented Jun 25, 2019 at 7:39

1 Answer 1


how will a file-reader differentiate between a 10-bit value taking 2 bytes and two 8-bit values?

The file AnalogBinLogger.h, which comes with the example sketch you mentioned, contains the following:

// First block of file.
struct metadata_t {
  unsigned long  adcFrequency;     // ADC clock frequency
  unsigned long  cpuFrequency;     // CPU clock frequency
  unsigned long  sampleInterval;   // Sample interval in CPU cycles.
  unsigned long  recordEightBits;  // Size of ADC values, nonzero for 8-bits.
  unsigned long  pinCount;         // Number of analog pins in a sample.
  unsigned long  pinNumber[123];   // List of pin numbers in a sample.

Thus, the file reader just needs to be aware of the format of this first block, and read the recordEightBits field in order to know whether this is 8-bit data or not.

Is SdFat's buffer also in SRAM?

Yes, that's the only sensible place you could have a data buffer. It's allocated on the stack, only during the execution of the logData() function.

  • Is this the 'four-byte header' mentioned in AnalogBinLogger.ino which I have quoted in my question? But clearly, this header is 24 bytes.
    – Harini
    Commented Jun 25, 2019 at 9:06
  • @Harini: No. This is the first block of the file. It's 512 bytes long, not 24. The four byte header you mention is in every block except the first one. Commented Jun 25, 2019 at 9:17
  • You're right, pardon me for not accounting for the 123 elements in pinNumber. It would be helpful if you can clarify the third part of the question about SRAM and Flash memory use.
    – Harini
    Commented Jun 25, 2019 at 10:03
  • Yes, the line of code highlighted in the link provided is the additional buffer SdFat uses. The sketch mentions the use of SdFat's internal buffer and my question is whether this too resides in the SRAM.
    – Harini
    Commented Jun 26, 2019 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.