아두이노 - SD 카드에 타임스탬프와 함께 데이터 기록하기 | Arduino - Log Data with Timestamp to SD Card

이 튜토리얼에서는 Arduino를 사용하여 타임스탬프와 함께 로그를 마이크로 SD 카드에 작성하는 방법을 배우게 됩니다. 구체적으로, 우리는 다음을 배울 것입니다:

시간 정보는 RTC 모듈에서 가져와 데이터와 함께 마이크로 SD 카드에 기록됩니다.

마이크로 SD 카드에 기록된 데이터는 무엇이든 될 수 있습니다. 예를 들면:

간단히 말해서, 이 튜토리얼은 데이터의 예로서 두 개의 아날로그 핀에서 값을 읽습니다. 어떤 종류의 데이터에도 쉽게 코드를 적용할 수 있습니다.

Arduino Log to Micro SD Card

준비물

1×Arduino Uno Amazon
1×USB 2.0 cable type A/B 쿠팡 | Amazon
1×Micro SD Card Amazon
1×Micro SD Card Module 쿠팡 | Amazon
1×USB 3.0 SD Card Reader Amazon
1×Real-Time Clock DS3231 Module Amazon
1×CR2032 battery Amazon
1×Jumper Wires Amazon
1×(Optional) 9V Power Adapter for Arduino Amazon
1×(Recommended) Screw Terminal Block Shield for Arduino Uno 쿠팡 | Amazon
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno Amazon
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

마이크로 SD 카드 모듈과 RTC 모듈에 관하여

Micro SD 카드 모듈과 RTC 모듈(핀 배치, 작동 방식, 프로그래밍 방법 등)에 대해 잘 모른다면, 다음 튜토리얼에서 알아보세요:

선연결

Arduino Micro SD Card Module Wiring Diagram

이 이미지는 Fritzing을 사용하여 만들어졌습니다. 이미지를 확대하려면 클릭하세요.

※ NOTE THAT:

이더넷 쉴드나 마이크로 SD 카드 홀더가 있는 쉴드를 사용하는 경우, 마이크로 SD 카드 모듈을 사용할 필요가 없습니다. 단지 쉴드 위의 마이크로 SD 카드 홀더에 마이크로 SD 카드를 삽입하면 됩니다.

아두이노 - 마이크로 SD 카드에 타임스탬프와 함께 데이터 기록하기

/* * 이 Arduino 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/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); // RTC 모듈 설정 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); // 더 이상 아무것도 하지 않음: } Serial.println(F("SD CARD INITIALIZED.")); Serial.println(F("--------------------")); } void loop() { // 쓰기 위해 파일 열기 myFile = SD.open(FILE_NAME, FILE_WRITE); if (myFile) { Serial.println(F("Writing log to SD Card")); // 타임스탬프 쓰기 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(" "); // 타임스탬프와 데이터 사이의 구분자 // 데이터 읽기 int analog_1 = analogRead(A0); int analog_2 = analogRead(A1); // 데이터 쓰기 myFile.print("analog_1 = "); myFile.print(analog_1); myFile.print(", "); // 데이터 사이의 구분자 myFile.print("analog_2 = "); myFile.print(analog_2); myFile.write("\n"); // 새로운 줄 myFile.close(); } else { Serial.print(F("SD Card: error on opening file ")); Serial.println(FILE_NAME); } delay(2000); // 2초 지연 }

사용 방법

  • Micro SD 카드가 FAT16 또는 FAT32로 포맷되어 있는지 확인하세요 (Google에서 검색)
  • 위의 코드를 복사한 다음 Arduino IDE로 엽니다
  • Arduino IDE에서 Upload 버튼을 클릭하여 코드를 Arduino에 업로드하세요
  • 결과를 시리얼 모니터에서 확인하세요.
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  
  • 마이크로 SD 카드 모듈에서 마이크로 SD 카드를 분리하세요
  • 마이크로 SD 카드를 USB SD 카드 리더기에 삽입하세요
  • USB SD 카드 리더기를 PC에 연결하세요
  • PC에서 log.txt 파일을 열면 아래와 같습니다
Arduino log to Micro SD Card with time information

USB SD 카드 리더기가 없다면, 아래의 아두이노 코드를 실행하여 로그 파일의 내용을 확인할 수 있습니다.

/* * 이 Arduino 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/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); // 아무것도 더 하지 않음: } Serial.println(F("SD CARD INITIALIZED.")); // 파일을 읽기 위해 열기 myFile = SD.open(FILE_NAME, FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // Micro SD 카드에서 한 문자씩 읽기 Serial.print(ch); // 시리얼 모니터에 문자를 출력 } myFile.close(); } else { Serial.print(F("SD Card: error on opening file ")); Serial.println(FILE_NAME); } } void loop() { }

아두이노 - 여러 파일에 데이터 기록

단일 파일에 로그를 작성하면 시간이 지남에 따라 파일 크기가 커지고 확인하기 어렵습니다. 아래 코드는 로그 파일을 여러 개로 작성합니다:

  • 하루에 파일 하나
  • 파일 이름은 날짜 정보입니다: YYYYMMDD.txt
/* * 이 Arduino 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/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"; // 파일명(확장자 제외)은 8자를 초과하면 안됩니다. void setup() { Serial.begin(9600); // RTC 모듈 설정 if (!rtc.begin()) { Serial.println(F("Couldn't find RTC")); while (1); } if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD 카드 실패, 또는 없음!")); while (1); // 더 이상 아무 것도 하지 않음: } Serial.println(F("SD 카드 초기화됨.")); Serial.println(F("--------------------")); } void loop() { DateTime now = rtc.now(); int year = now.year(); int month = now.month(); int day = now.day(); // 파일명 업데이트 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'; // 쓰기를 위한 파일 열기 myFile = SD.open(filename, FILE_WRITE); if (myFile) { Serial.println(F("SD 카드에 로그 작성 중")); // 타임스탬프 작성 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(" "); // 타임스탬프와 데이터 사이 구분자 // 데이터 읽기 int analog_1 = analogRead(A0); int analog_2 = analogRead(A1); // 데이터 작성 myFile.print("analog_1 = "); myFile.print(analog_1); myFile.print(", "); // 데이터 사이 구분자 myFile.print("analog_2 = "); myFile.print(analog_2); myFile.write("\n"); // 새로운 줄 myFile.close(); } else { Serial.print(F("SD 카드: 파일 열기 오류 ")); Serial.println(filename); } delay(2000); // 2초 지연 }

오래 달린 후, 만약 당신이:

  • 마이크로 SD 카드를 마이크로 SD 카드 모듈에서 분리하세요.
  • 마이크로 SD 카드를 USB SD 카드 리더기에 삽입하세요.
  • USB SD 카드 리더기를 PC에 연결하세요.
  • 파일이 다음과 같이 보일 것입니다:
Arduino log to Micro SD Card multiple files

동영상

비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!