The very first thing we need to do is call the libraries for our various needs. The RTC needs two libraries:


#include <Wire.h>

#include "RTClib.h"


and the Micro SD board one:


#include <SD.h>


Then we have to tell it what sort of RTC chip we're using:




followed by setting an objectname for the file we're going to create. This is NOT the file name. The file name will be set by the object as we'll see later on:


File myFile;


Now we declare a string that we're going to use to read the temperature:


String outstring;


and a long variable for the analogue reading we're going to convert to temperature:


long centtemp;


Now we get to the sketch proper, and in the setup we set the libraries running that require it:

void setup(){




Pin 10 is the pin that's going to control whether the Micro SD board is active or not. So we set it to output and tell the Arduino so:



  const int chipselect=10;



Now we come to the gubbins of the sketch. First we poll the RTC for the time and date:


void loop(){

    DateTime now = RTC.now();


Then take the reading the TMP36 is putting out:


centtemp = analogRead (A0);


Next we map that reading. When the TMP36 is putting out zero volts, it's -50 degrees. When it's doing 5, it's 460.8. It does 10 mV per degrees centigrade, and so we know that's constant:


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


We can put that to another variable. This variable is a string:




Now we have a conditional. We only want one reading per minute. The easiest way is to say if the seconds are at 00, read it, and then wait 1.5 seconds before going through the loop again. So:


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


Now we switch on that LED to show it's recording and start the Micro SD board:


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


Assign a filename to the myFile object and open it (if it doesn't exist it'll create it and write to it, if it does it'll append it):


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


Now finally we want to write the data to the file. We start with a carriage return:




Then the word "Temperature"


myFile.print("Temperature: ");


Then the temperature value:




Then the word "Time" followed by a colon:


myFile.print("      Time: ");


Then the current hour:




A colon:




The next bit looks to see if the minutes are lesser than 10. If they are, it adds a leading zero, because 6:02  would come out as 6:2 otherwise and it's just annoying:


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



We put in a space to delineate where time finishes and date starts:


myFile.print("   ");


write the date:




write a forward slash:




then the month and another forward slash:





then the year:




close the file (or else the data won't save)




and finally wait one and a half seconds for the clock to tick past 00 seconds so we don't get multiple readings for the same minute and extinguish the LED.






Piece of cake.


Reading through the IDE serial monitor is not very stylish, and nor is whipping out the Micro SD card. We can do better. Over the page, let's see if we can use Visual Basic 2010 to hack together an interface.



Logging Data to an SD Card

Go Back More> Previous page