SD card read/write with Arduino

Hi

Reading from SD is a solved problem. The discussion happening here was to do with a specific problem sketch.

Charlie

But does it read/write FAT? Will it be added to the official Arduino library page?

Hi

Kind-of, and no. The library is intended for tight memory situations where you are willing to accept limitations in usage. uFat allows you to find the starting sector and length of a file on a fat12 or fat16 formatted device.

You can then read data from and write data to those sectors. It doesn't understand the filing system more than it needs to, which in this case is only reading the boot sector, partition table and directory.

In most cases this has been quite enough for me!

Charlie

A few posts ago bobemoe posted code to use the Arduino library print routines to output strings to an mmc card. I've spoken with him and used his code as a basis for DevicePrint. It has been modified to fit my usual criteria - small & tight with few (if any) dependencies.

Get it herehttp://arduinonut.blogspot.com/2009/02/libraries.html along with some of my usual written drivel about whatever and stuff.

Commentary and questioning welcomed as usual. Enjoy.

C

wow,that looks very interestering, gonna have to finish soldering my sd card thingy ;0)

Anyway - is there a chance you could also write a function for reading stuff from an uFat File?

Hi again everyone!

After numerous emails, pulling of hair & re-writes, sirmorris has helped to get my SD card logging dreams up & running. You can get the 'final' sketch here: http://docs.google.com/Doc?id=dqhc6fg_0gmk96kdd. It's certainly not the prettiest sketch, and I can't claim much of it at all as my own work, but it does what I hoped it would (although real-world testing hasn't happened yet).

Here's a photo of my current test setup...


Needs a LOT of work before it's ready for final implementation!! ;D

Please feel free to suggest code improvements/rip my sketch to shreds - I'm no programmer, I've got thick skin, and any help is greatly appreciated!!

One last thanks to sirmorris & bobemoe for their work here - it's amazing how generous you've been with your help & knowledge.

I'll pop a note in here when I get something up in the exhibition area.

JB.

Hi Sirmorris.. unfortunately i dont have much luck compiling your code - i am using 011 of the IDE with a 328 Chip..

Errors are:

In file included from C:\Documents and Settings\Administrator\Desktop\arduino-m328-win_current\arduino-0011-m328\hardware\cores\arduino/WProgram.h:6,
                 from DevicePrint.cpp:1:
c:/documents and settings/administrator/desktop/arduino-m328-win_current/arduino-0011-m328/hardware/tools/avr/lib/gcc/../../avr/include/avr/signal.h:36:2: warning: #warning "This header file is obsolete.  Use <avr/interrupt.h>."
In file included from DevicePrint.cpp:3:
/DevicePrint.h:20: error: expected class-name before '{' token
23: error: microfat2.h: No such file or directory In function 'uint8_t proxyWriter(const uint8_t*, long unsigned int, uint8_t)':
 In function 'void setup()':

As it stands the libraries don't compile under 11 - the Print functionality hasn't been abstracted from the serial library in this version.

You'll need to upgrade to version 0013. http://arduino.cc/en/Main/Software.

It also looks from this error that the mmc, microfat and deviceprint folders haven't been copied to the folder

arduino-00XY\hardware\libraries

which is where they should live.

I hope the upgrade won't cause you too many problems, but there are issues with some sketches - the libraries changed significantly during the 11-12 transition. You can keep both versions side-by-side though, so if you have a problem sketch just compile it under 0011. For all new work, start out in the 0013 IDE.

If you do need to port code then the rule seems to be - if it's an .h file then add

#include <WConstants.h>

and if it's a cpp file, add

#include <WProgram.h>

Good luck. If you need any help just yell.

C

well... i thought it had to do with 0011.. alright.. so i upgraded.. lets see if it works now ;0)

So far it compiles and runs - did i miss something or do you use the same wiring as on the first post shown? Either that has changed or my Card reader is not soldered properly ><

Just for the record - everything is working fine with 0012. Will upgrade to 0013 soon to keep up with the community!

Hey guys, I've been following this very helpful thread but haven't made much progress. I've figured out it's not detecting the card is in since I get STA_NOINIT and STA_NODISK from the MMC init function. I'm using the 012 arduino software.

