Skip to content
If this is your first visit, please click here to learn about shipping and delivery.
If this is your first visit, please touch here to learn about shipping and delivery.
SD Card Data Logging Shield for Arduino

Building an SD Card Data Logger with a DS1307 Clock: Timestamps, Files and Power Loss

A useful data logger must do three different jobs: acquire a measurement, attach meaningful context and preserve the record. A changing number on a display proves only the first part. Reliable timekeeping and storage require their own checks.

The PMD Way SD card shield collection includes an SD data-logging shield with a DS1307 real-time clock. Treat the clock and card interface as separate subsystems during development. The product listing specifies a CR1220 backup-cell holder; confirm the actual holder and circuit before fitting the correct cell.

Define what one record means

Before wiring the shield, write a sample record on paper. Include a sequence number, timestamp, measurement, unit convention and validity status. Decide whether a record represents one instantaneous sample, an average or a minimum and maximum over an interval.

A temperature logger might collect a sample every ten seconds and save one average per minute. That is different from recording only the value present at the minute boundary. The choice affects how later users interpret short events.

Give failures their own representation. A disconnected sensor should not become an apparently genuine zero-temperature reading. An invalid clock should not silently produce a plausible date.

Bring up the clock on its own

The DS1307 manufacturer information describes an I2C calendar clock with backup timekeeping. The backup source preserves the clock’s operation; it does not power the Arduino, SD card or rest of the logger.

First confirm that the host can communicate with the RTC and that the seconds advance. Set a known time using the chosen library’s documented method, then read it back. Remove main power for a controlled interval and confirm that the clock continues correctly with its backup source.

The DS1307 datasheet identifies a clock-halt control bit and specific supply requirements. Use a library that handles the device correctly, and do not assume a returned register value means the oscillator is running.

Separate clock setting from ordinary startup

A common mistake is setting the RTC to a compile-time value every time the program starts. That can reset the clock backwards after a power interruption, especially when an old compiled program remains installed for months.

Make clock adjustment an explicit setup action or a controlled response to a detected invalid state. Record when adjustment occurs so discontinuities in a log can be explained.

Analog Devices’ RTC software guidance discusses robust initialisation and oscillator handling. Apply the principle of checking the clock’s state instead of assuming every boot is a fresh installation.

Choose a consistent time convention

Document whether the RTC stores UTC or local civil time. UTC is often convenient for exchanging logs, while the display can convert it to the user’s local time. The RTC itself does not know a place’s daylight-saving rules.

Use a consistent, unambiguous date format and keep the time convention in the file header or metadata. A timestamp such as “03/04/26” is less clear than an explicitly ordered date.

For periodic sampling, use an appropriate elapsed-time mechanism rather than relying exclusively on wall-clock changes. A clock correction should not unexpectedly make the logger take a burst of samples or wait an hour for the next one.

Test the card independently

Choose a storage library compatible with the host and the intended filesystem. The SdFat project maintained by Bill Greiman documents its supported features and example programs. Avoid transferring configuration assumptions from a different library merely because both can open files.

Confirm the shield’s SPI connections and card-select pin from its actual documentation. Test card detection, file creation, a small write and reopening the result on a computer before integrating sensors.

Format only a card whose contents you have backed up or do not need. Capacity alone does not identify the existing filesystem or guarantee library compatibility.

Plan buffering and write latency

Card operations can take varying amounts of time. If measurements must arrive at a fixed rate, separate acquisition from storage using a bounded buffer appropriate to available RAM.

Calculate the data rate. For example, a hypothetical 80-byte text record written once per second produces about 6.9 MB per day before filesystem overhead. That is easy to accommodate in capacity terms, but capacity says nothing about the longest write delay.

Test worst-case behaviour with the actual card, record size and library settings. Define what happens if the buffer fills: count lost samples, reduce work where acceptable or stop with an explicit error. Do not allow silent overwriting of unread records.

Define what a successful save means

Data can remain in software or device buffers after a write call. Use the library’s documented synchronisation or close operations at a suitable interval, and check reported errors. A file opening successfully at startup does not establish that every later record was stored.

More frequent synchronisation can reduce the amount of recently buffered data at risk, but it is not a guarantee against every power-loss failure. Balance the interval against latency and workload, then test the actual system.

For abrupt shutdowns, make the file format tolerant of a partial final record. On recovery, identify and reject incomplete entries instead of accepting a truncated line as valid data.

Expose status that helps diagnosis

Useful logger status indicators
Status What it tells you
Sample count The acquisition loop is running.
Clock valid Timestamps meet the application’s checks.
Saved-record count The storage path is progressing.
Buffer high-water mark Storage delays are approaching available buffering.
Last error A failed sensor, clock or card operation remains visible.

Keep error reporting lightweight enough that it does not create the timing problem it is trying to diagnose. A small status display or periodic summary is often more useful than continuous verbose output.

Validate a complete recording session

Run the logger with the final power source and sensors, then inspect the file for gaps, duplicate sequence numbers, timestamp jumps and incomplete records. Test a controlled main-power interruption and verify both the clock and storage recovery.

Save the file format, sampling rules, library versions and clock-setting procedure with the firmware. Those details let someone understand the measurements later, when the original breadboard and development computer are no longer available.

Previous article Planning an Arduino LoRa Link: Frequency, Antennas and Reliable Sensor Messages
Next article Connecting an Arduino RS232 Shield to an Instrument: Cable, Handshaking and Message Checks