ESP32 - SD 카드에 변수 쓰기 | ESP32 - Write Variable to SD Card

이 튜토리얼에서는 ESP32를 사용하여 다양한 타입의 변수들을 Micro SD 카드에 쓰는 과정을 탐구할 것입니다. 특히 다음과 같은 주제들을 다룰 것입니다:

이 튜토리얼은 다양한 종류의 변수를 마이크로 SD 카드에 저장할 수 있는 종합적인 가이드를 제공하여, 유연한 데이터 저장 및 검색을 가능하게 합니다. 마이크로 SD 카드에서 키-값 쌍을 읽고 정수, 부동 소수점, 문자열 형식으로 변환하는 방법을 배우려면 ESP32 - SD 카드에서 설정 읽기 튜토리얼을 참조하세요.

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

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

Micro SD Card Module, 핀아웃, 기능 및 프로그래밍에 익숙하지 않으신가요? ESP32 - Micro SD Card 튜토리얼에서 이에 대해 배워보세요.

선연결

ESP32 Micro SD Card Module Wiring Diagram

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

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

※ NOTE THAT:

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

ESP32 - Micro SD 카드에 변수를 파일로 작성하는 방법

아래 코드는 다음 작업을 수행합니다:

  • 정수 변수를 마이크로 SD 카드에 쓰기
  • 실수 변수를 마이크로 SD 카드에 쓰기
  • 문자열 변수를 마이크로 SD 카드에 쓰기
  • 문자 배열을 마이크로 SD 카드에 쓰기
  • 바이트 배열을 마이크로 SD 카드에 쓰기
/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp32/esp32-write-variable-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 File myFile; int myInt = -52; float myFloat = -12.7; String myString = "HELLO"; char myCharArray[] = "esp32io.com"; byte myByteArray[] = {'1', '2', '3', '4', '5'}; 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.")); Serial.println(F("--------------------")); 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(myInt); // write int variable to SD card in line myFile.println(myFloat); // write float variable to SD card in line myFile.println(myString); // write String variable to SD card in line myFile.println(myCharArray); // write char array to SD card in line myFile.write(myByteArray, 5); myFile.write("\n"); // new line for (int i = 0; i < 5; i++) { myFile.write(myByteArray[i]); // new line if (i < 4) myFile.write(","); // comma } myFile.write("\n"); // new line 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 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 esp32.txt")); } } void loop() { }

사용 방법

  • ESP32를 처음 사용하는 경우, Arduino IDE에서 ESP32 환경 설정 방법을 참고하세요.
  • 위 이미지와 같이 배선하세요.
  • ESP32 보드를 마이크로 USB 케이블을 사용하여 PC에 연결하세요.
  • PC에서 Arduino IDE를 엽니다.
  • 올바른 ESP32 보드(예: ESP32 Dev Module)와 COM 포트를 선택하세요.
  • Micro SD 카드가 FAT16 또는 FAT32로 포맷되었는지 확인하세요 (구글에서 검색)
  • 위 코드를 복사하여 Arduino IDE로 엽니다
  • Arduino IDE에서 Upload 버튼을 클릭하여 코드를 ESP32에 업로드하세요
  • 시리얼 모니터에서 결과를 확인하세요.
COM6
Send
SD CARD INITIALIZED. -------------------- -52 -12.70 HELLO newbiely.kr 12345 1,2,3,4,5
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • 마이크로 SD 카드를 마이크로 SD 카드 모듈에서 분리하세요.
  • 마이크로 SD 카드를 USB SD 카드 리더기에 삽입하세요.
  • USB SD 카드 리더기를 PC에 연결하세요.
  • PC에서 esp32.txt 파일을 열어보세요, 아래와 같아 보입니다.
ESP32 writes variable to Micro SD Card

ESP32 - 마이크로 SD 카드에 key-value를 파일로 작성하는 방법

/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp32/esp32-write-variable-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 File myFile; int myInt = -52; float myFloat = -12.7; String myString = "HELLO"; char myCharArray[] = "esp32io.com"; byte myByteArray[] = {'1', '2', '3', '4', '5'}; 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.")); Serial.println(F("--------------------")); 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.print("myInt="); // write key to SD card myFile.println(myInt); // write int variable to SD card in line myFile.print("myFloat="); // write key to SD card myFile.println(myFloat); // write float variable to SD card in line myFile.print("myString="); // write key to SD card myFile.println(myString); // write String variable to SD card in line myFile.print("myCharArray="); // write key to SD card myFile.println(myCharArray); // write char array to SD card in line myFile.print("myByteArray="); // write key to SD card myFile.write(myByteArray, 5); myFile.write("\n"); // new line myFile.print("myByteArray2="); // write key to SD card for (int i = 0; i < 5; i++) { myFile.write(myByteArray[i]); // new line if (i < 4) myFile.write(","); // comma } myFile.write("\n"); // new line 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 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 esp32.txt")); } } void loop() { }

사용 방법

  • 만약 이게 ESP32를 처음 사용하는 경우라면, Arduino IDE에서 ESP32 환경 설정하는 법을 참조하세요.
  • 위 이미지대로 배선하세요.
  • ESP32 보드를 마이크로 USB 케이블로 PC에 연결하세요.
  • PC에서 Arduino IDE를 엽니다.
  • 올바른 ESP32 보드(예: ESP32 Dev Module)와 COM 포트를 선택하세요.
  • 위 코드를 복사하고 Arduino IDE로 엽니다.
  • Arduino IDE에서 Upload 버튼을 클릭하여 ESP32에 코드를 업로드하세요.
  • 시리얼 모니터에서 결과를 확인하세요.
COM6
Send
SD CARD INITIALIZED. -------------------- myInt=-52 myFloat=-12.70 myString=HELLO myCharArray=newbiely.kr myByteArray=12345 myByteArray2=1,2,3,4,5
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • 마이크로 SD 카드 모듈에서 마이크로 SD 카드를 분리하세요.
  • 마이크로 SD 카드를 USB SD 카드 리더기에 삽입하세요.
  • USB SD 카드 리더기를 PC에 연결하세요.
  • PC에서 esp32.txt 파일을 열면, 아래와 같이 보입니다.
ESP32 writes variable to Micro SD Card

동영상

비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, 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!