IAN LANG ELECTRONICS

Pictured on the left more-or-less life sized to the screen is the Adafruit Micro-SD breakout board which, as the name suggests, uses a Micro SD card to store data sent to it by a controlling source, in this case of course your Arduino board. It can read and write text files through the serial monitor on the IDE, or through software you write for the job, all controlled by your Arduino. But first things first; it doesn't come supplied with a card, so you'll need to acquire one from somewhere. It does come supplied with headers, and if you want to do breadboard evaluation you'll need to solder them in.

Logging Data to an SD Card

Go Back

There's a wee bit more legwork to do to get it up and running, such as downloading and installing  the library. Adafruit are very good and have provided full instructions on doing so, as well as wiring it in to your Arduino and some test code to make sure it's all up and running, and you can get it all here:

 

www.ladyada.net/products/microsd/#what_to_watch_for

This board is available to buy at Oomlout.com

So, when it's up and running, what can we do with it? Well, to test it out I decided to make a device that logs the temperature where it is once every minute. Here's the resultant text file for just over half an hour of testing this morning:

 

 

Temperature: 18      Time: 6:05   24/1/2012

Temperature: 18      Time: 6:06   24/1/2012

Temperature: 18      Time: 6:07   24/1/2012

Temperature: 18      Time: 6:08   24/1/2012

Temperature: 18      Time: 6:09   24/1/2012

Temperature: 18      Time: 6:10   24/1/2012

Temperature: 18      Time: 6:11   24/1/2012

Temperature: 18      Time: 6:12   24/1/2012

Temperature: 18      Time: 6:13   24/1/2012

Temperature: 19      Time: 6:14   24/1/2012

Temperature: 18      Time: 6:15   24/1/2012

Temperature: 18      Time: 6:16   24/1/2012

Temperature: 19      Time: 6:17   24/1/2012

Temperature: 19      Time: 6:18   24/1/2012

Temperature: 19      Time: 6:19   24/1/2012

Temperature: 18      Time: 6:20   24/1/2012

Temperature: 19      Time: 6:21   24/1/2012

Temperature: 19      Time: 6:22   24/1/2012

Temperature: 19      Time: 6:23   24/1/2012

Temperature: 19      Time: 6:24   24/1/2012

Temperature: 19      Time: 6:25   24/1/2012

Temperature: 19      Time: 6:26   24/1/2012

Temperature: 19      Time: 6:27   24/1/2012

Temperature: 19      Time: 6:28   24/1/2012

Temperature: 19      Time: 6:29   24/1/2012

Temperature: 19      Time: 6:30   24/1/2012

Temperature: 19      Time: 6:31   24/1/2012

Temperature: 19      Time: 6:32   24/1/2012

Temperature: 19      Time: 6:33   24/1/2012

Temperature: 19      Time: 6:34   24/1/2012

Temperature: 19      Time: 6:35   24/1/2012

Temperature: 19      Time: 6:36   24/1/2012

Temperature: 19      Time: 6:37   24/1/2012

 

 

The temperature is actually measured in degrees centigrade and the reading time and date stamped. This is because I decided to unite it with something we looked at in an earlier chapter, the RTC (Real-Time Clock) also from Oomlout and Adafruit. Here comes a wiring diagram:

So, we're using the TMP36 as the temperature reading device, the RTC as the time and date stamping device, and the Micro SD board as the storage. The LED is there to give us a visual indicator when a piece of information has been recorded. Now we need some code to bring it all together. Here comes a sketch:

 

//******* Reads the time and temperature and logs**********

//******** them to a text file on a micro SD card**********

//******** Ian Lang Electronics ***************************

#include <Wire.h>

#include "RTClib.h"

#include <SD.h>

RTC_DS1307 RTC;

File myFile;

String outstring;

long centtemp;

void setup(){

      Wire.begin();

    RTC.begin();

    pinMode(10,OUTPUT);

  const int chipselect=10;

}

void loop(){

    DateTime now = RTC.now();

centtemp = analogRead (A0);

centtemp = map(centtemp,0, 1023, -50, 460.8);

outstring=centtemp;

if (now.second()==0){

   digitalWrite(2,HIGH);  SD.begin(10);

myFile = SD.open("temp.txt", FILE_WRITE);

myFile.println();

myFile.print("Temperature: ");

myFile.print(outstring);

myFile.print("      Time: ");

myFile.print(now.hour(),DEC);

myFile.print(":");

if (now.minute()<10){myFile.print("0");}

myFile.print(now.minute(),DEC);

myFile.print("   ");

myFile.print(now.day(),DEC);

myFile.print("/");

myFile.print(now.month(),DEC);

myFile.print("/");

myFile.print(now.year(),DEC);

myFile.close();

delay(1500);digitalWrite(2,LOW);}

}

 

 

Once a minute, when the seconds on the RTC reach 00, this will record the temperature of the surroundings. Whilst it's doing this, the LED will come on, and then go off again. You can read the file created, called TEMP.txt , a number of ways. Firstly you could just slip it out of the Micro SD board and into a reader. My computer has a built-in SD reader, and using a Micro to standard SD adaptor it reads it easily and that's how I tested. You could upload this sketch too:

 

#include <SD.h>

 File myFile;

 void setup()

{

  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.

  // Note that even if it's not used as the CS pin, the hardware SS pin

  // (10 on most Arduino boards, 53 on the Mega) must be left as an output

  // or the SD library functions will not work.

   pinMode(10, OUTPUT);

   if (!SD.begin(10)) {

    Serial.println("initialization failed!");

    return;

  }

  Serial.println("initialization done.");

   // open the file for reading:

  myFile = SD.open("TEMP.txt");

  if (myFile) {

    Serial.println("TEMP.txt:");

     // read from the file until there's nothing else in it:

    while (myFile.available()) {

    Serial.write(myFile.read());

    }

    // close the file:

    myFile.close();

  } else {

  // if the file didn't open, print an error:

    Serial.println("error opening TEMP.txt");

  }

}

 

void loop()

{

// nothing happens after setup

}

 

This is copied directly from ladyada.net with two modifications for our purposes and is part of the test code for the library. Once uploaded, open your serial monitor and the file will appear in it.

 

So, over the page let's look at the sketch and see what it's doing.

More>