how to access a specific data byte on SD card ?


Well-known member

I have made a MIDI controller and want to save some presets on SD card. I used to do this with EEPROM. But I have too much data so I want to store data on SD card. I use a T3.6.
I can write and read my data on SD card but I can't figure out a way to retrieve data at a specific address.
This was easy with EEPROM, you just have to specify an address : EEPROM.put(myAddress, my data) / EEPROM.get(myAddress, myData).
How is it possible to save and access data on SD card using some kind of index or address ?

My data are very simple. It is a list of bytes :

Thanks for help !
Layers of software and hardware exist between your code and the actual non-volatile storage inside the SD card. The answer to your question really depends on which of these layers you want to mentally consider and which you hope to keep blissfully out of sight, out of mind.

In the simplest approach where you just use the SD library and don't worry about any low-level details, the answer is pretty easy. Just open a file. When you want to write 1 byte at a particular position, just use and myfile.write(byte). When you want to read it, also very simple, and byte = If this gives good enough performance, why bother with anything more complicated?

If you want higher performance, you're going to need work with chunk sizes much larger than 1 byte. This usually requires allocating RAM and designing more complex code to manage your data. There are really 3 chunk sizes that make sense. The smallest is 512 bytes, which is the sector size used in the SD card's communication protocol. Next is 4096 bytes. Cards with "A1" or "A2" rating have optimization (or at least performance specs) for managing data in 4K chunks. Largest is the native block size inside the card, but this info is hard to access and with modern cards it can be 128K or larger, so usually not very practical from a microcontroller with limited on-chip RAM.

For SDXC cards (larger than 32GB) come with exfat format, which supports creating files with continuous allocation. If you create a large file this way, the SD / SdFat library can skip extra work when seeking within the file when the seek crosses FAT filesystem cluster boundaries. Obviously this gives no benefit for relatively small files that fit within 1 cluster.

If you're really wanting to dive into details, you could try to access the SD card directly rather than going through the SD / SdFat library. But this probably won't give any benefit beyond using a continuously allocated file and performing all reads and writes in 512 or 4096 byte chunks. The library will just translate your high-level requests into card access if you follow that sort of access pattern. If you do abandon the library and access the raw SD card (using the SD protocol which accesses a controller inside that card that still does a lot of extra manipulation you can't control) just keep in mind the low-level protocol uses 512 byte sectors. It has substantial overhead and with A1 or A2 rated cards, you'll find the sweet spot for performance is usually working with chunks of 8 sectors.
Thank you so much Paul, this is exactly the information I was looking for.
I take the occasion of this thread to to say how much I appreciate Teensy chips and the community. Amazing work, amazing people !