OK now that we have introduced both the RTC and the SD card and verified that they're working, we can move onto logging!
We'll use a pretty good & detailed demonstration to show off the capabilities of this most awesome data logging shield: We'll log both temperature and relative light levels to determine:
- How much does the temperature in a fridge vary as the compressor turns on and off?
- Does keeping the door open cause a big temperature drop? How long does it take for it to cool down?
- Does the light inside really turn off when the door is closed?
Items you'll need:
- Arduino (of course!) a Atmega328 type is best - we always recommend going with an official 'classic' Arduino
- Adafruit data logger shield - assembled
- SD card formatted for FAT and tested using our example sketch
- CdS photocell and a matching 10K pulldown resistor
- Temperature sensor with analog out, such as TMP36
- Battery pack such as a 6-AA 'brick' and a 2.1mm DC jack.
- or you can use a 9V clip for a power supply but a 9V powered logger will last only a couple hours so we suggest 6xAA's
- Some 22 AWG wire, soldering iron, solder, etc.
You can get most everything in that list in a discounted pack in the Adafruit shop!
We'll use two basic sensors to log data, a CdS photocell to track light (this will tell us when the door has been opened) and a semiconductor temperature sensor to log the ambient fridge temperature.
We will wire the sensors as follows, with a 10K pulldown resistor for the photocell connected to Analog input 0 and the output of the TMP36 connected to Analog input 1
Note that we connect ARef, the power pin of the temp sensor, and the light sensor to 3.3V not to 5.0V - we do this because the 5V line is very noisy and the 3.3V regulator is better filtered. In the actual board we used the 3.3V line from the datalogger's regulator, see the images below - in theory its the same as the one off of the Arduino but we trust ours more.

Of course, we can use the handy prototyping area on the data logging shield, so that there is no breadboard necessary!
Dont forget to connect the red&green LEDs to Digital 2 and 3 (see below)
By looking at the bottom, you can see how the wires are folded over when soldered to make joints.
If you can use the 3.3V regulator instead of the 5.0V one for your sensors, do that - you'll find you get better readings.
We'll now test the sensors, using this sketch which is a bit of a mashup of the two examples in our tutorials
/* Sensor test sketch
for more information see http://www.ladyada.net/make/logshield/lighttemp.html
*/
#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the analog resistor divider
//TMP36 Pin Variables
int tempPin = 1; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures
int tempReading; // the analog reading from the sensor
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
}
void loop(void) {
photocellReading = analogRead(photocellPin);
Serial.print("Light reading = ");
Serial.print(photocellReading); // the raw analog reading
// We'll have a few threshholds, qualitatively determined
if (photocellReading < 10) {
Serial.println(" - Dark");
} else if (photocellReading < 200) {
Serial.println(" - Dim");
} else if (photocellReading < 500) {
Serial.println(" - Light");
} else if (photocellReading < 800) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}
tempReading = analogRead(tempPin);
Serial.print("Temp reading = ");
Serial.print(tempReading); // the raw analog reading
// converting that reading to voltage, which is based off the reference voltage
float voltage = tempReading * aref_voltage / 1024;
// print out the voltage
Serial.print(" - ");
Serial.print(voltage); Serial.println(" volts");
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((volatge - 500mV) times 100)
Serial.print(temperatureC); Serial.println(" degrees C");
// now convert to Fahrenheight
float temperatureF = (temperatureC * 9 / 5) + 32;
Serial.print(temperatureF); Serial.println(" degrees F");
delay(1000);
}
OK upload this sketch and check the Serial monitor again
In my workroom, I got about 24 degrees C and a 'light measurement' of about 400 - remember that while the temperature sensor gives an 'absolute' reading in C or F, the light sensor is not precise and can only really give rough readings.
Once you've verified that the sensors are wired up correctly & running its time to get to the logging!
Download the light and temperature logging sketch from GitHub. Insert the SD card. Upload the sketch to your Arduino. We'll now test it out while still 'tethered' to the computer
While the Arduno is still connected, blinking and powered, place your hand over the photocell for a few seconds, then shine a flashlight on it. You should also squeeze the temp sensor with your fingers to heat it up
When you're ready to check out the data, unplug the Arduino and put the SD card into your computer's card reader. You'll see a at least one and perhaps a couple files, one for each time the logger ended up running

