Arduino Uno writing garbage on SD card

Hi all,

I need a little help with simple SD card data logging using arduino uno(NO ethernet shield attached). I am using an SD card module and a 2GB SD card formatted using FAT32 file system. The problem is that it is writing garbage on SD card whenever I open the SPI pin (4) for writing a test.txt file.
pin configuration is as follows:
MOSI : 11
MISO: 12
SCK: 13
chipselect: 4

Here's the test code that I am running: https://www.arduino.cc/en/Tutorial/ReadWrite

I have to supply 5v and 3.3v (not enough current can be drawn out from one pin on arduino) both from arduino to SD card shield for it to even initialize the SD card, otherwise the initialization fails.
I'd greatly appreciate if you can help me identify the problem.
Thanks a lot!

I think you should show us your circuit and provide a link to the SD card module that you're using, or at least a photo of it.
Does the module have on-board level converters, or are you using external ones?

Your current problem is likely due to your SD shield/module or a wiring error so the information OldSteve requested is key.

To avoid future problems you should reformat your SD card. 2GB cards were not designed to be formatted FAT32. The SD association specifies a unique format layout for each SD card that matches the internal configuration of the card.

The only way to insure you get the correct format is to use SD Formatter.

The format is not critical for devices with lots of RAM buffering but the Arduino SD library has only a single 512 byte cache buffer so you should use the standard formatter.

Thank you for your responses. One small correction regarding the size of the sd card. It's a 16 gb card on which I've created a 2gb partition.

Here are the pictures of the module, the arduino's digital pins in use and a picture depicting 5v and 3.3v supply to sd card module going from arduino.

And I'm not sure about the level shifters/converts. Can you find that out by looking at the pictures?
I can try out formatting using the tool that you gave a link to. is it causing problem?

I haven't worked a lot on Arduinos but know a bit about electronics, any help will be greatly appreciated.

umarshehzad:
Thank you for your responses. One small correction regarding the size of the sd card. It's a 16 gb card on which I've created a 2gb partition.

Here are the pictures of the module, the arduino's digital pins in use and a picture depicting 5v and 3.3v supply to sd card module going from arduino.

SD card module with Arduino uno - Album on Imgur

And I'm not sure about the level shifters/converts. Can you find that out by looking at the pictures?
I can try out formatting using the tool that you gave a link to. is it causing problem?

I haven't worked a lot on Arduinos but know a bit about electronics, any help will be greatly appreciated.

That type of module, (identical to mine), doesn't have on-board level shifters, so you need to use external ones.
Also, don't connect 3.3V to the module's 3.3V pin. The SD card module has an onboard 3.3V regulator, and you're connecting 3.3V to it's output. Not a good thing.

I used a CD4050 hex buffer for level conversion on the MOSI, SCK and CS lines, with the CD4050 powered by the 3.3V from the SD card module. I connected the MISO line straight from the UNO to the module. I connected 5V from the UNO to the module's 5V pin.
Someone said yesterday that I should use a 74HC4050, but when I looked at the CD4050 datasheet this seems well within it's specs, so I don't intend to change. You might want to use a 74HC4050 though. Up to you.
(You can see more details on this here:- Using a CD4050 )
N.B. A 74HC4050 is pin-compatible with the CD4050, so you can use the same schematic for either. I didn't show capacitors on the schematic, either, but it's a good idea to add one for the CD4050 supply.

This is my schematic:-
SD Card Circuit.JPG

And here's a photo:-
(Right-click then "View Image" (Firefox) or similar for a full-sized view.)

I should add, this setup works fine and has never missed a beat. I've had another module running 24 hours per day for some time with these connections and it's never had a problem. Reads and writes perfectly.

Edit: It's possible that you've already damaged the SD card module with your connections. I hope you bought a spare. I always buy at least two of items like this, just in case.

And don't forget to connect the CD4050's unused inputs to ground. CMOS inputs don't like to be left floating. :slight_smile:

Thanks for the elaborate explanation. I can try it out on Monday after getting my hands on the hex buffer you suggested. Is there another workaround that I can try in the meanwhile? Otherwise I'll have to wait for two days. Guess I'm getting impatient but just asking if there's a possibility.

Thanks anyway, you rock!

umarshehzad:
Thanks for the elaborate explanation. I can try it out on Monday after getting my hands on the hex buffer you suggested. Is there another workaround that I can try in the meanwhile? Otherwise I'll have to wait for two days. Guess I'm getting impatient but just asking if there's a possibility.

Thanks anyway, you rock!

Well I guess you could try a pair of NPN small-signal transistors, both in commom-emitter configuration, with the second left as an open-collector for MOSI and SCK. (One pair for each.) Nothing is needed for MISO.
CS would need the same, but you'd need to use a 3.3V pullup resistor on the second transistor where it connects to the module's CS line because according to the schematic of the SD card module, there's no on-board pullup resistor on CS.
It's a lot of messing around, and would need 6 transistors and 7 resistors. If I were you, I'd wait until Monday.

This is what I mean:-
Non-inverting level shift.JPG

And this is the schematic that I found for the SD card modules:-

Disclaimer: I'm not 100% sure that this is the exact schematic for these modules, but it does appear to be right.
And, of course, this is untested.

Edit: Also, you could possibly use a voltage divider for the CS line, I guess, but not for the others.

