or MicroSD adaptor
Library Used:
AF_SDLog library. from http://www.ladyada.net/make/gpsshield/download.html
I also have used the Sparkfun FAT16 library and examples <HERE> for use with there shield <HERE> and changed the '#define CS 8' on line 33 to '#define CS 10' in the examples to work with my build.
Code
/************************************//* SD card temperature Logger *//* Version 1.e *//* Chris Hawkins *//************************************/#include "AF_SDLog.h"#include "util.h"#include <avr/pgmspace.h>AF_SDLog card;File f;#define ledBusyPin 8#define powerPin 9#define temperaturePin 0#define BUFFSIZE 99#define buttonStartStop 7int buttonState = 0; // current state of the buttonint runState = false; // previous state of the buttonfloat lastPressed =0;char buffer[BUFFSIZE];uint8_t bufferidx = 0;uint8_t i;unsigned int logCount;// blink out an error codevoid error(uint8_t errno) {while(1) {for (i=0; i<errno; i++) {digitalWrite(ledBusyPin, HIGH);digitalWrite(powerPin, HIGH);delay(200);digitalWrite(ledBusyPin, LOW);digitalWrite(powerPin, LOW);delay(200);}for (; i<10; i++) {delay(300);}}}void setup(){Serial.begin(9600);putstring_nl("\r\nData logger");// configure PINSpinMode(ledBusyPin, OUTPUT);pinMode(powerPin, OUTPUT);pinMode(buttonStartStop, INPUT);digitalWrite(powerPin, HIGH);// check SD card - FAT 2GBif (!card.init_card()) {putstring_nl("Card init. failed!");error(1);}if (!card.open_partition()) {putstring_nl("No partition!");error(2);}if (!card.open_filesys()) {putstring_nl("Can't open filesys");error(3);}if (!card.open_dir("/")) {putstring_nl("Can't open /");error(4);}// Check for next file in sequence 00 to 99strcpy(buffer, "TMPLOG00.TXT");for (buffer[6] = '0'; buffer[6] <= '9'; buffer[6]++) {for (buffer[7] = '0'; buffer[7] <= '9'; buffer[7]++) {putstring("\ntrying to open ");Serial.println(buffer);f = card.open_file(buffer);if (!f)break;card.close_file(f);}if (!f)break;}if(!card.create_file(buffer)) {putstring("couldnt create ");Serial.println(buffer);error(5);}f = card.open_file(buffer);if (!f) {putstring("error opening ");Serial.println(buffer);card.close_file(f);error(6);}putstring("writing to ");Serial.println(buffer);putstring_nl("ready!");delay(250);// clear bufferstrcpy(buffer,"");}void loop(){// get button statebuttonState = digitalRead(buttonStartStop);// if button pressed and 2 seconds passedif (buttonState ==HIGH && (millis() - lastPressed)>2000) {lastPressed = millis();runState = !runState;}// if the state has true then log temperature dataif (runState == true) {// build string to write to cardsprintf(buffer, "%s,%d,%u,%d\n",getDate(),getTime (),logCount++,getTemperature());//log data to SD CardlogData();}}int getTemperature(){//getting the voltage reading from the temperature sensorfloat temperature = getVoltage(temperaturePin);//converting from 10 mv per degree wit 500 mV offsettemperature = (temperature - .5) * 100;return temperature;}unsigned int getTime(){//replace with RTC code once addedreturn (millis() /1000);}char* getDate (){//replace with RTC code once addedreturn "24/04/2010";}float getVoltage(int pin){return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts// using TMP36 temperature sensor}void logData( ){digitalWrite(ledBusyPin, HIGH);// LED on for witting to card// get string lengthbufferidx = strlen(buffer);// write data to sd cardif(card.write_file(f, (uint8_t *) buffer, bufferidx) != bufferidx) {putstring_nl("can't write!");error(5);}digitalWrite(ledBusyPin, LOW);// LED off to show finished writting to card}/* End code */
Sample of output to SD card
Schematic