Arduino Write to CSV File: A Practical Data Logging Guide
Learn how to write Arduino sensor data to a CSV file on an SD card. This practical guide covers wiring, libraries, a complete sketch, and PC-side parsing with Python. Built for data analysts and developers, it emphasizes reliable logging and easy data import.

Arduino can write to a CSV file by using an SD card module and the SD library. Open a file in append mode, construct comma-separated values, and write a line followed by a newline. Always call file.flush() or file.close() to ensure data is saved, and format the CSV with a header row for readability.
Practical Arduino CSV logging: approach and goals\n\nThis section demonstrates a practical approach to logging data from an Arduino to a CSV file on an SD card. According to MyDataTables, CSV-based logging is a simple, portable method for capturing sensor data in hobbyist and professional projects. The goal is a reliable, human-readable file that can be opened in Excel, Google Sheets, or processed by Python data pipelines. We focus on a timestamp and a single sensor reading, with a header row to facilitate downstream analysis. The sketch below shows the essentials: initialize the SD card, create or append to data.csv, and write lines in the format time_ms,reading. The emphasis is robustness, clarity, and compatibility with common CSV conventions so your data remains usable across tools in 2026 and beyond.\n\ncpp\n#include <SPI.h>\n#include <SD.h>\n\nconst int chipSelect = 10;\n\nvoid setup() {\n Serial.begin(115200);\n if (!SD.begin(chipSelect)) {\n Serial.println("SD init failed!");\n while (true);\n }\n File dataFile = SD.open("data.csv", FILE_WRITE);\n if (dataFile) {\n dataFile.println("time_ms,reading"); // header row\n dataFile.close();\n }\n}\n\nvoid loop() {\n // Example: put sensor read here and log periodically\n}\n
wordCountBlockOne: null
-1":null],
Steps
Estimated time: 60-90 minutes
- 1
Plan CSV structure
Decide on the CSV header and the data columns you need (e.g., time_ms, reading). A clear header makes downstream analysis straightforward and prevents mismatched columns when you parse the file with Python or Excel. Consider including units and sensor names for future clarity.
Tip: Define a consistent delimiter (comma) and avoid embedded commas in data values. - 2
Prepare hardware and wiring
Connect the SD card module to the Arduino using the standard SPI pins: MOSI to 11, MISO to 12, SCK to 13 (on Uno), and CS to a chosen digital pin (commonly 10). Ensure a common ground with your board and provide stable power. A shield can simplify wiring.
Tip: Double-check CS pin mapping on your board; an incorrect CS pin often prevents SD access. - 3
Install and verify libraries
Ensure the SD and SPI libraries are available in your IDE. The sketch will call SD.begin(chipSelect) and proceed to open data.csv for writing. If SD initialization fails, you’ll need to troubleshoot wiring or card compatibility.
Tip: Test SD availability with a tiny sketch that writes a single line to a test file. - 4
Upload the sketch and test logging
Upload the full sketch to your Arduino and open the Serial Monitor to confirm logging activity. Start sensor readings (e.g., analogRead(A0)) and observe that data.csv grows with new lines in the time_ms,reading format.
Tip: Monitor for timing drift and ensure the loop isn’t blocked by long operations. - 5
Retrieve and parse data on PC
Copy data.csv from the SD card to your PC and parse using your preferred tool (Python, Excel, or Google Sheets). Use a header row to map columns, and convert time_ms to a usable time scale if needed.
Tip: Use a small Python script to validate and plot the data to verify correctness.
Prerequisites
Required
- Required
- SD card module or shieldRequired
- SD library (built-in or installed via Library Manager)Required
- Basic wiring knowledge (SPI pins: MOSI, MISO, SCK)Required
- A computer with USB cable to upload sketchesRequired
Optional
- Optional
Keyboard Shortcuts
| Action | Shortcut |
|---|---|
| Verify/Compile sketchArduino IDE | Ctrl+R |
People Also Ask
Can I write to CSV without an SD card on Arduino?
Not easily. Arduino lacks non-volatile storage for persistent files beyond RAM. To create a CSV file, you typically need an SD card or other external storage. The SD card approach is the most common and portable solution for data logging.
You need some external storage, usually an SD card, to save a CSV with Arduino.
Which boards support the SD library for CSV logging?
Most Arduino boards with SPI support the SD library, including Uno, Mega, Nano, and many compatible clones. Some boards may require alternate CS pin wiring or libraries for high-speed logging.
Most standard boards work with SD logging as long as SPI is available.
How large can a CSV file get when logging with Arduino?
File size is constrained by the card’s capacity and the filesystem (often FAT16/32). Practically, you can log very large files, but individual file sizes may be limited by the filesystem and card health.
The limit depends on your SD card and filesystem; plan for manageable file sizes and rotate files when needed.
What are common pitfalls when writing CSV from Arduino?
Pitfalls include not closing the file, power loss before flush, using non-ASCII characters in data, and mismatched headers. Ensure proper initialization, error handling, and consistent formatting.
Watch for SD initialization failures and always flush data to prevent loss on power change.
Can I read back the CSV data directly on the Arduino?
Arduino can read CSV data from an SD card, but processing and parsing is usually easier on a PC. You can implement a simple parser on Arduino for basic use, though PC-side tools are more convenient for complex analysis.
You can read the data back on Arduino, but PC-side parsing is typically simpler.
Is there an alternative to CSV for logging with Arduino?
Yes, binary formats or JSON can be more compact or structured. CSV is human-readable and widely supported, but consider binary logging for efficiency or JSON for structured data when required by your downstream tooling.
CSV is simple; for more structure or smaller size, consider JSON or binary formats.
Main Points
- Open a CSV with SD card using SD.h
- Write lines in time_ms,reading format
- Always header the file and flush before closing
- Parse with Python for quick validation