Arduino Ghi dữ liệu có dấu thời gian vào thẻ SD

Trong hướng dẫn này, chúng ta sẽ học cách ghi log có dấu thời gian vào thẻ nhớ Micro SD bằng Arduino. Cụ thể, chúng ta sẽ học:

Thông tin thời gian được lấy từ một mô-đun RTC và được ghi vào thẻ Micro SD cùng với dữ liệu.

Dữ liệu được ghi vào thẻ nhớ Micro SD có thể là bất kỳ thứ gì. Ví dụ:

Để đơn giản, bài hướng dẫn này đọc giá trị từ hai chân analog làm ví dụ về dữ liệu. Bạn có thể dễ dàng điều chỉnh mã để phù hợp với bất kỳ loại dữ liệu nào.

ghi nhật ký Arduino vào thẻ nhớ microsd

Phần cứng cần chuẩn bị

1×Arduino Uno R3
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Micro SD Card
1×Micro SD Card Module
1×(Tùy chọn) MicroSD to SD Memory Card Adapter
1×Real-Time Clock DS3231 Module
1×CR2032 battery
1×dây jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino Uno
1×(Khuyến nghị) Breadboard Shield for Arduino Uno
1×(Khuyến nghị) Enclosure for Arduino Uno
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V3 Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về mô-đun thẻ nhớ Micro SD và mô-đun RTC

Nếu bạn chưa biết về Mô-đun thẻ Micro SD và mô-đun RTC (bố trí chân, cách hoạt động, cách lập trình ...), hãy tìm hiểu về chúng trong các hướng dẫn sau đây:

Sơ đồ đấu dây

sơ đồ đấu nối mô-đun thẻ micro sd cho Arduino

This image is created using Fritzing. Click to enlarge image

※ Lưu ý:

Nếu bạn sử dụng một Ethernet shield hoặc bất kỳ shield nào có khe cắm thẻ Micro SD, bạn sẽ không cần dùng Mô-đun thẻ Micro SD. Bạn chỉ cần chèn thẻ Micro SD vào khe cắm thẻ Micro SD trên shield.

Arduino - Ghi dữ liệu có dấu thời gian vào thẻ nhớ Micro SD

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-log-data-with-timestamp-to-sd-card */ #include <SD.h> #include <RTClib.h> #define PIN_SPI_CS 4 #define FILE_NAME "log.txt" RTC_DS3231 rtc; File myFile; void setup() { Serial.begin(9600); // SETUP RTC MODULE if (!rtc.begin()) { Serial.println(F("Couldn't find RTC")); while (1); } if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); Serial.println(F("--------------------")); } void loop() { // open file for writing myFile = SD.open(FILE_NAME, FILE_WRITE); if (myFile) { Serial.println(F("Writing log to SD Card")); // write timestamp DateTime now = rtc.now(); myFile.print(now.year(), DEC); myFile.print('-'); myFile.print(now.month(), DEC); myFile.print('-'); myFile.print(now.day(), DEC); myFile.print(' '); myFile.print(now.hour(), DEC); myFile.print(':'); myFile.print(now.minute(), DEC); myFile.print(':'); myFile.print(now.second(), DEC); myFile.print(" "); // delimiter between timestamp and data // read data int analog_1 = analogRead(A0); int analog_2 = analogRead(A1); // write data myFile.print("analog_1 = "); myFile.print(analog_1); myFile.print(", "); // delimiter between data myFile.print("analog_2 = "); myFile.print(analog_2); myFile.write("\n"); // new line myFile.close(); } else { Serial.print(F("SD Card: error on opening file ")); Serial.println(FILE_NAME); } delay(2000); // delay 2 seconds }

Hướng dẫn từng bước

  • Đảm bảo thẻ Micro SD được định dạng FAT16 hoặc FAT32 (tìm kiếm trên Google)
  • Sao chép đoạn mã ở trên và mở bằng Arduino IDE
  • Nhấp vào nút Tải lên trên Arduino IDE để tải mã lên Arduino
  • Xem kết quả trên Serial Monitor.
