0Pricing
Arduino & IoT Academy · Lesson

Write CSV to an SD Card

Append readings to a file for later analysis.

Write CSV to an SD Card is a free Arduino & IoT Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why an SD Card?

An SD card gives your device cheap, roomy storage that survives power loss. It is perfect for keeping a long history of logged readings. 💾

Why CSV?

CSV means comma-separated values: plain text where each line is a record and commas split the fields. Spreadsheets open it instantly.

What a Row Looks Like

One log entry per line keeps things tidy. A row might pair a timestamp with a reading so each value has clear meaning later.

2026-06-25 14:03,24.7,55

The SD Module

An SD card module connects over the SPI bus. You wire its MOSI, MISO, SCK, and CS pins to the matching pins on your board.

Starting the Card

Before reading or writing you must initialize the card and tell the library which pin is chip select. If it fails, the card or wiring is off.

if (!SD.begin(10)) { return; }

Opening a File

You open a file by name, choosing a mode. Use FILE_WRITE to add data; it creates the file if it does not exist yet.

File f = SD.open("log.csv", FILE_WRITE);

Appending, Not Overwriting

FILE_WRITE seeks to the end, so each new line is appended. Your old entries stay safe and the history keeps growing.

Writing a Line

You print fields and commas just like Serial output. End the row with println so the next entry starts on a fresh line.

f.print(temp);
f.print(",");
f.println(humidity);

Always Close the File

Calling close() flushes your data and frees the file. Skip it and your last writes may never reach the card.

f.close();

A Header Row

Write a header line once when the file is new, naming each column. Spreadsheets then label your charts automatically.

time,temp,humidity

Don't Write Too Often

Each write costs power and time. For battery nodes, log every few seconds or minutes rather than every loop to keep the card happy.

Quick Check

What happens if you forget to close the file after writing?

Recap

Wire an SD module over SPI, open a CSV file in append mode, print comma-separated rows, and always close it. Now your readings are saved for good. ✅

Frequently asked questions

Is the “Write CSV to an SD Card” lesson free?

Yes — the full text of “Write CSV to an SD Card” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.

What will I learn in “Write CSV to an SD Card”?

Append readings to a file for later analysis. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Arduino & IoT Academy?

No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Write CSV to an SD Card” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Arduino & IoT Academy lesson?

Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Get Real Time with NTP/RTC
  2. Write CSV to an SD Card
  3. Buffer & Batch Uploads
  4. Visualize Logged Data
← Back to Arduino & IoT Academy