# Temperature data logger with DS18B20, DS3231 clock and SD card on Arduino: design worksheet and code

> A ₹1,462 design worksheet for a stand-alone temperature logger that writes a timestamped CSV line to an SD card, with the storage per day and per year worked out for different logging intervals.

Canonical: https://roboticsengineering.in/projects/temperature-data-logger-ds18b20-ds3231-sd-card-arduino/ | Published: 2026-11-04 | Updated: 2026-11-04

**A ₹1,462 logger that records temperature with a time stamp to an SD card, with no computer attached. A DS18B20 probe measures, a DS3231 keeps accurate time, and every interval the sketch appends one line to a CSV file you can open in a spreadsheet. The page works out how much space each logging rate uses.**

**Scope:** intermediate. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The storage table was computed by script for a 23-byte line such as 2026-11-04 12:30,24.50 plus a newline. The DS18B20 is commonly listed at about ±0.5 °C accuracy over most of its range, so treat logged values as indicative; check any important reading against a trusted thermometer. SD cards need FAT16 or FAT32 formatting for the usual Arduino library.

## How it works

A 12-bit DS18B20 reading takes up to 750 ms to convert and has a resolution of 0.0625 °C, which is finer than its accuracy, so two decimals are plenty. One line of text at 23 bytes written every minute is 1,440 lines, or about 32 kB, per day, and 11.5 MB in a year. At one line per second it is about 1.9 MB per day, still small for a card of several gigabytes but awkward for a spreadsheet.

The sketch opens the file, appends a line and closes it each time, which costs a few milliseconds but means a power cut loses at most the last line. Opening and closing once per sample is the safe pattern for a battery logger.

## Bill of materials

| Part | Qty | Unit | Total |
| --- | --- | --- | --- |
| Arduino Uno R3 (or compatible) | 1 | ₹550 | ₹550 |
| DS18B20 waterproof probe | 1 | ₹150 | ₹150 |
| 4.7 kilohm resistor | 1 | ₹2 | ₹2 |
| DS3231 real-time clock module with CR2032 | 1 | ₹200 | ₹200 |
| Micro SD card module (SPI) | 1 | ₹100 | ₹100 |
| Micro SD card, 8 GB | 1 | ₹350 | ₹350 |
| Breadboard | 1 | ₹60 | ₹60 |
| Jumper wires | 1 | ₹50 | ₹50 |
| Parts total | ₹1,462 |  |  |

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.

## The numbers

| Interval | Lines per day | Data per day | Data per year |
| --- | --- | --- | --- |
| 1 s | 86400 | 1940.6 kB | 691.7 MB |
| 10 s | 8640 | 194.1 kB | 69.2 MB |
| 1 min | 1440 | 32.3 kB | 11.5 MB |
| 10 min | 144 | 3.2 kB | 1.2 MB |

*23 bytes per line (timestamp, comma, value, newline). Real files vary with number formats.*

The DS3231 is commonly listed at about ±2 ppm, which is roughly a minute of drift a year in the best case, but check the module you receive. Fake or low-grade clocks drift more. Set the clock once from the compile time and check it against a phone after a week.

## Power and wiring

| Part | Connects to | Note |
| --- | --- | --- |
| DS18B20 data | D2 with 4.7 kΩ to 5V | VCC 5V, GND |
| DS3231 SDA / SCL | A4 / A5 | Address 0x68 |
| SD module CS | D10 | MOSI D11, MISO D12, SCK D13 |
| SD module VCC and GND | 5V and GND | Use a module with a level shifter for 5 V boards |

*Many SD modules need 5 V on VCC because of an on-board regulator; check yours.*

## The sketch

```
// Temperature logger: Uno + DS18B20 + DS3231 + SD card
// Design worksheet: libraries are OneWire, DallasTemperature, RTClib, SD.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <RTClib.h>
#include <SD.h>

OneWire oneWire(2);
DallasTemperature sensors(&oneWire);
RTC_DS3231 rtc;
const int CS = 10;
const unsigned long INTERVAL_MS = 60000UL;       // one line per minute
unsigned long last = 0;

void setup() {
  Serial.begin(9600);
  sensors.begin();
  if (!rtc.begin()) { Serial.println("RTC not found"); while (true) {} }
  if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  if (!SD.begin(CS)) { Serial.println("SD failed"); while (true) {} }
  File f = SD.open("log.csv", FILE_WRITE);
  if (f) { if (f.size() == 0) f.println("time,temp_c"); f.close(); }
  last = millis() - INTERVAL_MS;
}

void loop() {
  if (millis() - last < INTERVAL_MS) return;
  last = millis();

  sensors.requestTemperatures();
  float t = sensors.getTempCByIndex(0);
  if (t == DEVICE_DISCONNECTED_C) { Serial.println("probe missing"); return; }

  DateTime now = rtc.now();
  char line[32];
  snprintf(line, sizeof(line), "%04d-%02d-%02d %02d:%02d,",
           now.year(), now.month(), now.day(), now.hour(), now.minute());

  File f = SD.open("log.csv", FILE_WRITE);
  if (f) {
    f.print(line); f.println(t, 2);
    f.close();                                    // safe against power loss
  } else {
    Serial.println("write failed");
  }
}
```

If the clock lost power, the sketch sets it to the time the code was compiled, which will be wrong by however long ago that was; set it properly once with a known time. A missing probe is skipped rather than logged as an extreme value. The Uno has little RAM, so keep strings short and avoid building large Strings. The sketch has been proofread but not compiled on this site.

## Test plan

1. **Probe check:** hold the probe in your hand and see the reading rise; put it in iced water and check it near 0 °C.
2. **Clock check:** compare the timestamp with a phone.
3. **SD check:** remove the card after a few lines and open the file on a computer.
4. **Power cut:** pull power during logging and confirm earlier lines survive.
5. **Long run:** log for a day and check for gaps.
6. **Missing parts:** unplug the probe and the card in turn and check the messages.

## What usually goes wrong

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| SD failed | Card not FAT16 or FAT32, or wiring wrong | Format the card and check the pins |
| Reading −127 | Probe or pull-up resistor missing | Check the 4.7 kΩ resistor |
| Wrong time | Clock never set | Set it from a known time |
| Gaps in the file | Card write errors or power dips | Use a good card and a steady supply |
| Strange characters | File open when removed | Close the file each time and power down before removing the card |

## Upgrades

- **Two probes:** log inside and outside.
- **Battery logging:** sleep the board between readings.
- **Display:** show the latest value on an OLED.
- **Wi-Fi sync:** move to an ESP32 to upload the file.

## Cost and parts notes

The ₹1,462 total is an illustration and does not include shipping. Use the [cost calculator](https://roboticsengineering.in/robotics-project-cost-calculator/) to compare sellers.

## Common questions

**How much space does it use?**

About 32 kB a day at one line a minute, from the storage table.

**Why close the file every time?**

So a power cut loses at most the last line.

**How accurate is the DS18B20?**

It is commonly listed at about ±0.5 °C, so check important readings against a reference.

**Why use a DS3231 and not the Arduino clock?**

The Arduino’s timer drifts and forgets the time at power-off, while the DS3231 is much steadier and has a battery.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.