COM6
Send
SD CARD INITIALIZED. -------------------- Writing log to SD Card Writing log to SD Card Writing log to SD Card Writing log to SD Card Writing log to SD Card Writing log to SD Card Writing log to SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Rút thẻ Micro SD khỏi module thẻ Micro SD
  • Gắn thẻ Micro SD vào đầu đọc thẻ SD USB
  • Kết nối đầu đọc thẻ SD USB với máy tính
  • Mở tệp log.txt trên máy tính của bạn, nó trông như dưới đây
ghi nhật ký từ Arduino vào thẻ micro sd kèm theo thông tin thời gian.

Nếu bạn không có đầu đọc thẻ nhớ SD USB, bạn có thể kiểm tra nội dung của tệp nhật ký bằng cách chạy mã Arduino dưới đây.

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-log-data-with-timestamp-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 #define FILE_NAME "log.txt" File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); // open file for reading myFile = SD.open(FILE_NAME, FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // read characters one by one from Micro SD Card Serial.print(ch); // print the character to Serial Monitor } myFile.close(); } else { Serial.print(F("SD Card: error on opening file ")); Serial.println(FILE_NAME); } } void loop() { }

Arduino - Ghi dữ liệu vào nhiều tệp

Ghi log vào một tệp duy nhất sẽ làm kích thước tệp tăng lên theo thời gian và khiến việc kiểm tra trở nên khó khăn. Đoạn mã dưới đây sẽ ghi log vào nhiều tệp:

  • Một tệp cho mỗi ngày
  • Tên tệp là thông tin ngàyL YYYYMMDD.txt
/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-log-data-with-timestamp-to-sd-card */ #include <SD.h> #include <RTClib.h> #define PIN_SPI_CS 4 RTC_DS3231 rtc; File myFile; char filename[] = "yyyymmdd.txt"; // filename (without extension) should not exceed 8 chars void setup() { Serial.begin(9600); // SETUP RTC MODULE if (!rtc.begin()) { Serial.println(F("Couldn't find RTC")); while (1); } if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); Serial.println(F("--------------------")); } void loop() { DateTime now = rtc.now(); int year = now.year(); int month = now.month(); int day = now.day(); // update filename filename[0] = (year / 1000) + '0'; filename[1] = ((year % 1000) / 100) + '0'; filename[2] = ((year % 100) / 10) + '0'; filename[3] = (year % 10) + '0'; filename[4] = (month / 10) + '0'; filename[5] = (month % 10) + '0'; filename[6] = (day / 10) + '0'; filename[7] = (day % 10) + '0'; // open file for writing myFile = SD.open(filename, FILE_WRITE); if (myFile) { Serial.println(F("Writing log to SD Card")); // write timestamp myFile.print(now.year(), DEC); myFile.print('-'); myFile.print(now.month(), DEC); myFile.print('-'); myFile.print(now.day(), DEC); myFile.print(' '); myFile.print(now.hour(), DEC); myFile.print(':'); myFile.print(now.minute(), DEC); myFile.print(':'); myFile.print(now.second(), DEC); myFile.print(" "); // delimiter between timestamp and data // read data int analog_1 = analogRead(A0); int analog_2 = analogRead(A1); // write data myFile.print("analog_1 = "); myFile.print(analog_1); myFile.print(", "); // delimiter between data myFile.print("analog_2 = "); myFile.print(analog_2); myFile.write("\n"); // new line myFile.close(); } else { Serial.print(F("SD Card: error on opening file ")); Serial.println(filename); } delay(2000); // delay 2 seconds }

Sau một quãng chạy dài, nếu bạn:

  • Rút thẻ Micro SD khỏi mô-đun thẻ Micro SD
  • Chèn thẻ Micro SD vào đầu đọc thẻ SD USB
  • Kết nối đầu đọc thẻ SD USB với PC
  • Bạn sẽ thấy các tệp tin như sau:
ghi nhật ký Arduino vào thẻ nhớ micro sd với nhiều tệp tin

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.