Software

Here’s the complete sketch. This requires the SD card library, and a FAT-formatted SD card installed in the shield.

Every 60 seconds, the software takes two voltage measurements from the watch. The first is taken while the watch is still in the power-down state, indicating the resting voltage of the battery. The second reading is taken two seconds later, after the watch “marquee” display has been running (which drags down the voltage…so many LEDs!).

The minute counter (starting from zero) and two voltages are written to a line in a text file in CSV (comma-separated value) format, which can then be directly imported into most spreadsheet applications, or it’s fairly easy for other software to parse.

Since we’re just counting the passage of minutes, the counter value is written to the file; it’s not an absolute time stamp. If a project requires proper time/date stamps, we’d want to tie into RTClib (an Arduino realtime clock library) for reading the shield’s clock.
// Watch battery voltage logger.  Activates watch circuit every
// 60 seconds and records voltages (sleep and running) to SD card.
// Based on Tom Igoe's Datalogger example from the SD library.

// Connections:
// Arduino GND to watch battery -
// Arduino analog 0 to watch battery +
// Arduino digital 2 to pull-down switch on watch
// 3.3V to AREF (both on Arduino)

#include <SD.h>

long minutes = 0; // Elapsed time / line number

void setup() {
  Serial.begin(9600);

  // Use 3.3V analog reference for better resolution on 3V battery
  analogReference(EXTERNAL);

  // Watch is awakened with button tap which ties pin (w/internal
  // pullup) to ground.  Rather than write high/low levels to this
  // wire, a pin is set LOW and switched between input (high
  // impedance) and output states to approximate the button press
  // without introducing voltages into the watch circuit.
  pinMode(2, INPUT);
  digitalWrite(2, LOW);

  Serial.print("Initializing SD card...");
  Serial.println(SD.begin(10) ? // 10 = card sel. pin (4 on Arduino Ethernet)
    "card initialized." :
    "card failed, or not present.");

  // Sketch continues even if SD init fails...may just want to
  // read output in serial monitor for debugging.
}

void loop()
{
  long     mVsleep, mVrun;
  char     dataString[40];
  File     dataFile;
  uint32_t start = millis();

  mVsleep = 3300L * analogRead(A0) / 1023L; // Millivolts while asleep

  pinMode(2, OUTPUT); // Pull watch button LOW
  delay(100);         // Hold a moment
  pinMode(2, INPUT);  // and release

  delay(2000);  // Let the watch run for a couple seconds...

  mVrun = 3300L * analogRead(A0) / 1023L; // Millivolts while running

  sprintf(dataString, "%ld,%ld,%ld", minutes++, mVsleep, mVrun);

  if((dataFile = SD.open("datalog.csv", FILE_WRITE))) {
    dataFile.println(dataString);
    dataFile.close();
  } else {
    Serial.println("error opening datalog.csv");
  }
  Serial.println(dataString);

  // Serial and SD card access times may vary.  Rather than delay(),
  // monitor time to allow remainder of 1 minute to pass.
  while(millis() < (start + 60000L));
}
This guide was first published on Dec 04, 2012. It was last updated on Nov 17, 2018. This page (Software) was last updated on May 04, 2015.