이더넷 쉴드 또는 Micro SD 카드 홀더가 있는 어떠한 쉴드를 사용한다면, Micro SD 카드 모듈을 사용할 필요가 없습니다. 단지 쉴드의 Micro SD 카드 홀더에 Micro SD 카드를 삽입하기만 하면 됩니다.
아두이노 - 마이크로 SD 카드에서 파일을 열고 존재하지 않으면 생성하는 방법
아두이노 코드
사용 방법
마이크로 SD 카드를 마이크로 SD 카드 모듈에 삽입하세요
위의 배선도대로 마이크로 SD 카드 모듈과 아두이노 사이의 배선을 하세요
USB 케이블을 이용해 아두이노를 PC에 연결하세요
아두이노 IDE를 열고 올바른 보드와 포트를 선택하세요
아두이노 IDE에서 시리얼을 열기
아래 코드를 복사해서 아두이노 IDE에 붙여넣기하세요
/* * 이 아두이노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-micro-sd-card */#include <SD.h>#define PIN_SPI_CS 4File 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."));if (!SD.exists("arduino.txt")) {Serial.println(F("arduino.txt doesn't exist. Creating arduino.txt file..."));// 새 파일을 열어서 바로 닫음으로써 새 파일 생성 myFile = SD.open("arduino.txt", FILE_WRITE); myFile.close(); }// 파일이 생성되었는지 다시 확인if (SD.exists("arduino.txt"))Serial.println(F("arduino.txt exists on SD Card."));elseSerial.println(F("arduino.txt doesn't exist on SD Card."));}voidloop() {}
Arduino IDE에서 Upload 버튼을 클릭하여 아두이노에 코드를 업로드하세요
첫 실행에 대한 시리얼 모니터의 결과
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')
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
SD CARD INITIALIZED.
arduino.txt exists on SD Card.
Ln 11, Col 1
Arduino Uno on COM15
2
※ 주의:
시리얼 모니터를 열기 전에 첫 번째 업로드가 완료되기 전에는 첫 실행에서 시리얼 모니터의 출력을 볼 수 없을 수 있습니다.
모듈에서 마이크로 SD 카드를 분리하세요.
마이크로 SD 카드를 USB SD 카드 리더기에 삽입하세요.
USB SD 카드 리더기를 PC에 연결하세요.
파일이 있는지 없는지 확인하세요.
아두이노 - 마이크로 SD 카드에 파일로 데이터를 쓰고/읽는 방법
아래 코드는 다음을 수행합니다:
파일에 데이터를 쓰기
파일의 내용을 문자 단위로 읽고 시리얼 모니터에 출력하기
/* * 이 아두이노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-micro-sd-card */#include <SD.h>#define PIN_SPI_CS 4File 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("arduino.txt", FILE_WRITE);if (myFile) { myFile.println("Created by ArduinoGetStarted.com"); // Arduino에 줄 쓰기 myFile.println("Learn Arduino and SD Card"); // Arduino에 또 다른 줄 쓰기 myFile.close(); } else {Serial.print(F("SD Card: error on opening file arduino.txt")); }// 읽기 위해 파일 열기 myFile = SD.open("arduino.txt", 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 arduino.txt")); }}voidloop() {}
시리얼 모니터가 파일의 내용을 보여주었습니다.
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
Created by ArduinoGetStarted.com
Learn Arduino and SD Card
Ln 11, Col 1
Arduino Uno on COM15
2
※ 주의:
기본적으로 데이터는 파일 끝에 추가될 것입니다. 위의 코드로 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
ArduinoGetStarted.com에서 제작
아두이노와 SD 카드 배우기
ArduinoGetStarted.com에서 제작
아두이노와 SD 카드 배우기
Ln 11, Col 1
Arduino Uno on COM15
2
모듈에서 마이크로 SD 카드를 분리한 후 PC에서 내용을 확인할 수도 있습니다 (USB SD 카드 리더기가 필요합니다).
아두이노 - 마이크로 SD 카드에 있는 파일을 줄 단위로 읽는 방법
/* * 이 아두이노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-micro-sd-card */#include <SD.h>#define PIN_SPI_CS 4File 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("arduino.txt", FILE_WRITE);if (myFile) { myFile.println("Created by ArduinoGetStarted.com"); // Arduino에 한 줄 쓰기 myFile.println("Learn Arduino and SD Card"); // Arduino에 또 다른 줄 쓰기 myFile.close(); } else {Serial.print(F("SD Card: error on opening file arduino.txt")); }// 읽기용 파일 열기 myFile = SD.open("arduino.txt", FILE_READ);if (myFile) {int line_count = 0;while (myFile.available()) {char line[100]; // 최대는 100자, 필요한 경우 변경하십시오int line_length = myFile.readBytesUntil('\n', line, 100); // Micro SD 카드에서 한 줄씩 읽기 line_count++;Serial.print(F("Line "));Serial.print(line_count);Serial.print(F(": "));Serial.write(line, line_length); // 시리얼 모니터에 문자를 인쇄한다// \n 문자는 readBytesUntil 함수에 의해 이스케이프됩니다Serial.write('\n'); // 새 줄 문자를 인쇄한다 } myFile.close(); } else {Serial.print(F("SD Card: error on opening file arduino.txt")); }}voidloop() {}
시리얼 모니터에서의 결과
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.
Line 1: Created by ArduinoGetStarted.com
Line 2: Learn Arduino and SD Card
Ln 11, Col 1
Arduino Uno on COM15
2
※ 주의:
파일의 내용이 이전에 삭제되지 않은 경우 시리얼 모니터에서 더 많은 줄을 볼 수 있습니다.
아두이노 - 마이크로 SD 카드에 있는 파일을 덮어쓰는 방법
기본적으로 내용은 파일의 끝에 추가됩니다. 파일을 덮어쓰는 가장 간단한 방법은: 기존 파일을 삭제하고 같은 이름으로 새 파일을 생성하는 것입니다.
/* * 이 아두이노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-micro-sd-card */#include <SD.h>#define PIN_SPI_CS 4File 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."));SD.remove("arduino.txt"); // 파일이 있으면 삭제// 쓰기 위해 파일 열어 새 파일 생성 myFile = SD.open("arduino.txt", FILE_WRITE);if (myFile) { myFile.println("Created by ArduinoGetStarted.com"); // Arduino에 한 줄 쓰기 myFile.println("Learn Arduino and SD Card"); // Arduino에 또 다른 줄 쓰기 myFile.close(); } else {Serial.print(F("SD Card: error on opening file arduino.txt")); }// 읽기 위해 파일 열기 myFile = SD.open("arduino.txt", FILE_READ);if (myFile) {while (myFile.available()) {char ch = myFile.read(); // Micro SD Card에서 한 글자씩 읽기Serial.print(ch); // 시리얼 모니터에 글자 출력 } myFile.close(); } else {Serial.print(F("SD Card: error on opening file arduino.txt")); }}voidloop() {}
시리얼 모니터의 결과
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.
Created by ArduinoGetStarted.com
Learn Arduino and SD Card
Ln 11, Col 1
Arduino Uno on COM15
2
아두이노를 재부팅하십시오.
시리얼 모니터의 파일 내용이 추가되었는지 확인하십시오.
모듈에서 마이크로 SD 카드를 분리한 다음, PC에서 내용을 확인하기 위해 열 수도 있습니다 (USB SD 카드 리더기가 필요합니다).
동영상
비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.