Temperature Data Logger

This is our second project with Michael Margolis, this time a two sensor temperature data logger.  In our case we have set it up to measure internal and external temperatures but you might equally use it to measure flow and return temperatures on a solar thermal system or something else.  You could also expand the number of sensors to 5 and start looking at hot water tank stratification in your solar system.... the possibilities are many and the cost is very low.   We are using the MCP9700A temperature sensor in this example as they are very cheap and much less noisy than the LM35, especially on long wires.  As they are such good value we have made up a little kit which we are providing at the same cost as a bare Seeeduino Stalker.... to get you off to a good start - enjoy.

The Seeeduino Stalker sketch is nicely written by Micahel Margolis and takes care of some of the pitfalls experienced with the Stalker demo code.  The sketch will either create a new file on the card if it doesn't exist and start writing to it or it will append an existing file - gone are the frustrations of non-zero byte files and formatting sensitivities - it just works much better.

Michael has also included a write error LED which lets you know if you have a problem writing to the microSD card without having to pop the card out and have a look.  You may notice that the sketch is also sampling 8 times during the sample interval and averaging the result to iron out aberations.

The sketch is available below - enjoy, and please let us know how you get on via the comments section at the bottom of the page.

The Parts List:

2no. MicroChip - MCP9700A - temperature sensors.

1 no. LED.

1 no. 100Ω resistors for the LED

The wiring is simple enough:

The Sensors:

The MCP9700A temperature sensor looks like this (Actually ths is an LM35 which we were using intially - the MCP9700A is wired the same)




Sensor Left Pin (+V) to Stalker +5V

Sensor Mid Pin (Vout) to Stalker Analog pin 0 & 1

Sensor Right Pin (GND) to Stalker GND 






The LED:

Solder a 100Ω resistor to the positive (longer) leg of the LED.

This Write Error LED is on Stalker digital pin 6 so the LED positve (with the resistor attached) goes to Stalker digital pin 6 and the negative leg (adjacent to the flat on the plastic LED housing) goes to Stalker GND.

You should end up with this (with the MCP9700A flat face towards you):


Mounting the sensors:

Your call really, there are some nice mounting ideas out there on the web.  For this internal/external temperature example I had the internal MCP9700A bare and the external sensor mounted in an old 35mm film canister using SUGRU silcon stuff to seal the hole/lid - a wrap of tin foil helped reflect any incident light (I am working on a propoer external sensor housing at mo - more later).

The Arduino Sketch:

You need to download the FAT16 Arduino library from http://code.google.com/p/fat16lib/ and place the files in your ...\arduino-0019\libraries  (or similar if using a different Arduino IDE version) folder before you attempt to upload your sketch (downloaded from the bottom of the page).

You can adjust the logging interval using this line in the code:

const long logInterval = 300;

The 300 is the interval, in seconds, at which the data gets written to the SD card.  In this case every 5 minutes.  A microSD card of 256Mb is enough space to store about 8 million time stamped data records which at a 5 minute logging interval equates to about 76 years worth of data - should be enough !

You should now be set - I have assumed you have successfully uploaded the sketch below to your Stalker data logger and inserted your SD card ready to power up for your data logging session.

So, all being well, the Stalker onboard LED will be blinking every time a sample is taken and gets written to the microSD card.  Once you have collected your desired data period you can download your CSV file from the microSD card which will be in the following format:


Pretty good - but even better if you graph it out in your spreadsheet:


This graph shows internal and external temperatures in centigrade recorded over a 4 day period.  Have fun & do share your experiences via the comments section at the bottom of the page.


The Sketch:

You can also download the PDE directly from the link at the bottom of the page.

 * SimpleMCP9700ALogger -
 * This version logs the temperature every 5 minutes
 * change logInterval variable to change interval
 * Copyright Michael Margolis 2010
 * In association with www.airsensor.co.uk

#include <Fat16.h>  // the SD Card library - http://code.google.com/p/fat16lib/
#include <Wire.h> 
#include <Time.h> 
#include <DS1307RTC.h> // a basic DS1307 library - http://www.arduino.cc/playground/Code/Time
const int  fileLedPin  =  6;  // LED to indicate a file error
const long logInterval = 300; // the interval in seconds between each log
const char *fileName = "logdata.csv";  // the name of the log file
SdCard card;
Fat16 LogFile;
// Sensor constants
const int AREF_MV = 1100; // the reference voltage in millivolts
const int sensor1Pin = 0; // the analog pin the first sensor is connected to
const int sensor2Pin = 1; // the analog pin the second sensor is connected to
void setup(void)
  pinMode(fileLedPin, OUTPUT);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  // initialize the SD card
  if (!card.init())
  // initialize a FAT16 volume
  if (!Fat16::init(&card))
  // open file for append, create if it doesn't exist 
  if (!LogFile.open(fileName, O_CREAT | O_APPEND | O_WRITE))
  // clear write error
  LogFile.writeError = false;
void loop(void)
   time_t timeStamp = now(); 
   int val1 = 0;
   int val2 = 0;
   for( int i=0; i < 8; i++)  // take 8 readings from LM35 1
     val1 = val1 + analogRead(sensor1Pin);
   for( int i=0; i < 8; i++)  // take 8 readings from LM35 2
     val2 = val2 + analogRead(sensor2Pin);
   delay (logInterval*100);
   float average1 = val1 / 8.0;  // the average is the total divided by 8
   float average2 = val2 / 8.0;  // the average is the total divided by 8
   float millivolts1 = (average1 / 1024) * AREF_MV;  // calculate the voltage
   float millivolts2 = (average2 / 1024) * AREF_MV;  // calculate the voltage
   float tempC1 = ((millivolts1-500) / 10);     // calculate the temperature (10mv per degree with 500mV offset)
   float tempC2 = ((millivolts2-500) / 10);     // calculate the temperature (10mv per degree with 500mV offset)
   // write the data to the card at the end of every line
    if (!LogFile.sync())
    delay(logInterval * 1000L);   
// routine handle file errors
void error(int err)
  // blink forever
   digitalWrite(fileLedPin, HIGH);
   delay(err * 200);
   digitalWrite(fileLedPin, LOW);
void printDateTime(time_t t)
  printDigits(hour(t),':' );
  printDigits(minute(t),':' );
  printDigits(second(t),',' );  // uncomment this to print seconds (and replace the comma above with colon)
  // print a space here if you need it
  printDigits(day(t),'//' ); 
  printDigits(month(t),'//' );  
//  LogFile.print(year(t));  // prints year using 4 digits
  LogFile.print(year(t)-2000);  // prints year using 2 digits
void printDigits(int digits, char seperator)
  // utility function time with leading 0
  if(digits < 10)

5.0/5 rating (3 votes)

Comments (0)

Leave a comment

Please login to leave a comment.