Reading CSV files from an SD Card

Writing CSV files to an SD card is a fairly easy matter, build a string, adding a comma between each number and send that string to the SD card. However, getting that data off the card and loaded back into arrays to use is not such an easy matter.

My application read temperature and pressure each hour, storing the results in an array. Three days worth of data is stored on the Arduino to allow the data to be plotted on three graphs. To do this I needed two arrays each 3×25, ie SDtemperature[3][25] and SDpressure[3][25]. This data is stored once an hour as a CSV file that can be opened in a spreadsheet such as Excel. What I wanted was to replace the data held in strings if the power was lost, however, I did not have a lot of space available on the Arduino.

I spent several hours on the internet looking for for an answer and eventually found the method shown below. This sketch has been modified to suit my requirements and the original can be found here on the Arduino Forum. To save dynamic memory I reuse most of my variables and unlike the original sketch I did not want to print the data out, but to save it in the data arrays.

My sketch checks for the presence of the data file in the setup loop and if it exists loads the data into the arrays. Each hour the file is deleted and replaced by the latest readings, so unlike the main data file which grows each hour the backup file remains small. The only problem I found was that decimal numbers needed to be multiplied by 100, then divided by 100 when read back in.

Using this method allows saved data to be automatically restored in the event of power loss.

You can see this method being used in my blog for the Joystick Weather Clock here.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s