SDカードにCSVを書き込む
後で分析できるよう読み取り値をファイルに追記します。
「SDカードにCSVを書き込む」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはArduino & IoT Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Arduino & IoT Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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,55The 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,humidityDon'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. ✅
よくある質問
「SDカードにCSVを書き込む」レッスンは無料ですか?
はい。「SDカードにCSVを書き込む」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。
「SDカードにCSVを書き込む」で何を学びますか?
後で分析できるよう読み取り値をファイルに追記します。 ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Arduino & IoT Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「SDカードにCSVを書き込む」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このArduino & IoT Academyレッスンでコードを書いて実行できますか?
はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- NTP/RTCで正確な時刻を取得する
- SDカードにCSVを書き込む
- アップロードをバッファリング・一括処理する
- 記録データを可視化する