Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SD card datalogger
by Tom Igoe
hacked by Peter Marks to write time stamps
#include <SD.h>
// For the SD Shield Plus
const int chipSelect = 10;
// Global Variables
int i;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
void setup()
{
Wire.begin();
Serial.begin(9600);
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:
pinMode(chipSelect, OUTPUT);
Serial.print("chipSelect set to output\n");
void loop()
{
// make a string for assembling the data to log:
String dataString = getDateDs1307();
dataString += String(",");
// 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);
// Gets the date and time from the ds1307 and return
// result in a format a spreadsheet can parse: 06/10/11 15:10:00
String getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// 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());
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);
return dataString;
}