Someone said yesterday that I should use a 74HC4050, but when I looked at the CD4050 datasheet this seems well within it's specs, so I don't intend to change.

I think the problem is that early CD4050 parts had classic ESD protection with a diode to Vcc. The input voltage is limited to Vcc + 0.5. For Vcc of 3.3V the allowed input voltage is too low for a 5V system.

These may appear to work OK by either dragging down input signals and/or raising 3.3V somewhat. The signals are reduced by at least some.

Modern CD4050B parts have ESD without the diode to Vcc. Some old style parts and parts in the LV family still are around. See this.

The 74HC4050 always works.

he 74HC4050 provides six non-inverting buffers with a modified input protection structure, which has no diode connected to Vcc. Input voltages of up to 15 V may therefore be used.

fat16lib:
I think the problem is that early CD4050 parts had classic ESD protection with a diode to Vcc. The input voltage is limited to Vcc + 0.5. For Vcc of 3.3V the allowed input voltage is too low for a 5V system.

These may appear to work OK by either dragging down input signals and/or raising 3.3V somewhat. The signals are reduced by at least some.

Modern CD4050B parts have ESD without the diode to Vcc. Some old style parts and parts in the LV family still are around. See this.

The 74HC4050 always works.

Ah, right, that explains it. But are there many early CD4050s still around?
The ones I'm using are fairly recent, CD4050B, bought from a large dealer, (RS Components). They have a large turnover, so are unlikely to be selling early CD4050 versions of this chip. The datasheet says 15V max on the inputs, and also says that the input voltage can exceed the supply voltage. On top of that, in that same datasheet the internal schematic for the CD4050 shows a single input protection diode from ground to the input with a reverse-breakdown voltage of 30V.
Still something to beware of though.

In the other thread, I asked pito to elaborate, but he didn't seem to want to. I also asked for links to threads discussing why CD4050s shouldn't be used, but haven't had a reply yet. I guessed it must have been related to their current sink/source capabilities.

The other option besides 74HC4050 and CD4050B would be CD4504B level converters. They're a dual-supply CMOS chip designed for the purpose.

So, @umarshehzad, now that I'm better informed on this, if you use CD4050 chips for this, make sure they're the "B" version, (CD4050B), and not the earlier CD4050 (no "B").

And @fat16lib, thank you for filling me in on this. :slight_smile:
(From now on I'll make sure I say CD4050B, not just CD4050.)

This is from the CD4050B datasheet:-
CD4050B input protection.JPG

Price aside, the advantage of CD4050B chips is that they're much easier to get hold of. Not so many suppliers sell 74HC4050s.
For example, here in Australia neither of our most common suppliers, Altronics or Jaycar, sell 74HC4050 but both sell CD4050B chips, or at least they did the last time I looked.

OldSteve:
Ah, right, that explains it. But are there many early CD4050s still around?
The ones I'm using are fairly recent, CD4050B, bought from a large dealer, (RS Components). They have a large turnover, so are unlikely to be selling early CD4050 versions of this chip. The datasheet says 15V max on the inputs, and also says that the input voltage can exceed the supply voltage. On top of that, in that same datasheet the internal schematic for the CD4050 shows a single input protection diode from ground to the input with a reverse-breakdown voltage of 30V.
Still something to beware of though.

In the other thread, I asked pito to elaborate, but he didn't seem to want to. I also asked for links to threads discussing why CD4050s shouldn't be used, but haven't had a reply yet. I guessed it must have been related to their current sink/source capabilities.

The other option besides 74HC4050 and CD4050B would be CD4504B level converters. They're a dual-supply CMOS chip designed for the purpose.

So, @umarshehzad, now that I'm better informed on this, if you use CD4050 chips for this, make sure they're the "B" version, (CD4050B), and not the earlier CD4050 (no "B").

And @fat16lib, thank you for filling me in on this. :slight_smile:
(From now on I'll make sure I say CD4050B, not just CD4050.)

This is from the CD4050B datasheet:-
CD4050B input protection.JPG

Price aside, the advantage of CD4050B chips is that they're much easier to get hold of. Not so many suppliers sell 74HC4050s.
For example, here in Australia neither of our most common suppliers, Altronics or Jaycar, sell 74HC4050 but both sell CD4050B chips, or at least they did the last time I looked.

Hi,

I've used a CD4050BE on MOSI SCK and CS pins and it has worked like a charm. You're a life saver.
Thank you very much! :slight_smile:

umarshehzad:
Hi,I've used a CD4050BE on MOSI SCK and CS pins and it has worked like a charm. You're a life saver.
Thank you very much! :slight_smile:

No problem. Glad to help.

To avoid having to mess around connecting up a CD4050B every time I want to do some prototyping with one of these SD card modules, I made up a little carrier PCB with an onboard CD4050BE. It's much faster to get set up each time now, takes seconds.

SD Card Board.JPG

What capacitors did you use?

OldSteve:
That type of module, (identical to mine), doesn't have on-board level shifters, so you need to use external ones.

I used a CD4050 hex buffer for level conversion on the MOSI, SCK and CS lines, with the CD4050 powered by the 3.3V from the SD card module. I connected the MISO line straight from the UNO to the module. I connected 5V from the UNO to the module's 5V pin.

Why do we need level shifters?