An SD Shield Plus, purchased via eBay, has all the things you need for stand alone data logging. An SD slot, a battery backed clock, and even a bit of non-volatile utility ram.
My Arduino is a slightly older model, the Diecimila and I found that as soon as I called SD.begin() the device would reset. It turns out that the SD library code uses more ram than is available in the older models.
The new Arduino Uno looks like a good improvement. Aside from a more spacious Atmel chip, the USB interface is a processor itself and can be set as a HID device which could be handy. I ordered a couple of Uno's from Little Bird Electronics, and they shipped within minutes, but I still couldn't wait and went to Jaycar to pick up an interesting Uno clone called "Eleven" made by Freetronics.
I hacked together the sample code to write a log file to the SD card with a date time stamp from the clock chip in a format that a spreadsheet will parse. It reads three of the analog inputs and writes a line to the file every ten seconds. Here's the code for future reference:
SD card datalogger
by Tom Igoe
hacked by Peter Marks to write time stamps
This example code is in the public domain.
// For the SD Shield Plus
const int chipSelect = 10;
// This is to talk to the real time clock
#define DS1307_I2C_ADDRESS 0x68 // This is the I2C address
// Global Variables
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Serial.print("Initializing SD card...\n");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
Serial.print("chipSelect set to output\n");
// see if the card is present and can be initialized:
Serial.println("Card failed, or not present\n");
// don't do anything more:
// make a string for assembling the data to log:
String dataString = getDateDs1307();
dataString += String(",");
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++)
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2)
dataString += ",";
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.csv", FILE_WRITE);
// if the file is available, write to it:
// print to the serial port too:
// if the file isn't open, pop up an error:
Serial.println("error opening datalog.csv");
delay(1000 * 10);
// Gets the date and time from the ds1307 and return
// result in a format a spreadsheet can parse: 06/10/11 15:10:00
// Reset the register pointer
// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.receive() & 0x7F);
minute = bcdToDec(Wire.receive());
hour = bcdToDec(Wire.receive() & 0x3F);
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());
String dataString = "";
dataString += Print2Digit(dayOfMonth);
dataString += String("/");
dataString += Print2Digit(bcdToDec(month));
dataString += String("/"); // Y2k1 bug!
dataString += Print2Digit(bcdToDec(year));
dataString += String(" ");
dataString += Print2Digit(hour);
dataString += String(":");
dataString += Print2Digit(minute);
dataString += String(":");
dataString += Print2Digit(second);
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
return ( (val/16*10) + (val%16) );
String Print2Digit(byte Val)
String dataString = "";
if (Val < 10)
dataString = "0";
dataString += String(Val, DEC);
The output file looks like this:
Here's a few days of data. Today was partially cloudy but the battery was re-charged in the end.
import matplotlib.pyplot as plt
INFILENAME = 'datalog.csv'
VOLTSCALE = 665.0 / 13.7
datareader = csv.reader(open(INFILENAME,'r'), delimiter=',')
dates = 
points = 
for row in datareader:
if len(row) > 1:
#Converts str into a datetime object. 08/10/11 12:23:12
volts = float(row) / VOLTSCALE
fig = plt.figure()
plt.title("Battery Voltage over several days")
if __name__ == "__main__":
Hey I use processing with the arduino to graph and save my data. I've made a real time monitoring graph and save also the data on excel in case of further analysis.
Now this program matplotlib looks really nice. I want to use but it doesnt look like it comes free. im downloading,
The Enthought Python Distribution
which is released under a less restrictive license than the (full) EPD. do you know where I can download a full or is this restricted enough for just saving and plotting data.
Also with processing I am planning on saving data in excel with timestamps in a column and graphing both real time with the timestamp. However I dont have any sd card shield. I googled a bit to find the function for timestamp
and came across this Date d = new Date();
long current = d.getTime()/1000;
do you knw about this?
My real time montoring is no where near as good as yours. I'd love to try your code for my project.
Matplotlib is free so give it a try. I don't know what Date() does without a clock chip, maybe it starts when you power up.
I am very sorry to bother you about this, but the links I've googled so far are working properly on my computer.
Can you please send me a link on where I can download this program for windows, because I've googled a few websites and I cant find any proper link.
You can find a matplotlib installer for windows at: http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.1.1/
It's a python module so you'll also have to install python.
I'm sorry I don't use Windows so I can't really be of much help there. Consider getting an old PC and installing linux on it - everything works much better in my experience.
hi.. may i know is it this arduino code used an additional library? because when i want to compile it, it shows error...
What error do you get?
thanks for posting your code, I'm also getting an error, Arduino Compiler is expecting a file after your first #include, please advise on the filename.
Sorry about that Juane and Hazim.
I've updated the code to include SD.h which comes on the CD with the SD Shield plus.
I have been looking at your code and changed it to suit a project I'm working on. I was wondering what your thoughts were on the Data Logger Shield? Did it take long to arrive from Thailand?
It's a good shield. I don't really remember how long it took to arrive, so I guess it was reasonable.
date & time issue how can I solve it ?
Great code, the only issue I'm having is that I'm getting an error saying that the "Wire" library doesn't have the members 'send'and 'receive'? Any ideas on how to get around this?
Thanks in advance.
Post a Comment