Go Down

Topic: Sd card read/write sector library (Read 15082 times) previous topic - next topic


This class was written for the Arduino library.

It reads and writes directly to sectors on a sd card.
It's use is for mass storage on the arduino platform.
It's not to store data and then load it(without a special program) to a PC.

Import the library:


#include <SDCARD.h>

It has only two functions:


SDCARD.readblock(sector number); //read 512 bytes from this sector
SDCARD.writeblock(sector number); //write 512 bytes to this sector

The 512 bytes are read/write from a buffer which must be declared as global:


unsigned char buffer[512] ; //contains the 512 bytes to read/write

Operational notes:
The code reads/writes direct to the sectors on the sd card.
It does not use a FAT(always FAT16).
If a 2GB card has been formatted the partition(sector 0), boot (around sector 135),
FAT's(around sectors 100-600) and the root directory(around 600-700) can be written over.
The data in files(above sector 700) can also be written over.

The card can be reformated, but be aware that if the partition(sector 0)
has been written over formating with windows XP or 7 will not restore it.
It will put the boot sector at 0 which can confuse some(not all) programs.

The playground has SDuFAT-basic - library to handle SD cards from Arduino compatible platforms.
This program cannot run when the boot sector is 0.

I have found that my cannon digital camera will restore the partition
and boot to the origional factory conditions.
No doubt there are other format programs which will use a partition sector.

I have previously described this library class.


I have made several improvements to the code. It is now faster and can handle
a greater range of sd cards.

It can handle Ver2.00 or later Standard SD Memory Cards.
256 MB, 1.0 and 2.0 GB cards purchased from 2008-2011 work well.

The library and the circuit are in SDCARDsectors.zip
Download if from:


Paste the folder SDCARD into the library:

Then from the IDE under:


You should see two programs:


Feb 22, 2011, 04:39 pm Last Edit: Feb 22, 2011, 04:44 pm by tecmac Reason: 1
Hey carl47,

Many thanks for this library, almost exactly what I was looking but I had to make some minor modifications to suit my needs:

1. Be able to use any pin for SS that I choose, not just D10.
2. Be able to use the existing SPI library instead of implementing its own SPI comms code.

In case anyone needs the same requirements I had, I packaged my changes on to a modded version of carl47 library called SDCARDmodded ;-)

Main visible changes are:

SDCARDmodded.readblock(sector number, port); //read 512 bytes from this sector, port is the SS pin (any pin you choose)
SDCARDmodded.writeblock(sector number, port); //write 512 bytes to this sector, port is the SS pin (any pin you choose)
and sketch will require an #include <sdi.h> to run.

Read description and get modded library from here: http://code.google.com/p/wifispeak/downloads/detail?name=SDCARDmodded.zip&can=2&q=

Hope it helps,
Cheers and thanks carl47


Happy it worked for you.

Your mods are a definate improvement.
I had no end of trouble trying to expain how to change my code to someone who needed a different SS pin.
You have done it very well.

Like your blog.


I have updated a small changed code reading analog values from sensors and update in sd card with 10 files.
I have already posted in the below link.

Tenet Technetronics



This library looks more then very interesting, but it doesn't seem to work here. Also the modded version mentioned above is not giving anything usefull.

The example "checksdcard" fails at various different start sectors I tried; always returning error 2 (Failed to initialize)

The weird thing is, my SD cards (one brand new Sandisk 2GB / and an older Toshiba/Kingston 1GB) are perfectly readable with the relatively huge Arduino SD lib. There I'm able to create files and read them back. That library is, unfortunately, way too big for my already heavy sketch. Also; I only need raw access, nothing fancy. I just need too write a huge amount of data once and  then "play" that back.

I don't necessarily need to do a lowlevel format or something on the card, do I?

Any ideas?

Also, could it be that my cards aren't fully compatible or something? Any hints for alternative brands?


I am disappointed that it does not work for you.

A format will make no difference to this program.

I have 2 SanDisk 2GB cards bought in the last 6 months.

Both work perfectly.

As long as the connections are as for my setup and the leads are not too long
(2 inches in my case)I don't have any idea.

As the SD lib reset must work how about using their reset function in:

unsigned char SDCARDclass::SD_reset(void)
use SD lib reset
}//end of SD_reset function

I'm sorry I can't be more helpfull.


Thanks for your reply. I bought another (512MB) card that works fine :)

Will conduct more testing soon, to see if I'm able to use my other two cards too.


Happy that it works on at least one card.

Please let me know if you have any luck diagnosing the problem with the other two.


hi . i have problem when tray to run your library
some message error say that     fatal error: WProgram.h: No such file or directory

pls can you help me solve this


Hi, I'm new to write in a forum, I hope to write in the right place and not create problems.

I'm in trouble with SDCARD library.
I use the modded version of SDCARD, because I need to assign a specific SS pin.

My problem is :
With an old 128MB SD card all work well, but with SD cards available on the market now, the test "readwritesectormodded.pde" return to me "sd card read error... code =  2".

I tried to solve by myself, I did not want to bother anyone, but I can not do it.

I think the problem is about the type of SD card.
I think the solution is explained at this link: http://elm-chan.org/docs/mmc/mmc_e.html
in the "Initializing high-capacity cards" section, but I'm not that expert to apply it myself.

Can anyone help me ?
Thanks in advance.


My problem is :
With an old 128MB SD card all work well, but with SD cards available on the market now, the test "readwritesectormodded.pde" return to me "sd card read error... code =  2".
I doubt you still need an answer, but the reason it works for old cards, but not for new, is because this library is written for "SD" cards, while almost all cards today are "SD High Capacity" (or "SDHC") or even newer "SDXC"  or "SDUC".

If you're really desperate you could attempt modifying the code to send the correct instruction set for a newer format using for example this or even sth like this.

I seems manageable, but I'm just a fresh uni graduate, so don't take my word for it!

Go Up