Temperature data logger with an SD card on an Arduino Uno
A DS18B20, a DS3231 clock and a microSD card: every 5 seconds a dated temperature line goes into TEMPLOG.CSV, ready to open in a spreadsheet. The whole build, an Arduino Uno and 6 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 30 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $36. On Mokxi they're free.
What you'll learn
- Logging readings to a CSV file on an SD card
- Time-stamping data with a DS3231 real-time clock
- Reading a DS18B20 over a one-wire pin with a pull-up
- Opening and closing a file for each write to protect data
- Timing a repeating task with millis
- Difficulty: Intermediate
- Time: About 30 minutes
- Board: Arduino Uno R3
- Parts: 11
How it works
Every 5 seconds the sketch reads a DS18B20 temperature sensor on pin 2 (with a 4.7k pull-up to 5 V) and asks a DS3231 clock for the date and time. It writes one line such as 2026-11-01,09:00:05,21.50 to the Serial Monitor and to TEMPLOG.CSV on a microSD card.
The first time the card is used, the sketch writes a header line, date,time,temp_c. After that it adds one line per reading. The file is opened, written and closed each time, so pulling the card loses at most the line being written. The LED on pin 7 blinks for every line saved.
Type d in the Serial Monitor to print the whole file back from the card. With no card, the logger keeps printing readings and says so once.
Parts list
11 parts, plus the jumper wires. They retail for about $36; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × microSD card moduleIn Rocket Flight Computer Kit.
- 3 × Power, 5 V
- 1 × Half breadboard
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, greenIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × Temperature sensor, DS18B20
- 1 × Resistor, 4.7k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Real-time clock, DS3231In Christmas Lights Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| microSD card module pin SCK | Arduino Uno R3 pin 13 |
| microSD card module pin MISO | Arduino Uno R3 pin 12 |
| microSD card module pin MOSI | Arduino Uno R3 pin 11 |
| microSD card module pin CS | Arduino Uno R3 pin 10 |
| Resistor, 220 Ω pin 1 | Arduino Uno R3 pin 7 |
| Temperature sensor, DS18B20 pin DQ | Arduino Uno R3 pin 2 |
| Resistor, 4.7k Ω pin 1 | Arduino Uno R3 pin 2 |
| Arduino Uno R3 pin GND | Ground |
| Real-time clock, DS3231 pin SDA | Arduino Uno R3 pin A4 |
| Real-time clock, DS3231 pin SCL | Arduino Uno R3 pin A5 |
| microSD card module pin GND | Ground |
| microSD card module pin VCC | 5 V |
| LED, green pin A | Resistor, 220 Ω pin 2 |
| LED, green pin C | Ground |
| Temperature sensor, DS18B20 pin GND | Ground |
| Temperature sensor, DS18B20 pin VDD | 5 V |
| Resistor, 4.7k Ω pin 2 | 5 V |
| Real-time clock, DS3231 pin VCC | 5 V |
| Real-time clock, DS3231 pin GND | Ground |
The note on the canvas
Word for word, as it sits beside the circuit in the editor.
Try it
1. Press Run. Every 5 seconds a dated line goes into TEMPLOG.CSV and the LED blinks.
2. Drag the DS18B20's slider and watch the next lines follow it.
3. Type d and Enter in the Uno's serial box to print the file back from the card.
4. Click TEMPLOG.CSV in the card's list to download it, then open it in a spreadsheet.
5. Click the card to pull it out: the next write fails and the logger says so. Put it back and press the Uno's reset button: the new lines go on the end of the same file.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Temperature data logger: a DS18B20 thermometer, a DS3231 clock for the
// time stamps and a microSD card. Every five seconds one line goes into
// TEMPLOG.CSV, which opens in any spreadsheet.
//
// DS18B20 DQ pin 2, with a 4.7k pull-up from DQ to 5V; VDD 5V, GND
// DS3231 SDA A4, SCL A5, VCC 5V, GND
// microSD CS pin 10, MOSI 11, MISO 12, SCK 13, VCC 5V, GND
// LED pin 7 through 220 ohms: one blink for every line written
//
// THE FILE. The first time the card is used the sketch writes a header,
//
// date,time,temp_c
// 2026-11-01,09:00:05,21.50
//
// and after that one line per reading. The file is opened, written and
// closed every time, so pulling the card (or the power) loses at most the
// line being written. Leave the card in for days and the file keeps
// growing: at one line every five seconds that is about 450 KB a day.
//
// THE CLOCK. millis() forgets the time at every reset, and a log without
// real dates is hard to use. The DS3231 keeps the time on its own coin cell,
// so it is set once and then left alone. To set a new module, upload once
// with the rtc.adjust() line in setup() uncommented (it takes the time the
// sketch was compiled), then comment it out again and upload, or every reset
// would put the clock back to that moment. In Mokxi the clock starts at the
// time in its start property.
//
// THE SERIAL MONITOR (115200) shows every line as it is logged. Type d and
// Enter to print the whole file back from the card, which is how you check
// what is on it without taking it out.
//
// NO CARD? The logger still reads and prints every five seconds, and says
// so once. Put a card in and press reset to start logging. A card pulled out
// while it runs is noticed at the next write, and the same applies.
//
// Change INTERVAL_MS to log more or less often.
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
const int SD_CS = 10;
const int LED = 7;
const unsigned long INTERVAL_MS = 5000;
const char *FILE_NAME = "TEMPLOG.CSV";
OneWire oneWire(2);
DallasTemperature sensors(&oneWire);
RTC_DS3231 rtc;
bool cardReady = false;
unsigned long lastLog = 0;
void print2(Print &out, int n) {
if (n < 10) out.print('0');
out.print(n);
}
// "2026-11-01,09:00:05,21.50" to wherever `out` points.
void writeLine(Print &out, const DateTime &t, float c) {
out.print(t.year());
out.print('-');
print2(out, t.month());
out.print('-');
print2(out, t.day());
out.print(',');
print2(out, t.hour());
out.print(':');
print2(out, t.minute());
out.print(':');
print2(out, t.second());
out.print(',');
out.println(c, 2);
}
// Start the card, and write the header if the file is new.
bool startCard() {
if (!SD.begin(SD_CS)) return false;
if (!SD.exists(FILE_NAME)) {
File file = SD.open(FILE_NAME, FILE_WRITE);
if (!file) return false;
file.println("date,time,temp_c");
file.close();
}
return true;
}
void dump() {
if (!cardReady) {
Serial.println("no card to read");
return;
}
File file = SD.open(FILE_NAME);
if (!file) {
Serial.println("could not open TEMPLOG.CSV");
return;
}
Serial.println("--- TEMPLOG.CSV ---");
while (file.available()) Serial.write(file.read());
file.close();
Serial.println("--- end ---");
}
void logReading() {
sensors.requestTemperatures();
float c = sensors.getTempCByIndex(0);
DateTime t = rtc.now();
if (c == DEVICE_DISCONNECTED_C) {
Serial.println("no DS18B20: check DQ on pin 2 and the 4.7k pull-up");
return;
}
writeLine(Serial, t, c);
if (!cardReady) return;
File file = SD.open(FILE_NAME, FILE_WRITE);
if (!file) {
Serial.println("could not write TEMPLOG.CSV: is the card still in? Press reset once it is.");
cardReady = false;
return;
}
writeLine(file, t, c);
file.close();
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
}
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
sensors.begin();
if (!rtc.begin()) Serial.println("no DS3231 at 0x68: check SDA and SCL");
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // once, to set the clock
cardReady = startCard();
if (cardReady) {
Serial.println("logging to TEMPLOG.CSV every 5 s (type d to print it)");
} else {
Serial.println("no SD card: check the card and CS on pin 10, then press reset. Readings go to the Serial Monitor only.");
}
}
void loop() {
while (Serial.available()) {
char c = Serial.read();
if (c == 'd' || c == 'D') dump();
}
if (millis() - lastLog >= INTERVAL_MS) {
lastLog = millis();
logReading();
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
startCard sketch.ino
This starts the SD card on chip select pin 10. If TEMPLOG.CSV does not exist yet, it creates it and writes the header line date,time,temp_c. It returns false if the card or file cannot be opened, so the rest of the sketch knows whether it can log.
// Start the card, and write the header if the file is new.
bool startCard() {
if (!SD.begin(SD_CS)) return false;
if (!SD.exists(FILE_NAME)) {
File file = SD.open(FILE_NAME, FILE_WRITE);
if (!file) return false;
file.println("date,time,temp_c");
file.close();
}
return true;
}writeLine sketch.ino
This formats one CSV line to wherever you point it: the Serial Monitor or an open file. It prints the date and time with leading zeros through print2, then the temperature with two decimals. Using one function for both places keeps the screen and the file identical.
// "2026-11-01,09:00:05,21.50" to wherever `out` points.
void writeLine(Print &out, const DateTime &t, float c) {
out.print(t.year());
out.print('-');
print2(out, t.month());
out.print('-');
print2(out, t.day());
out.print(',');
print2(out, t.hour());
out.print(':');
print2(out, t.minute());
out.print(':');
print2(out, t.second());
out.print(',');
out.println(c, 2);
}logReading sketch.ino
This takes one reading. It asks the DS18B20 for a temperature and reads the clock. If the sensor is disconnected, it says so and stops. Otherwise it prints the line, then opens the file, appends the line, closes it and blinks the LED. If the file cannot be opened, it reports that and stops logging.
void logReading() {
sensors.requestTemperatures();
float c = sensors.getTempCByIndex(0);
DateTime t = rtc.now();
if (c == DEVICE_DISCONNECTED_C) {
Serial.println("no DS18B20: check DQ on pin 2 and the 4.7k pull-up");
return;
}
writeLine(Serial, t, c);
if (!cardReady) return;
File file = SD.open(FILE_NAME, FILE_WRITE);
if (!file) {
Serial.println("could not write TEMPLOG.CSV: is the card still in? Press reset once it is.");
cardReady = false;
return;
}
writeLine(file, t, c);
file.close();
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
}loop sketch.ino
Each pass checks the Serial Monitor for a d and calls dump if it sees one. Then, once INTERVAL_MS has passed since the last log, it calls logReading.
void loop() {
while (Serial.available()) {
char c = Serial.read();
if (c == 'd' || c == 'D') dump();
}
if (millis() - lastLog >= INTERVAL_MS) {
lastLog = millis();
logReading();
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The Serial Monitor says no DS18B20 This project
Check DQ on pin 2 and the 4.7k resistor between DQ and 5 V. Without the pull-up, the one-wire sensor cannot be read. - The Serial Monitor says no SD card This project
Check CS on pin 10 and the SPI wires on pins 11, 12 and 13. Insert the card, then press reset, because the card is only started insetup. - The dates are wrong on a real module This project
A new DS3231 must be set once with thertc.adjustline insetup. Upload with it uncommented, then comment it out again and upload again. - SD.begin() fails. microSD card module
Check MOSI, MISO, SCK and CS wiring, and that CS matches the pin in your code. Use a card formatted FAT16 or FAT32. Many modules are 5 V logic with a level shifter, so check your board. - A file is empty or corrupted. microSD card module
Call file.close() or file.flush() after writing, before removing power. Use short 8.3 style file names. Do not open the same file twice at once. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
Questions and answers
How often does it log, and can I change it?
Every 5 seconds. Change INTERVAL_MS at the top of the sketch. At one line every 5 s the file grows by about 450 KB a day.
How do I open the log in Excel or Google Sheets?
TEMPLOG.CSV is plain comma-separated text with a date,time,temp_c header, so a spreadsheet opens it directly. In Mokxi, click the file in the card’s list to download it.
Why use a DS3231 instead of millis()?
millis() starts again from zero at every reset and knows no date. The DS3231 keeps the date and time on its coin cell. Set it once by uploading with the rtc.adjust() line uncommented, then comment it out again.
What happens if the SD card is missing or pulled out?
Readings still go to the Serial Monitor and the sketch says the card is missing. The file is closed after every line, so at most the line being written is lost. Put the card back and press reset to carry on in the same file.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.