Interfacing Arduino to micro SD Memory
I’m working on an Arduino project that requires oodles of storage. For no reason in particular, I had planned to use a micro SD memory card. I found out quickly that there were many hardware choices that seemed to suit my needs. I chose the Sparkfun Breakout Board for DOSonCHIP FAT16 FAT32 uSD Module because (1) it looked easy to implement and (2) it had a real time clock built in. Supposedly, this product “makes it easy to add up to 32GB of storage to your project without adding software complexity”. The example cited on the product web page describes the programming simplicity: “Datalogging is now as easy as sending ‘md CHAT’ to make a directory!”
After reading various blogs and Arduino forum posts, I understood that even a simplified FAT implementation for the micro SD card could consume most of the free memory on the Arduino. Since the DOSonChip promised to handle all of the FAT issues and more, it seemed like a good choice.
I wired the module to my Arduino and quickly established communication through the device’s UART interface. However I did not get the expected response from the DOSonCHIP module. The folks at WEARABLE INC., the makers of the DOSonCHIPTM CD17B10 were quick to respond to my email inquiries. Sadly, they’ve made big changes to the device’s command structure. They now utilize a packet-based protocol. Matt, the tech support guy who responded to my emails, did give me the instructions to revert back to the 1.02 version firmware (not to be confused with the version 1.02 API), but my RS232 to 5V converter only has TX and RX. I need CTS and RTS to upload the 1.02 firmware.
I was stuck for a week or so as I pondered my options. (a) buy or build a four wire RS232 level shifter; (b) convert the DOSonCHIPTM sample code to work with the Arduino – not very appealing as I haven’t programmed a big project in C for 10 years (d) use Ladyada’s FAT implementation (e) what is this uFAT thing being discussed over at http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1206874649/84#84 ?
A discussion about the pros and cons of these choices could fill an entire blog post. However, with my particular combination of resources and limitations I chose to try out uFAT – uFAT2 actually.
I have the Sparkfun breakout board for the micro SD flash card. uFAt2 and many of the other flash card libraries were written for SD or MMC memory. They all communicate to the microcontroller via the SPI bus. Not all micro SD memory cards support SPI mode communications (see http://en.wikipedia.org/wiki/MicroSD ). The Transcend micro SD card that I purchased supports SPI communication, so it seemed that I had a good chance of getting it all working.
I wired the two devices together like this:
When using SPi communications on the Arduino, SPI bus pins are predefined. MOSI, MISO, and SCK can only be Port B pins 3, 4 and 5, as described above, the Chip Select line, however, has no such restrictions. Each device on the SPI bus requires its own chip select. Here’s more info about SPI and Atmel.
I used a voltage divider network and resistor values similar to Ladyada’s circuit in her GPS Shield. It wasn’t clear to me as to why she chose to use the Zener diode on the CS line. I did not include it and my circuit works. Assuming a 5V output from the Arduino, we can use the voltage divider rule to calculate that the micro SD will see 3.4v as a logic high input.
Next I downloaded and installed the uFAT2 library provided by Arduino Nut. I inserted the micro SD card and powered up the circuit. uFAT2 comes with a demo sketch that helps to explain the operation of the uFAT library. I compiled the demo, which is called uFAT_example.pde, by the way.
I uploaded the sketch to the Arduino and selected the Serial Monitor. I got this message:
Success? Maybe. I needed to copy a file named data.bin to the micro SD so that I could see uFAT do its thing. I selected a jpeg image of the state of RI. It’s a 4MB file, so I guess it renders the state at nearly full size. I put my memory card into my AtivaTM Memory Card USB Drive, microSDTM (Office Depot, 10 bucks) and inserted it into one of my computer’s USB ports. I copied the file and renamed it to data.bin, this time the results were:
It is working! Eventually I’ll need to log csv files, so the DevicePrint library, also from ArduinoNut, is also of interest to me. I downloaded and installed the DevicePrint library. It comes with an example program. In order to see it run, I renamed the file on my memory card to data.txt. I ran the sketch got this message:
About 2.5 hours later, the sketch completed writing to my file. Memo to self: this is a good time to use debug code. When the program finishes it displays the message below:
So in my opinion uFAT2 is a great library. It will work with micro SD memory IF the memory uses the optional SPI communication mode. uFAT2 is a great optimization for the competing interests of simplicity, ease of use and memory footprint. Furthermore the DevicePrint library increases the utility of the uFAT2 library.
If you are new to Arduino or flash memory operation, as I am, you may find this daunting. My advice: dig in, follow along, ask for help and you’ll soon be logging data!