Sensor test

We'll now test the sensors, using this sketch which is a bit of a mashup of the two examples in our tutorials

#include <SPI.h>
#include <SD.h>

/* Sensor test sketch
  for more information see

#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

  // If you want to set the aref to something other than 5v

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");
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!

Logging sketch

Download the light and temperature logging sketch from GitHub. Insert the SD card.

Look at the top of the sketch for this section and uncomment whichever line is relevant. Check the RTC page for details if you're not sure which one you have.

/************** if you have a DS1307 uncomment this line **************/

//RTC_DS1307 RTC; // define the Real Time Clock object

/************** if you have a PCF8523 uncomment this line **************/

//RTC_PCF8523 RTC; // define the Real Time Clock object


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

Plotting with a spreadsheet

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

Using Gnuplot

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 definitely help you find even more tutorials and links. Mucking about is the best teacher, too!

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.

Other plotters

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!

Portable logging

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 a 6xAA battery holder. It ran the Arduino logging once a second for 18.5 hours. If you use alkalines you could easily get 24 hours or more.

Fridge logging

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.

This guide was first published on Apr 12, 2013. It was last updated on Mar 28, 2013.

This page (Use It!) was last updated on Mar 30, 2013.

Text editor powered by tinymce.