Getting an SD card to work on my Arduino Uno (I changed boards with my father – Thanks! 🙂 ), was an interesting endeavour during the last days.
I was aware that getting an SD card to work with an Arduino can be a funny experiment. See for example: http://www.ladyada.net/products/microsd/, which is a great tutorial getting started and set up the wiring, but already details some issues, which generally arise around using SD cards with Arduino. As a side note: in parallel I watched the great Arduino video tutorials on http://www.jeremyblum.com/ . Gr8 stuff!
What I tried and didn´t help…..
Each of the following items may have helped, but I´m too lazy to try if taking these preconditions away would break the system:
- Different SD cards, in particular exchanging SDHC card by a non-high speed SD card.
Meanwhile I can report that also SDHC card (<= 2GB) seems to work
- Providing external power supply instead of powering from USB only
- Trying different libraries and ways to work with the SD card
Only one of these libraries really worked, see below
What helped – Watch out for memory overflows
Some time during all the testing it occurred to me that all attempts and failures felt very arbitrary.
Short version: I had buffer / heap overflows, because the libraries I first used require lots of memory before even using them during runtime. In particular after initializing the SD card free heap memory partially went down to 200 Bytes. Add to that some function calls increasing the consumed stack size (used for function paramters and local variables) plus lots of nested calls: voila, you get arbitrary behaviour and sometimes the Arduino even resets, for example because the stack pointer (SP) wanders into the used heap.
I had set up a small console program for the arduino such that I could work from the Arduino PC Serial Monitor with commands like, create file, delete file, dir/ls (directory listing etc).
Using this arduino program I can analyze memory consumption and memory leaks induced by the different file operations. In particular ls/dir proved to be a pain in the …memory ;), because of the traversal of directories and files, which by some libs or examples is even done by a recursive algorithm.
Tools for Arduino memory analysis during runtime
The Fat16 library includes a function FreeRam() could maybe have helped, but I found this only after all the other fuzz described above.
The following two references and coding very very helpful to show the current values of SP (StackPointer) and free/allocated heap memory during runtime of my Arduino sketch.
http://playground.arduino.cc/Code/AvailableMemory and http://andybrown.me.uk/wk/2011/01/01/debugging-avr-dynamic-memory-allocation/
Finally my arduino program now works using the Arduino Fat16 library on the root folder only of the SD card and this works stable at least in short time tests and according to my memory usage analysis. As my use case does not include long-term tracking and not lots of files are involved I should be fine with that, but will keep an eye on memory – which anyway is a good idea on a 2K RAM microcontroller 🙂
Information and coding sources
Future Art ?
I started to read some things about Arduino boot loaders capable to load and start programs from SD card. In combination with approaches for Arduino development environments on Android, I could potentially create sketches on my tablet and directly download them to an arduino (so far no such option seems to exist….).