datetime readings from it as we log our sensor data.
In the last article, we saw that we only need to record
the datetime at the beginning of the sampling session,
then we can extrapolate the datetime for each sample by
simply adding the sampling interval. We could still do that,
but since we have 2 GB of data to play with, why bother?
Let’s just get lazy and record the datetime for each
Like many things Arduino, we are blessed to have a
library that does what we want to do with an SD card,
and we are doubly blessed because the Arduino IDE
(Integrated Development Environment) has several very
useful examples for using the SD card.
Open the Arduino IDE and then click on the File
menu button, then on Examples\SD\. You’ll see the six
example files listed in Figure 13: Cardinfo, DataLogger,
DumpFile, Files, listfiles, Read Write. These examples will
provide the basis for us to build the SD card part of our
data logger. You’ll want to use the Cardinfo example to
make sure your SD card setup is working properly. When I
first ran the program, I got the information in the Serial
Monitor shown in Figure 14.
So, I went back and actually looked at the code and
saw that the example defaults to the Ethernet shield. If you
want to use it with the AdaFruit SD shield, you need to
change chipSelect from 4 to 10 as follows:
To further simplify things, let’s do a test run where we
only sample the two DHT22 sensors so that we can show
that data and leave the ‘Control’ part of the fresh air
controller software for the moment. We will write a simple
program (sd_sensor_test) that samples the sensors once
per five seconds, then saves that data to the SD card. I’ll
do this sampling next to my laptop, with one sensor
hanging out in the room and the other stuck right up next
to my laptop’s exhaust fan (again, refer to Figure 1). Both
sensors start at the same temperature and humidity, but
the one next to the exhaust sees a rapid increase in
temperature and decrease in relative humidity.
//change this to match your SD shield or module;
//Arduino Ethernet shield: pin 4
//Adafruit SD shields and modules: pin 10
//Sparkfun SD shield: pin 8
const int chipSelect = 10;
We will then upload this data to the PC and chart it as
we did with the sine wave data. Figure 16 shows the
results. Since I’m running a bit long on this article, I’ll
make the source code ( sd_sensor_test.ino) available at the
article link. This program loads the data onto the SD card
and it also sends it out to the PC at each sample interval.
This gives you two ways to get the data.
Figure 15 shows the information with the correct
chipSelect set for a 2 GB card.
Saving Temperature and Humidity Data in CSV Format
First, we need to decide how we’ll save the data so
that we can chart it. We will want to have each of the four
fresh air controller sensor samples — inTemp, inHum,
out Temp, and outHum — associated with a datetime for
each sample interval.
You could get the data using Pu TTY as discussed, or
you could wait until you’ve finished sampling and then
remove the SD card from the AHP, plug it into the PC,
and get the data off the card [we name this file datalog.csv
in the Arduino source code, and the Arduino creates this
file on the SD card]. When you plug the SD card into your
PC, it opens the SD card as a ‘Device with removable
storage’ in Windows Explorer. You can access the file just
like any other file in a Windows directory as shown in
Figures 16 and 17.
■ FIGURE 17: DATALOG.CSV. ■ FIGURE 16: Removable disk D:.
December 2013 63