Friday, January 7, 2011

Arduino Light and Temperature Logger

Hardware mostly been magic to me.  Except for the broad system categories of how it works I'm pretty ignorant.  It's kinda like the power coming out of the "holes".  I write software and something wonderful happens in the complicated looking green circuit boards inside.  And yet I've often had hardware ideas that I've always dismissed as "I have *no* idea how someone might build that."  For example a device controlled by SMS (e.g. an electronic lock so that you could let your friend into your house from work simply by texting a code to your doorknob).

So it is with some delight that I found Arduino.  It's an open source hardware prototyping platform with a simple language (similar to C) and slick IDE to compile and upload code to micro-crontroller.  One of the elegant things about how the Arduino is build is that you can stack "shields" on top of the board that add functionality.  There are several places online where you can buy shields such as Adafruit.

For Christmas I got several shields, one of which was a light and temperature logger.  The base of the shield was an interface to an SD card.  On top of that you added a light sensor and a temperature sensor.  After soldering it all together (I've come to love soldering) you upload the code attach a battery pack and then start collecting data.

Data is written out into a CSV file on the SD card that looks something like this:
millis,light,temp,vcc
1998,613,68.46,1.84
3000,628,68.46,1.79
3999,669,68.46,1.68
4999,671,68.46,1.68
5999,628,68.46,1.8
7000,385,68.46,2.99
7999,628,68.46,1.79
I left my device outside over night on a particularly cold night and generated a bunch of data.  So much so that I ran into the nasty limit that Excel doesn't deal with more than 32,000 data points.  So I whipped out Octave to look at the data.  I just wanted the light and temperature data, so after grabbing only those columns I whipped up the following Octave code:
d = csvread('cabin.csv')
statistics(d)
figure(1)
plot(d(42000:55000,1))
legend('Light', "location", 'southeast')
print('light.png', '-dpng')
figure(2)
plot(d(42000:55000,2))
legend('Temperature (F)', "location", 'southeast')
print('temperature.png', '-dpng')
It was a little painful to get Octave working on my mac with AquaTerm and all of the octave-forge contributed code building from MacPorts (there is a binary distribution, but I was determined to win and the key thing I ended up learning is that Octave wants/needs AquaTerm... not just any old X11 server)

The statistics line from above generated some basic summary statistics.
octave:2> statistics(d)
ans =

    39.0000    17.9900
    48.0000    18.5700
    48.0000    19.7300
    61.0000    22.0500
   948.0000    68.4600
   188.2125    21.4046
   307.3645     4.9441
     1.8685     3.6643
     1.5966    21.1867
From that data we can see cool stuff like the high temperature was 68.46 (inside the house before I too the device outside), the low temperature was 17.99, the average was 21.4 with a standard deviation of 4.94.  The numbers for light are a little more abstract to me because I didn't look up the units on the photo sensor was measure. (NOTE in octave do "help statistics" to figure out what the other values mean).

The plot commands were used to generate a plot of the data for a slice of time that seemed interesting to me:
What you see here is the temperature rising from the low back up to the mid-twenties as day broke.  An interesting thing to observe is the tiny oscillation of the data.  I doubt the temperature was actually changing like that, but rather the degree of sensitivity of the sensor was such that it sometimes was one value and other times was another.

Finally here's the light plot.
From here you can see daybreak happen until you hit the maximum reading from the light sensor.

Overall I've been loving the Arduino and projects like this are just plain awesome.  You get to build stuff that actually interacts with the world.

No comments:

Post a Comment