We'll open the most recent one. If you want to use the same logfile used in the graphing demos, click here to download it.
The quickest way to look at the data is using something like OpenOffice or Excel, where you can open the .csv file and have it imported directly into the spreadsheet

You can then perform some graphing by selecting the columns of data

Clicking the Chart button and using Lines (we think they are the best for such graphs)

Setting the First Column as label


Which will generate this graph

You can see pretty clearly how I shaded the sensor and then shone a flashlight on it.
You can make the graph display both with different axes (since the change in temperature is a different set of units. Select the temp line (red), right-click and choose Format Data Series. In the Options tab, Align data series to Secondary Y-axis.

Or you can make another graph with only the temp data

Now you can see clearly how I warmed up the sensor by holding it between my fingers
Gnuplot is an free (but not open source?), ultra-powerful plotting program. Its also a real pain to use! But if you can't afford a professional math/plotting package such as Mathematica or Matlab, Gnuplot can do a lot!
We're not good enough to provide a full tutorial on gnuplot, here are a few links we found handy. Google will definately help you find even more tutorials and links. Mucking about is the best teacher, too!
- http://www.cs.hmc.edu/~vrable/gnuplot/using-gnuplot.html
- http://www.duke.edu/~hpgavin/gnuplot.html
- http://www.ibm.com/developerworks/library/l-gnuplot/
We found the following commands executed in order will generate a nice graph of this data, be sure to put LOGTEST.CSV in the same directory as wgnuplot.exe (or if you know how to reference directories, you can put it elsewhere)

set xlabel "Time" # set the lower X-axis label to 'time' set xtics rotate by -270 # have the time-marks on their side set ylabel "Light level (qualitative)" # set the left Y-axis label set ytics nomirror # tics only on left side set y2label "Temperature in Fahrenheit" # set the right Y-axis label set y2tics border # put tics no right side set key box top left # legend box set key box linestyle 0 set xdata time # the x-axis is time set format x "%H:%M:%S" # display as time set timefmt "%s" # but read in as 'unix timestamp' plot "LOGTEST.CSV" using 2:4 with lines title "Light levels" replot "LOGTEST.CSV" using 2:5 axes x1y2 with lines title "Temperature (F)"
Which makes this:
Note the cool double-sided y-axis scales! You can zoom in on stuff pretty easily too.
Our friend John also suggests Live-Graph as a free plotting program - we haven't tried it but its worth looking at if you need to do a lot of plotting!
Of course, having a datalogger thats chained to a desktop computer isn't that handy. We can make a portable logger with the addition of a battery pack. The cheapest way to get a good amount of power is to use 6 AA batteries. I made one here with rechargables and it ran the Arduino logging once a second for 18.5 hours. If you use alkalines you could easily get 24 hours or more.
This is made by soldering a 6xAA battery holder to a 2.1mm plug, see here for a tutorial on how to assemble them
With my portable logger ready, its time to do some Fridge Loggin'! Both were placed in the fridge, in the center of the middle shelf.
I placed it in around 10PM and then removed it around noon the next day. If you don't have a fridge handy, you can grab the data from this zip file and use that
Here is the logged data:
You can see in the middle and end the temp and light levels are very high because the logger was outside the fridge. The green line is the temperature so you can see the temperature slowly rising and then the compressor kicking in every half hour or so. The red lines indicate when the door was opened. This night was a more insominac one than normal!
Zooming into the plot at about 12:40AM, we can see how the temperature climbs whenever the door is open, even in a few seconds it can climb 4 degrees very quickly!

OK that was a detailed project but its a good one to test your datalogging abilities, especially since its harder to fix bugs in the field. In general, we suggest trying other sensors and testing them at home if possible. Its also a good idea to log more data than you need, and use a software program to filter anything you dont need. For example, we dont use the VCC log but if you're having strange sensor behavior, it may give you clues if your battery life is affecting it.






