ESP32 - 타임스탬프를 포함한 데이터를 SD 카드에 기록하기 | ESP32 - Log Data with Timestamp to SD Card

이 가이드에서는 ESP32를 사용하여 타임스탬프가 있는 데이터를 Micro SD 카드에 로깅하는 과정을 탐색할 것입니다. 구체적으로 다음 주제를 다룰 것입니다:

시간 정보는 RTC 모듈에서 추출되며, 수집된 데이터와 함께 마이크로 SD 카드에 기록됩니다.

마이크로 SD 카드에 기록된 데이터는 다음과 같은 다양한 정보를 포함할 수 있습니다:

간단함을 위해, 이 튜토리얼은 예제 데이터셋으로서 두 개의 아날로그 핀에서 값을 읽는 과정을 보여줄 것입니다. 제공된 코드는 필요에 따라 다양한 유형의 데이터를 수용할 수 있도록 쉽게 조정될 수 있습니다.

ESP32 Log to Micro SD Card

준비물

1×ESP-WROOM-32 Dev Module 쿠팡 | Amazon
1×USB Cable Type-C 쿠팡 | 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×(Recommended) ESP32 Screw Terminal Adapter 쿠팡 | Amazon
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

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

마이크로 SD 카드 모듈과 RTC 모듈, 그리고 이들의 핀아웃, 기능성, 프로그래밍에 익숙하지 않으신가요? 아래에서 이 주제들에 대한 자세한 튜토리얼을 탐색해보세요:

선연결

ESP32 Micro SD Card Module Wiring Diagram

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

ESP32 및 다른 구성 요소에 전원을 공급하는 방법에 대해 잘 알지 못하는 경우, 다음 튜토리얼에서 안내를 찾을 수 있습니다: ESP32 전원 공급 방법.

※ NOTE THAT:

이더넷 실드나 마이크로 SD 카드 홀더가 있는 어떤 실드를 사용한다면, 마이크로 SD 카드 모듈을 사용할 필요가 없습니다. 실드에 있는 마이크로 SD 카드 홀더에 마이크로 SD 카드를 삽입하기만 하면 됩니다.

ESP32 - 타임스탬프가 포함된 데이터를 Micro SD 카드에 기록하기

/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp32/esp32-log-data-with-timestamp-to-sd-card */ #include <SD.h> #include <RTClib.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 #define FILE_NAME "/log.txt" RTC_DS3231 rtc; File myFile; void setup() { Serial.begin(9600); // SETUP RTC MODULE if (!rtc.begin()) { while (1) { Serial.println(F("RTC module is NOT found")); delay(1000); } } if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } 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: Issue encountered while attempting to open the file ")); Serial.println(FILE_NAME); } delay(2000); // delay 2 seconds }

사용 방법

ESP32를 처음 사용하는 경우, Arduino IDE에서 ESP32 환경 설정 방법을 참고하세요.

위 이미지처럼 배선하세요.

ESP32 보드를 마이크로 USB 케이블을 통해 PC에 연결하세요.

PC에서 Arduino IDE를 엽니다.

올바른 ESP32 보드(예: ESP32 Dev Module)와 COM 포트를 선택하세요.

Micro SD 카드가 FAT16 또는 FAT32로 포맷되었는지 확인하세요(Google에서 검색)

위의 코드를 복사하여 Arduino IDE로 엽니다.

Arduino IDE에서 Upload 버튼을 클릭하여 ESP32에 코드를 업로드하세요.

시리얼 모니터에서 결과를 확인하세요.

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 파일을 열면 아래와 같습니다
ESP32 log to Micro SD Card with time information

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

/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp32/esp32-log-data-with-timestamp-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 #define FILE_NAME "/log.txt" File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } 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: Issue encountered while attempting to open the file ")); Serial.println(FILE_NAME); } } void loop() { }

ESP32 - 여러 파일에 데이터 로깅

로그를 단일 파일에 기록하면 시간이 지남에 따라 파일 크기가 커져서 확인하기 어렵습니다. 아래 코드는 로그 파일을 여러 개로 기록할 것입니다:

  • 하루에 한 파일
  • 파일 이름은 날짜 정보입니다: YYYYMMDD.txt
/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp32/esp32-log-data-with-timestamp-to-sd-card */ #include <SD.h> #include <RTClib.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 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()) { while (1) { Serial.println(F("RTC module is NOT found")); delay(1000); } } if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } 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[1] = (year / 1000) + '0'; filename[2] = ((year % 1000) / 100) + '0'; filename[3] = ((year % 100) / 10) + '0'; filename[4] = (year % 10) + '0'; filename[5] = (month / 10) + '0'; filename[6] = (month % 10) + '0'; filename[7] = (day / 10) + '0'; filename[8] = (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: Issue encountered while attempting to open the file ")); Serial.println(filename); } delay(2000); // delay 2 seconds }

긴 달리기 후에, 만약 당신이:

  • 마이크로 SD 카드를 마이크로 SD 카드 모듈에서 분리하세요.
  • 마이크로 SD 카드를 USB SD 카드 리더기에 삽입하세요.
  • USB SD 카드 리더기를 PC에 연결하세요.
  • 다음과 같은 파일들을 볼 수 있습니다:
ESP32 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!