Data Logger
Temperature Logger
Temperature Data Logger
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)
{
analogReference(INTERNAL);
pinMode(fileLedPin, OUTPUT);
setSyncProvider(RTC.get); // the function to get the time from the RTC
// initialize the SD card
if (!card.init())
error(1);
// initialize a FAT16 volume
if (!Fat16::init(&card))
error(2);
// open file for append, create if it doesn't exist
if (!LogFile.open(fileName, O_CREAT | O_APPEND | O_WRITE))
error(3);
// clear write error
LogFile.writeError = false;
LogFile.println("Start");
}
void loop(void)
{
time_t timeStamp = now();
//LogFile.print(timeStamp);
//LogFile.print(',');
printDateTime(timeStamp);
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)
LogFile.print(tempC1,1);
LogFile.print(",");
LogFile.println(tempC2,1);
// write the data to the card at the end of every line
if (!LogFile.sync())
error(4);
delay(logInterval * 1000L);
}
// routine handle file errors
void error(int err)
{
// blink forever
while(1)
{
digitalWrite(fileLedPin, HIGH);
delay(err * 200);
digitalWrite(fileLedPin, LOW);
delay(200);
}
}
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
LogFile.print(',');
}
void printDigits(int digits, char seperator)
{
// utility function time with leading 0
if(digits < 10)
LogFile.print('0');
LogFile.print(digits);
LogFile.print(seperator);
}

Comments (0)