Introduction
CSV format helps save data in a simple table form. It is easy to read and use later in programs like Excel.
CSV format helps save data in a simple table form. It is easy to read and use later in programs like Excel.
Serial.print(value1); Serial.print(","); Serial.println(value2);
Use commas to separate values in one line.
Use println to end the line and start a new row.
Serial.print(25); Serial.print(","); Serial.println(60);
Serial.print("Time"); Serial.print(","); Serial.println("Temp");
Serial.print(millis()); Serial.print(","); Serial.println(sensorValue);
This program writes a CSV file on an SD card with time and temperature data. It logs 3 rows with a header.
#include <SD.h> const int chipSelect = 10; void setup() { Serial.begin(9600); while (!Serial) {} if (!SD.begin(chipSelect)) { Serial.println("SD card failed or not present"); return; } File dataFile = SD.open("datalog.csv", FILE_WRITE); if (dataFile) { dataFile.println("Time,Temperature"); for (int i = 0; i < 3; i++) { int temp = 20 + i; // example temperature dataFile.print(millis()); dataFile.print(","); dataFile.println(temp); delay(1000); } dataFile.close(); Serial.println("Data logged to datalog.csv"); } else { Serial.println("Error opening datalog.csv"); } } void loop() { // nothing here }
Always include a header row to label your CSV columns.
Use commas to separate each value in a row.
Close the file after writing to save data properly.
CSV format stores data in rows and columns separated by commas.
Use Serial.print and Serial.println to create CSV lines.
Logging data in CSV makes it easy to analyze later.
float temp = 25.5;
int humidity = 60;
Serial.print(temp);
Serial.print(",");
Serial.println(humidity);
Serial.println(temperature);
Serial.print(",");
Serial.println(humidity);
Pass the test and this pattern moves unknown