ESP32 및 다른 구성 요소에 전원을 공급하는 방법에 대해 잘 알지 못하는 경우, 다음 튜토리얼에서 안내를 찾을 수 있습니다: ESP32 전원 공급 방법.
※ 주의:
이더넷 실드나 마이크로 SD 카드 홀더가 있는 실드를 사용하는 경우, 마이크로 SD 카드 모듈을 사용할 필요가 없습니다. 그저 마이크로 SD 카드를 실드에 있는 마이크로 SD 카드 홀더에 삽입하기만 하면 됩니다.
ESP32 - 마이크로 SD 카드에서 파일을 여는 방법 및 존재하지 않는 경우 생성하기
ESP32 코드
/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp32/esp32-sd-card */#include <SD.h>#define PIN_SPI_CS 5 // The ESP32 pin GPIO5File myFile;voidsetup() {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."));if (!SD.exists("/esp32.txt")) {Serial.println(F("esp32.txt doesn't exist. Creating esp32.txt file..."));// create a new file by opening a new file and immediately close it myFile = SD.open("/esp32.txt", FILE_WRITE); myFile.close(); }// recheck if file is created or notif (SD.exists("/esp32.txt"))Serial.println(F("esp32.txt exists on SD Card."));elseSerial.println(F("esp32.txt doesn't exist on SD Card."));}voidloop() {}
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
SD CARD INITIALIZED.
esp32.txt exists on SD Card.
Ln 11, Col 1
ESP32 Dev Module on COM15
2
※ 주의:
시리얼 모니터를 열기 전에 첫 번째 업로드를 실행할 때 첫 실행에서 시리얼 모니터의 출력을 볼 수 없을 수 있습니다.
모듈에서 마이크로 SD 카드를 분리하세요.
마이크로 SD 카드를 USB SD 카드 리더기에 삽입하세요.
USB SD 카드 리더기를 PC에 연결하세요.
파일이 존재하는지 여부를 확인하세요.
ESP32 - 마이크로 SD 카드에 파일을 쓰고/읽는 방법
아래 코드는 다음과 같은 기능을 합니다:
파일에 데이터 쓰기
파일의 내용을 문자 단위로 읽고 시리얼 모니터에 출력하기
/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp32/esp32-sd-card */#include <SD.h>#define PIN_SPI_CS 5 // The ESP32 pin GPIO5File myFile;voidsetup() {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 writing myFile = SD.open("/esp32.txt", FILE_WRITE);if (myFile) { myFile.println("Created by esp32io.com"); // write a line to esp32.txt myFile.println("Learn ESP32 and SD Card"); // write another line to esp32.txt myFile.close(); } else {Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); }// open file for reading myFile = SD.open("/esp32.txt", FILE_READ);if (myFile) {while (myFile.available()) {char ch = myFile.read(); // read characters one by one from Micro SD CardSerial.print(ch); // print the character to Serial Monitor } myFile.close(); } else {Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); }}voidloop() {}
시리얼 모니터가 파일의 내용을 보여주었습니다.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Created by newbiely.kr
Learn ESP32 and SD Card
Ln 11, Col 1
ESP32 Dev Module on COM15
2
※ 주의:
기본적으로 데이터는 파일 끝에 추가됩니다. 위의 코드로 ESP32를 재부팅하면 텍스트가 파일에 다시 추가됩니다 ⇒ 시리얼 모니터는 아래와 같이 더 많은 줄을 표시합니다:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
newbiely.kr에 의해 생성됨
ESP32 및 SD 카드 배우기
newbiely.kr에 의해 생성됨
ESP32 및 SD 카드 배우기
Ln 11, Col 1
ESP32 Dev Module on COM15
2
모듈에서 마이크로 SD 카드를 분리하고 PC에서 내용을 확인할 수도 있습니다 (USB SD 카드 리더기가 필요합니다).
ESP32 - 마이크로 SD 카드에 있는 파일을 줄 단위로 읽는 방법
/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp32/esp32-sd-card */#include <SD.h>#define PIN_SPI_CS 5 // The ESP32 pin GPIO5File myFile;voidsetup() {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 writing myFile = SD.open("/esp32.txt", FILE_WRITE);if (myFile) { myFile.println("Created by esp32io.com"); // write a line to esp32.txt myFile.println("Learn ESP32 and SD Card"); // write another line to esp32.txt myFile.close(); } else {Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); }// open file for reading myFile = SD.open("/esp32.txt", FILE_READ);if (myFile) {int line_count = 0;while (myFile.available()) {char line[100]; // maximum is 100 characters, change it if neededint line_length = myFile.readBytesUntil('\n', line, 100); // read line-by-line from Micro SD Card line_count++;Serial.print(F("Line "));Serial.print(line_count);Serial.print(F(": "));Serial.write(line, line_length); // print the character to Serial Monitor// \n character is escaped by readBytesUntil functionSerial.write('\n'); // print a new line charactor } myFile.close(); } else {Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); }}voidloop() {}
시리얼 모니터의 결과
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
SD CARD INITIALIZED.
Line 1: Created by newbiely.kr
Line 2: Learn ESP32 and SD Card
Ln 11, Col 1
ESP32 Dev Module on COM15
2
※ 주의:
파일의 내용을 이전에 삭제하지 않았다면 시리얼 모니터에서 더 많은 줄을 볼 수 있습니다.
ESP32 - Micro SD 카드의 파일을 덮어쓰는 방법
기본적으로, 내용은 파일의 맨 끝에 추가됩니다. 파일을 덮어쓰는 가장 간단한 방법은: 기존 파일을 삭제하고 같은 이름의 새 파일을 생성하는 것입니다.
/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp32/esp32-sd-card */#include <SD.h>#define PIN_SPI_CS 5 // The ESP32 pin GPIO5File myFile;voidsetup() {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."));SD.remove("/esp32.txt"); // delete the file if existed// create new file by opening file for writing myFile = SD.open("/esp32.txt", FILE_WRITE);if (myFile) { myFile.println("Created by esp32io.com"); // write a line to esp32.txt myFile.println("Learn ESP32 and SD Card"); // write another line to esp32.txt myFile.close(); } else {Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); }// open file for reading myFile = SD.open("/esp32.txt", FILE_READ);if (myFile) {while (myFile.available()) {char ch = myFile.read(); // read characters one by one from Micro SD CardSerial.print(ch); // print the character to Serial Monitor } myFile.close(); } else {Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); }}voidloop() {}
시리얼 모니터에서의 결과
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
SD CARD INITIALIZED.
Created by newbiely.kr
Learn ESP32 and SD Card
Ln 11, Col 1
ESP32 Dev Module on COM15
2
ESP32 재부팅
시리얼 모니터의 파일 내용이 추가되었는지 확인하세요.
모듈에서 마이크로 SD 카드를 분리한 다음, PC에서 내용을 확인하기 위해 열 수도 있습니다(USB SD 카드 리더기가 필요합니다).
동영상
비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.