Categories

Writing to an SD card

The ability to read and write to SD cards is a fairly common scenario that you may encounter, there are a couple of ways you can do this by adding an SD card breakout and connecting it to your Arduino or purchasing a shield which has this capabilities. We will look at the shield option here , the shield I purchased was titled the Data logging shield and was manufactured by Keyes, there seems to be a few similar shields that can be bought. Here is a picture of the shield I used

data logging shield

data logging shield

In the example we will show we write some text to the SD card , you can then remove the SD card from your arduino data logging shield and then you can then open up the file in your operating system by inserting the card. You’ll see one line for each time the sketch has run.

Here are some general things to consider regarding writing to SD cards

You can have multiple files open at a time, and you can also write to them as well. Just remember and close them all.
You can use print and println() to write strings, variables for example. Its like outputting via Serial
You must close() the file(s) when you’re finished to make sure all the data is written to the file(s).
You can open files in a directory. If you wanted to open a file in a directory such as /temp/latesttemp.txt you simply do the following SD.open(“/temp/latesttemp.txt”).

The following sketch is a basic demo of writing to a file, it uses the built in SD library. This is a common scenario in datalogging type applications

 

Code

 

#include <SPI.h>
#include <SD.h>
 
 
File myFile;
 
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
 
if (!SD.begin(10)) 
{
Serial.println("initialization failed!");
return;
}
Serial.println("initialization complete.");
 
//open the file.
myFile = SD.open("test.txt", FILE_WRITE);
 
//write to file
if (myFile) 
{
Serial.print("Writing to test.txt...");
myFile.println("TESTING : 1,2,3,4,5");
//close the file
myFile.close();
Serial.println("done.");
} 
else 
{
// if the file didn't open, print an error:
Serial.println("error opening test.txt file");
}
}
 
void loop()
{
 
}

Result

If you take the SD card to your PC and find the test.txt file you should see the file and if you open the file you will see something like the following. The amount of lines will vary on how many times the sketch has been run

TESTING : 1,2,3,4,5
TESTING : 1,2,3,4,5
TESTING : 1,2,3,4,5

Links
Data Collection Logger Module Recorder Logging Shield for Arduino UNO SD Card

Share

Temperature logger

In this example we will add a DS18B20 temperature sensor and an SD card. We will then log the results of the sensor to a file on the SD Card called temp.txt.

 

Layout

arduino and sdcard and ds18b20 layout

arduino and sdcard and ds18b20 layout

Code

You need to download 2 libraries and copy them into your Arduino libraries folder.

“DallasTemperature” Library
“1-Wire” Library

#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define DS18B20 7

const int chipSelect = 4;
OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);

void setup()
{

Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect.
}
Serial.print(“Initializing SD card…”);
pinMode(10, OUTPUT);

//init SD card
if (!SD.begin(chipSelect))
{
Serial.println(“Card failed, or not present”);
return;
}
Serial.println(“card initialized.”);
sensors.begin();
}

void loop()
{

sensors.requestTemperatures();
// open the file.
File dataFile = SD.open(“temp.txt”, FILE_WRITE);

// if the file is available, write to it:
if (dataFile)
{
dataFile.println(sensors.getTempCByIndex(0));
dataFile.close();
}
// if the file isn’t open
else
{
Serial.println(“error opening temp.txt”);
}
}

Results

Run the example, then remove the SD Card from your SD Card breakout, connect it to your PC and open the temp.txt and look at the contents

Here is some of the entries in my file

28.19
28.44
28.44
28.69
28.69
28.69
28.87
28.87
29.12
29.12
29.25

Links

sd card breakout at Amazon

sd card breakout on Amazon UK

Share