Interfacing SD card's to the Arduino and stability - a few tips.....

I wanted to interface the arduino to a SD card. My method was to solder directly the wires to a micro sd connector for which any microsd card can be plugged. However i found that it was highly unstable with frequent SD errors. I corrected it by adding a 100uf and 0.1uf supply decoupling capacitors across pin's 3 and 4 of the sd connector. Now everything is stable at the maximum 8Mhz SPI bus speed using resistor voltage shifters. Even the SD card manufacturers recommend using capacitors, however few people use this which i think is the cause for many people commenting about unstable SD's. Keeping the wires short also helps as long wires act as stray capacitors ruining stability.

You can read more information about this at my blog post at http://kalum.posterous.com/sd-cards-and-arduino
I hope this information will help people trying to interface SD card's to the arduino.

thanks kalem, I have been having some fun and games with an intermittent SD card, I halved its speed to solve my problem but I will I put another 100nF across 3&4 of the holder to make sure.

Caps often help and short wires are important. Even with these precautions many people have problems with resistor level shifters.

I have done a study using cards with resistor level shifters. I find that most older cards work at 8 MHz and almost all older cards work at half speed, 4 MHz.

Many newer cards fail at all speeds with resistor level shifters. These cards expect very fast SPI signal rise times. The SD spec requires a rise time of less than 10 ns for all clock speeds under 25 MHz. It is just not possible to get sharp edges at 8 MHz into the typical capacitance with resistor level shifters.

New cards appear to use edge detectors. New cards expect 5 ns rise times for 25 - 50 MHz clock.

Hello.
Can you or someone else post a scheme with 100uf and 0.1uf capacitors included?

Great article.

littleclown:
Can you or someone else post a scheme with 100uf and 0.1uf capacitors included?

Put them (at least the 0.1uF) between VCC & ground (pins 3&4 of the SD card). If you also use the 100uF be sure to check the polarity right. (short leg is ground, pin 3)

Thanks. Will try.

fat16lib:
Caps often help and short wires are important. Even with these precautions many people have problems with resistor level shifters.

I have done a study using cards with resistor level shifters. I find that most older cards work at 8 MHz and almost all older cards work at half speed, 4 MHz.

Many newer cards fail at all speeds with resistor level shifters. These cards expect very fast SPI signal rise times. The SD spec requires a rise time of less than 10 ns for all clock speeds under 25 MHz. It is just not possible to get sharp edges at 8 MHz into the typical capacitance with resistor level shifters.

New cards appear to use edge detectors. New cards expect 5 ns rise times for 25 - 50 MHz clock.

Actually I am running the SPI bus at 8mhz (the maximum possible with a 16mhz AVR) using the above mentioned resistor based divider and it works ok. I am using the new TwinMOS SDHC 4GB micro SD card and everything works fine. The SdFAT library benchmark test gives me 150K/sec write speed and 260k/sec read speed. I tried a Sandisk 2gb card as well and it worked. I will be willing to test any new brand which has edge detectors to see if i can get them here.

Additional i would recommend that when buying SD cards for micro controllers to go for reputed names like TwinMOS, Sandisk, Kingston etc rather than cheap no-name brands.....SPI has to be implemented obligatorily for all SD cards but some no-name brands have rudimentary SPI support which is very slow and not optimised at all

I know that many SD cards work or appear to work with with resistor dividers but they are not as reliable as with IC level shifters and often users don't realize they have data errors.

There is no CRC with SD cards on the Arduino so undetected errors are not uncommon.

I have written three SD card libraries for the Arduino. SdFat, which is the basis for the "native SD library", Fat16, which is similar to SdFat with a smaller memory footprint, and WaveHC, which is a library optimized for playing audio files.

These libraries have been downloaded over 25,000 times and problems with resistor based level shifters are one of the top problems people contact me about.

There is no guarantee that a given brand will have a good SPI controller. In fact class 10 cards from top manufactures often have poor SPI performance since they are optimized for the 4-bit SD bus and are not expected to be used on the SPI bus.

Manufactures revised cards often without changing model numbers. Performance of cards with the same model number may vary a great deal on the SPI bus.

Once again it is not possible to achieve the specified rise times requirements for SD cards with resistor level shifters driving the capacitance required by the spec. Look at the signals on a good scope with a low capacitance probe.

This is why Adafruit replaced resistor level shifters with ICs in it's Wave shield and GPS shield and uses ICs in newer products.

Thanks for the insights again!

However, I can't seem to find the "real" solution for 5V to 3.3V interfacing. A lot has been posted, but it all seems a bit arbitrary and certainly not very simple to me. Apart from ready-made kits that (try to) solve this problem, do you have any ready and easy to use solutions or interesting reads or background information about this level shifting inconvenience?

Many thanks in advance!

Solution is to use a real chip to do it. Something like this perhaps

Sparkfun had an SD shield with a chip like this (or maybe Motorola version), I'm not seeing it online now tho.

A 74AHC125N is a DIP and is used in Adafruit kits. Here is the Data Logger Shield schematic http://www.ladyada.net/images/logshield/logschemv1.png.

