0Pricing
Arduino & IoT Academy · บทเรียน

เขียน CSV ลงการ์ด SD

เพิ่มค่าที่อ่านได้ต่อท้ายไฟล์เพื่อวิเคราะห์ภายหลัง

เขียน CSV ลงการ์ด SD เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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,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. ✅

คำถามที่พบบ่อย

บทเรียน “เขียน CSV ลงการ์ด SD” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เขียน CSV ลงการ์ด SD” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เขียน CSV ลงการ์ด SD”

เพิ่มค่าที่อ่านได้ต่อท้ายไฟล์เพื่อวิเคราะห์ภายหลัง คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “เขียน CSV ลงการ์ด SD” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม

ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. รับเวลาจริงด้วย NTP/RTC
  2. เขียน CSV ลงการ์ด SD
  3. บัฟเฟอร์และอัปโหลดเป็นชุด
  4. แสดงภาพข้อมูลที่บันทึกไว้
← กลับไปที่ Arduino & IoT Academy