Exploring SDCards and how to talk to them (all).

Latest Experimental Code



The first version of the SD Card code I did just supported the 2 1GB disks I had available. The spec actually has lots of branches, and I only implemented what I needed, so this obviously means there are lots of cards that don't work, as reported in the forum. This page will log some of my experiments to expand the support to more cards.

First, lets get one we know works, working...

Using the example in the cookbook, I constructed the simple program and ran it on a board with a known good SD card (Kingston 1GB) on a nice new SD Card testrig:

and got a file on the disk and this in the terminal:

Hello World!
Goodbye World!

Note that if the disk is not in, or the wiring is wrong, the result was:

Hello World!
Not in idle state
Could not open file for write

Now, we collect as many cards as we can find!

The Kingston 1GB is the top left, formatted as FAT.

With the other cards, we get:

Under the hood, this means it is not successfully getting past the initialisation for most of the cards. Lets investigate...

If you look at the spec, after moving to SPI mode you should send CMD8 - I'd ignored this as it wasn't essential, but it is a way to determine what commands/version the card is. Sending CMD8, I get:

Based on Alessandro's comment, I experimented with clocking the card after sending commands and cs has gone high. For the newer cards, these become recognised as version 2.0 rather than failing with a command error.

The program demonstrating that is:


This makes the cards change from a fundamental failure to a "this card tastes funny" which is more about the block sizes. Feel free to run and test. The older cards still timeout, so I think a focus should be doing an official startup first (CMD8, detect pass/fail to determine version 1 or 2 of std):

Here is an updated example that identifies a card as version 1.0 or 2.0 on the terminal:


If you have an SD card, please run this and report back with a photo of the SDCard and the terminal output so we can get an idea of what cards are what version. Still needs to actually implement the rest of the card setup for all versions to make it actually work :)...

Had a bit more of a jack and added support for these other modes, and stopped it bombing out when it thought the block size was too big (was actually the maximum block size).

Latest Code

This code writes a file to every one of my SD cards pictured (not the MMC card!):


Please test it yourself. If it looks like it is working, i'll refactor it to get rid of all the duplicated code.


19 Nov 2009

I'll root around at home and see if I can find any more specimens to bring in!

19 Nov 2009


Just got my mBed a couple of days ago.

Just tried the SD card example code and all works fine with my new

Disgo 1GB (

SD card.

I'm finding the mBed a great device to try ideas with.

I do use ARM7 & LPC17xx series parts professionally and found it strange to write code on a web based compiler but find it very good.

Using mBed website on both Mac & PC based computers.

On the mac and finding that it is putting in double line spacing in these comments!



04 Dec 2009

I found that adding a _spi.write(0xFF) after you de-assert cs (after _cs=1) does some magic for the Sandisk 2GB.  Particularly inside the _cmd() method :)

14 Dec 2009

Thanks Alessandro, good tip! Can't find it in the official documentation, but is certainly impacting some of the newer cards that now confirm they are v2 of the spec with this addition.

Did you just find this as a tip off in forums, or somewhere official?

14 Dec 2009


Unfortunately I just found that by chance, and could not find anything official to back it up.  It may just be masking something that's being neglected, perhaps some timing constraints on cs.


17 Jan 2010


Your latest code worked with two of my three SD cards.


The unbranded 128MB and the Sandisk Ultra II 512MB were fine but the Filemate 2GB was not recognised.

No disk, or could not put SD card in to SPI idle state
Didn't get a response from the disk
Set 512-byte block timed out
Could not open file for write

My PC didn't recognise the Filemate either, but my printer saw it ok.


09 Mar 2010

My SanDisk 1Gb (MicroSD) and Transcend 2Gb (MicroSD) both work with the most recent SDFileSystem.cpp and SDFileSystem.h files.

However I found the files linked to in the cookbook ( are older than those given in this thread. Neither card worked with the files linked to in the cookbook, I had to manually copy those from Simon's SDCardTest project. All works fine now though, ta :)


11 Apr 2010

hello Simon,

This is the SD I have found . I need to know how to conect it with the mbed. It would be useful if you could do a drawing.

12 Apr 2010

Hi Jose,

I don't think that is an SD Card unfortunately. M2 stands for the Sony Memory Stick Micro, which is not the same as the SD Card format. No idea if you can connect to these things easily.


13 Apr 2010

ok! thanks for the information! it was a memory card from a movil phone. I have just bought a SD and an adapter, when they arrive I will try the code.


20 Apr 2010

The Sd card just arrived, I tryed the code and got the correct message. :)    ScanDisk microSd 1Gb (like one of your Sd cards)

16 Jun 2010 . Edited: 16 Jun 2010

How can I delete this post?

24 Jun 2010 . Edited: 03 Jul 2010

Hi, I'm writing some similar code that I'm going to use for data logging.  Right now trying to get a microSD 2GB HC card to work, I'm wondering if you have had any luck.


My 4GB High-Capacity microSD card (Patriot) does not work with Simon's code, however it does work with my code.  I'm not sure what the differences in our codes are.

I'm working on a data-logging class for my aerospace application, so I'm writing my own code.  I would like to use the FAT system but have a few questions that I cannot find answers to.  If there's any interest in seeing my code, just ask.


I believe I have found the portability issue.  The disk_write doesn't seem to account for addressing differences in the two cards.  In version 1 we must multiply the block number by the block size (512 bytes).  In version 2, block size is fixed to 512 bytes and the address changes to simply the block number.

21 Dec 2010


is there a pull-up needed?



13 Jan 2011


Does this code also work with micro SD HC?

15 Jan 2011

this code does



I really don't mean to step in toes, but the SDCardTest did not work for my 4GB Patriot Micro SD HC card.

I hope this is helpful to you, please let me know if you have any problems with it.

13 Apr 2011 . Edited: 13 Apr 2011
Hello, I've just tried the latest code of SDCardTest with my FUJI 2GB micro SD ( and I got the following message: Hello World! No disk, or could not put SD card in to SPI idle state As I'm using the RS-EDP adapter module to connect the micro SD card to the LPC1768, I had to use some other pins (p11, p12, p13, p14). That's the only thing I changed. I also tried the CM-MBED_RS_EDP_SDCARD_Write_Example from the cookbook and I got the same message. Does anyone know why this card is not working properly? Regards.
27 May 2011 . Edited: 27 May 2011

Javier, I have same problem. I tested on the kingston 512 MB SD Card, and on the 64 MB SD Card and doesn't work. Are you fix the problem?

Please log in to post a comment.