이더넷 쉴드나 마이크로 SD 카드 홀더가 있는 쉴드를 사용하는 경우, 마이크로 SD 카드 모듈을 사용할 필요가 없습니다. 단지 쉴드 위의 마이크로 SD 카드 홀더에 마이크로 SD 카드를 삽입하면 됩니다.
아두이노 - 마이크로 SD 카드에 타임스탬프와 함께 데이터 기록하기
/* * 이 아두이노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * 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;voidsetup() {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("--------------------"));}voidloop() {// 쓰기 위해 파일 열기 myFile = SD.open(FILE_NAME, FILE_WRITE);if (myFile) {Serial.println(F("Writing log to SD Card"));// 타임스탬프 쓰기DateTimenow = 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에 업로드하세요
결과를 시리얼 모니터에서 확인하세요.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
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
Ln 11, Col 1
Arduino Uno on COM15
2
마이크로 SD 카드 모듈에서 마이크로 SD 카드를 분리하세요
마이크로 SD 카드를 USB SD 카드 리더기에 삽입하세요
USB SD 카드 리더기를 PC에 연결하세요
PC에서 log.txt 파일을 열면 아래와 같습니다
USB SD 카드 리더기가 없다면, 아래의 아두이노 코드를 실행하여 로그 파일의 내용을 확인할 수 있습니다.
/* * 이 아두이노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * 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;voidsetup() {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); }}voidloop() {}
아두이노 - 여러 파일에 데이터 기록
단일 파일에 로그를 작성하면 시간이 지남에 따라 파일 크기가 커지고 확인하기 어렵습니다. 아래 코드는 로그 파일을 여러 개로 작성합니다:
하루에 파일 하나
파일 이름은 날짜 정보입니다: YYYYMMDD.txt
/* * 이 아두이노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-log-data-with-timestamp-to-sd-card */#include <SD.h>#include <RTClib.h>#define PIN_SPI_CS 4RTC_DS3231 rtc;File myFile;char filename[] = "yyyymmdd.txt"; // 파일명(확장자 제외)은 8자를 초과하면 안됩니다.voidsetup() {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("--------------------"));}voidloop() {DateTimenow = rtc.now();intyear = now.year();intmonth = now.month();intday = 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에 연결하세요.
파일이 다음과 같이 보일 것입니다:
동영상
비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.