I buy them here http://search.digikey.com/scripts/DkSearch/dksus.dll?lang=en&site=us&vendor=0&WT.z_cid=ref_findchips0311_dkc_buynow&mpart=SN74AHC125N. They will ship First Class postage which is very low cost.

I have also used the 74HC4050N FAT32/SDHC library for Wave and other shields - adafruit industries

SparkFun has uses a version of the 4050 hex level shifter in their microSD shield http://www.sparkfun.com/datasheets/DevTools/Arduino/microSD_Shield-v13%20Schematic.pdf. See Findchips: HC4050 Price and Stock

Gravitech sells SD and MicroSD Card Adapters based on the 74LCX245 http://site.gravitech.us/MicroResearch/Others/SD-ADP/SD_ADP_Schematic.pdf.

Will definitely look into that. Thanks a lot!

Ooh, nice fast switching times on the 74ahc125!

With the tristate output, it could be used as a level shifter by tie-ing the input low, put a pullup to 3.3V on the output, and enable the input to bring the output low, disable the input to let it go to 3.3V for the SD card high input.

If running from 3.3V supply, the inputs from the arduino should be limited to 3.3V as well, and not allowed to go to 5V which the sparkfun design seems to do.

CrossRoads:
...
With the tristate output, it could be used as a level shifter by tie-ing the input low, put a pullup to 3.3V on the output, and enable the input to bring the output low, disable the input to let it go to 3.3V for the SD card high input.

But a passive pull-up is not as efficient as using driven outputs. (A fairly low value of pullup would be required to absolutely guarantee timing tolerances are met, and often we don't have good figures for capacitance of the circuits being driven.) If you are going to use passive pull-ups, you might as well use the hokey resistive voltage dividers that some people use.

No, they just tie the enable low (so the output is always enabled) and they feed the signals through from A-Y. They operate the 'AHC125 at Vcc = 3.3 Volts, and take advantage of the fact that the 74AHC125 inputs are 5-Volt tolerant. Note that many chips, including 74HC125 (no 'A' in the middle of the part number) are not 5 Volt tolerant when they are operating at 3.3 Volts. For the 'AHC125 note that maximum input voltage is specified to be 5.5V, not the Vcc+0.5V (or, maybe, Vcc+1.5V) that is typical for 3.3 Volt devices that are not 5 Volt tolerant.

For my money, I can't understand why (oh, why) anyone would use the '125 as a level shifter (even though it's not strictly illegal) when the '4050 chips are explicitly designed for level-shifting and buffering operating conditions. (What happens when your purchasing agent, hoping to save a buck, orders 74HC125 instead of 74AHC125 parts? Stranger things than that have happened. Believe me.) The last time I bought a few, the '4050s were cheaper, too.

Oh, well...

Regards,

Dave

Dave,

Here is my theory about use of the 125. I modified an Adafruit kit using a 4050. The 4050 is in a 16 pin DIP with two NC pins. I got it to "fit" by soldering wires to the pins and letting it float upside down above the board. I convinced Limor at Adafruit that the mod solved SD reliability problems.

The 125 is in a 14 pin package and it just fits in the space where the resistor level shifter was so the rest of the board needs no mods. I suspect that was at least part of the reason. Only Limor knows why she used the 125.

It had to be a DIP since it is a kit for beginners. Only three level shifters are needed but I couldn't find a simple part like the 4050 with fewer than 16 pins. Do you know of one?

fat16lib:
Dave,

Here is my theory about use of the 125. I modified an Adafruit kit using a 4050. The 4050 is in a 16 pin DIP with two NC pins. I got it to "fit" by soldering wires to the pins and letting it float upside down above the board. I convinced Limor at Adafruit that the mod solved SD reliability problems.

The 125 is in a 14 pin package and it just fits in the space where the resistor level shifter was so the rest of the board needs no mods. I suspect that was at least part of the reason. Only Limor knows why she used the 125.

It had to be a DIP since it is a kit for beginners. Only three level shifters are needed but I couldn't find a simple part like the 4050 with fewer than 16 pins. Do you know of one?

Thank you for this most useful info. I will try to use the 4050 in my further designs and see how it turns out. The things is that I am in a poor third world country where certain digital ic's are hard to come by, hence my advocacy of resistor based shifters. I will order some 4050's from ebay which i can use for my future designs. I also think that keeping the wires short to a few inches (3-4 inches max) might improve the stability of resistor based shifters for ppl who need to use them. It's important to know that the SPI bus frequency is 8mhz and any extra length of wire can lead to stray cpacitance!

fat16lib:
...I got it to "fit" by soldering wires to the pins and letting it float upside down above the board...

The official technical term for that is the "dead bug" technique. I use a tiny spot of hot-melt glue to stabilize it. (Hot-melt glue is the electronics assembly equivalent of duct tape.)

fat16lib:
....simple part like the 4050 with fewer than 16 pins. Do you know of one?

I don't know of any.

Note that I don't actually object to the use of a 3.3 Volt part that is specified as "5 Volt tolerant," and I think that there is always a reason that people do things. I was just wondering...

Regards,

Dave