Lesson 18: Data Logging and Reading Sensors Over TimeNew-age
A single sensor reading tells you what is happening now. A log of readings over time tells you what is changing — and change is where the interesting knowledge lives. A temperature that rises every afternoon, a light level that falls as a battery dies, a robot’s distance readings that show it slowing down: all of these are visible only in a log. This lesson is about recording data over time, storing it, and reading the story it tells.
You will log to the computer over serial, store data on a microSD card for a robot that runs away from the computer, and learn the two skills that turn a pile of numbers into knowledge: graphing, and looking for patterns rather than single values.
- Data logging — recording measurements over time for later analysis.
- microSD card — a small removable card that stores data, readable by a computer.
- CSV — comma-separated values: one reading per line, values separated by commas.
- Sampling interval — how often a reading is taken, such as once every 10 seconds.
- Timestamp — a note of when a reading was taken.
- Trend — a pattern of change over time, such as a steady rise.
- Outlier — a single reading far from the rest, often caused by a glitch.
- Graph — a visual plot of readings against time, which makes trends visible.
Logging over serial
The simplest log is the one from Lesson 12: one CSV line per reading over serial, with a timestamp and the values, copied from the Serial Monitor into a spreadsheet. It costs nothing and it works for any project that stays connected to a computer. The rules are the same: log on a timer, not every loop; include a timestamp; and use a format a spreadsheet can open — commas between values, one reading per line.
The weakness is the cable. A robot that drives away from the computer cannot log over serial, and a station that runs overnight needs a computer left on all night. For those jobs, the log must live on the robot itself, which is where the microSD card comes in.
Logging to a microSD card
A microSD card module connects to the microcontroller over SPI and stores data as a file, usually in CSV format. The SD library opens the file, appends a line, and closes it — and closing the file after each write is important, because data that is only in the buffer is lost if the power fails. Write the same CSV lines you would send over serial, and read the card in a computer afterwards to graph them.
Cards have limits: they draw noticeable current when writing, they wear out after many writes, and a card that is removed while the file is open can be corrupted. Log at a sensible rate — once every 10 seconds, not every loop — and always close the file. For a robot that must log for days, a card is still the cheapest and simplest storage available.
Reading the log: trends and outliers
A log is not knowledge until it is read. Graph the readings against time — a spreadsheet will do it in a minute — and look for trends: a steady rise, a daily cycle, a sudden change. A temperature that rises every afternoon and falls every night is a daily cycle; a battery voltage that falls steadily is a trend; a single reading of 200 °C among readings of 23 °C is an outlier, almost certainly a glitch, and should be ignored, not explained.
Compare the log with what you expected. If the robot’s distance readings show it slowing down over an hour, is the battery dying, or is the floor changing? The log cannot answer that on its own, but it can tell you where to look. The skill is to treat the log as evidence: it shows what happened, and your job is to explain why.
Sampling: how often is enough?
The sampling interval decides what the log can show. Sample every 10 seconds and you will see the temperature change through the day; sample every hour and you will miss it. But sampling too often wastes storage and power, and for a slow measurement it adds nothing — a temperature does not change in a second. The rule is to sample at least twice as fast as the fastest change you want to see: if the temperature changes over an hour, sample every few minutes.
Worked example — a day in the log
A weather station logs temperature every 10 minutes for a day: 144 readings. Graphed, they show a smooth curve: 18 °C at 6 a.m., rising to 26 °C at 3 p.m., falling to 19 °C by midnight. One reading, at 11:20 a.m., says 45 °C. The log’s story is clear: the daily cycle is real, and the 45 °C reading is an outlier — probably sunlight falling directly on the sensor for a few minutes.
Common mistakes
- Logging every loop, filling the card with thousands of useless lines.
- Forgetting to close the file after writing, so a power cut loses the data.
- Treating an outlier as real data. One wild reading is usually a glitch, not a discovery.
- Logging without a timestamp. A list of numbers with no times is a list, not a log.
Discussion questions
- A log shows a temperature spike at midday. Is it the weather, the sun on the sensor, or a glitch? How would you decide?
- Sampling every second gives more data than sampling every minute. When is more data worse?
- Your robot logs its distance readings and you find it slowed down over an hour. What are two possible causes, and how would the log help you tell them apart?
Practice questions
- Why log on a timer rather than every loop? Answer: Every loop would produce thousands of lines and fill the storage. A timer — every 10 seconds, say — gives enough data to show change and keeps the log readable. Explanation: Sample on a timer.
- Why must a file be closed after writing to an SD card? Answer: Because data sits in a buffer until the file is closed. A power cut before closing loses the buffered data. Explanation: Close the file.
- What is an outlier, and how should you treat one? Answer: A single reading far from the rest, usually a glitch. Flag it and investigate, but do not let it drive a conclusion. Explanation: One wild reading is not a discovery.
- Why does a log need a timestamp? Answer: Without times, the readings are just a list of numbers. A timestamp turns them into a record of change over time. Explanation: Time makes data a log.
- A temperature changes over an hour. How often should you sample it? Answer: At least every few minutes — twice as fast as the fastest change you want to see. Every second would waste storage; every hour would miss the change. Explanation: Sample faster than the change.
✅ Check your understanding
Quick recall check, generated from this lesson’s key terms. It is practice only and is not graded.