I've measured the pins with a multimeter and I suspect one of the ground pins isn't connected quite right. So I was wondering how you guys have it hooked up? I bought just the SD card holder and stuck a breakaway thing with the side with the long pins going in to the holder and wires soldered to the short pin side as shown below. I'm using a seeduino which has a 3v3 switch and it's hooked according to the diagram in the OP.

edit>oh right and the question: how do you guys have your card holder hooked up? Is there a better way of doing it? I could have gotten the card holder with a pcb soldered on from sparkfun but it was $15 but I want to use it in a final pcb design too. If nothing works I guess I'll have to suck it up and buy that sd holder board.

Any help would be great!
Thanks!

Hi

I always reach for this diagram:

SI on the diagram = arduino MOSI
SO on the diagram = arduino MISO

I always ground pin6 even though it says NoConnect.

So wire it like so:

sdCARD    ARDuino
1         10
2         11
3         gnd
4         +3v3
5         13
6         gnd
7         12

It looks like a great harness you've got there but I suspect that the little legs aren't making a good contact with the socket. Try making a plug-in board - you'll use it over and over.

When it comes time to attach it to protoboard I usually bend the little pins down a bit and use them to locate to the card solidly. I tack them, then solder the tabs at the sides to lock the holder in place. As you might expect really.

Check out some of the photos on a few of the posts on my blog, especially Arduino Nut: Lash-up! and Arduino Nut: Programmer Redux

Test your wiring by writing a sketch to make all of the signal lines high, measure them (without a card in place!!) and then make them low, measure again. Test for shorts.

I hope there's something here that helps.

Charlie

Thank you, that sure was helpful, but my first thought that it might be really damn simple to connect a card to the arduino is more or less destroyed. If i look at your pics, especially those close-ups of your shield makes me realise that i need more than a few pins and cables to connect the card to the arduino.. do you have a schemata of what is connected what way to what else? I ask becuase this looks much different to what i have seen in the first post of this thread... and i am quite confused now ><

Alright, after builidng it like done on the first post but using the same pins as in your post i was finally able to get to the point that the Serial window says: Done!

Unfortunately 'Done' is not exactly anything.. although it finds the file (or not) nothing is written into it ><

SDCard is formatted in FAT with 2GB Space - everything that is on it is just the 0byte data.txt

edit sectorsavailable() return 0
edit2 you may chose to ignore the whole post.. somehow it seems my SD card is done with the world ;0) - it cant be written with anything ><

I had to 'fill up' my data.csv file with junk data so that it was bigger than 512 bytes before it would write anything. One of the sketches which was presented in this post recommended at least 15kb file size; having a data.txt file of 0 bytes would lead to sectorsavailable() returning 0 & there being not enough space in the file to write any data. As I understand it, you will only be able to write data to the file up until the original file size; more data will NOT increase the file size as required, it will just fail to write to the file. (I think - sirmorris will correct me if this is wrong! :))

Try making your file larger... fill it with anything you like as it will be overwritten anyway. You could even take any other old .txt file & rename it if you wanted, saves the hassle of making stuff up!

Hope this helps,

JB.

wow, thats been exactly it :0)

Finally i can save stuff :0)))

Hooray - I've been able to give something BACK to the forums instead of just using everyone else's knowledge!!

Glad it's working for you!!

JB

if you just want to log data (and not necessarily read it) i have an sdlogging library that saves to fat16 formatted SD cards. i use it to log GPS & sensor data: GPS datalogging shield for Arduino

Is anybody using this library? It overcomes some of the limitations of the libraries everyone is discussing here.

I came across this one too, which supports fat32 and sdhc: Google Code Archive - Long-term storage for Google Code Project Hosting.

I have played arround with LadyAdas code for a while, but as i was not able to set up the wiring right for it i could not really test it.. but what i noticed was quite a big overhead.. if i remember right it took quite a bunch of space on the arduino.. yet it seemed really good from its limitation/options.

Is the wiring not the same as the others? I haven't taken time to examine the schematic yet.