ESP32 Thẻ SD

Trong hướng dẫn này, chúng ta sẽ khám phá cách sử dụng thẻ Micro SD Card với ESP32. Chúng ta sẽ tìm hiểu các chủ đề sau:

Linh Kiện Cần Thiết

1×mô-đun phát triển ESP-WROOM-32
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×Cáp USB Type-C
1×Thẻ Micro SD Card
1×Module Thẻ Micro SD Card
1×Dây Jumper
1×breadboard (bo mạch thí nghiệm)
1×Đầu Đọc Thẻ SD USB 3.0
1×(Khuyến nghị) Screw Terminal Expansion Board for ESP32
1×(Khuyến nghị) Breakout Expansion Board for ESP32
1×(Khuyến nghị) Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Module Thẻ Micro SD Card

Module Thẻ Micro SD Card có thể giao tiếp với ESP32 và có thể chứa một thẻ Micro SD Card. Nói cách khác, Module Thẻ Micro SD Card là cầu nối giữa ESP32 và thẻ Micro SD Card.

Sơ Đồ Chân

sơ đồ chân module thẻ micro thẻ SD

Module Thẻ Micro SD Card bao gồm 6 chân:

  • Chân VCC: kết nối với chân 5V của ESP32.
  • Chân GND: kết nối chân này với GND của ESP32.
  • Chân MISO: (Master In Slave Out) kết nối chân này với chân MISO của ESP32.
  • Chân MOSI: (Master Out Slave In) kết nối chân này với chân MOSI của ESP32.
  • Chân SCK: kết nối chân này với chân SCK của ESP32.
  • Chân SS: (Slave Select) kết nối chân này với chân được chỉ định trong code ESP32 làm chân SS.

Chuẩn Bị

  • Kết nối thẻ Micro SD Card với PC qua đầu đọc thẻ SD USB 3.0
  • Đảm bảo rằng thẻ Micro SD Card được định dạng FAT16 hoặc FAT32 (Google để tìm hiểu cách thực hiện)

Sơ Đồ Đấu Nối

sơ đồ đấu nối ESP32 module thẻ micro thẻ SD

This image is created using Fritzing. Click to enlarge image

Nếu bạn chưa rõ cách cấp nguồn cho ESP32 và các linh kiện khác, xem: Cách Cung Cấp Nguồn Điện Cho ESP32.

※ Lưu ý:

Nếu bạn sử dụng shield Ethernet hoặc bất kỳ shield nào có khe cắm thẻ Micro SD Card, bạn không cần sử dụng Module Thẻ Micro SD Card. Bạn chỉ cần chèn thẻ Micro SD Card vào khe cắm thẻ Micro SD Card trên shield.

ESP32 - Cách mở file trên thẻ Micro SD Card và tạo mới nếu chưa tồn tại

Code ESP32

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp32/esp32-sd-card */ #include <SD.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 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.")); 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 not if (SD.exists("/esp32.txt")) Serial.println(F("esp32.txt exists on SD Card.")); else Serial.println(F("esp32.txt doesn't exist on SD Card.")); } void loop() { }

Các Bước Thực Hiện

  • Nếu đây là lần đầu tiên bạn sử dụng ESP32, hãy xem ESP32 - Cài Đặt Phần Mềm.
  • Mở Arduino IDE trên PC của bạn.
  • Chọn đúng board ESP32 (ví dụ: ESP32 Dev Module) và cổng COM.
  • Chèn thẻ Micro SD Card vào module thẻ Micro SD Card
  • Thực hiện đấu nối giữa module thẻ Micro SD Card và ESP32 theo sơ đồ đấu nối ở trên
  • Kết nối ESP32 với PC qua cáp USB
  • Mở Serial Monitor trên Arduino IDE
  • Copy code ở trên và paste vào Arduino IDE
  • Click nút Upload trên Arduino IDE để upload code lên ESP32
  • Kết quả trên Serial Monitor cho lần chạy đầu tiên
COM6
Send
SD CARD INITIALIZED. esp32.txt doesn't exist. Creating esp32.txt file... esp32.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Kết quả trên Serial Monitor cho các lần chạy tiếp theo
COM6
Send
SD CARD INITIALIZED. esp32.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ Lưu ý:

Bạn có thể KHÔNG thấy kết quả trên Serial Monitor cho lần chạy đầu tiên nếu việc upload hoàn thành trước khi mở Serial Monitor.

  • Tháo thẻ Micro SD Card ra khỏi module
  • Chèn thẻ Micro SD Card vào đầu đọc thẻ SD USB
  • Kết nối đầu đọc thẻ SD USB với PC
  • Kiểm tra xem file có tồn tại hay không

ESP32 - Cách ghi/đọc dữ liệu từ/đến file trên thẻ Micro SD Card

Code dưới đây thực hiện:

  • Ghi dữ liệu vào file
  • Đọc nội dung của file từng ký tự một và in ra Serial Monitor
/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp32/esp32-sd-card */ #include <SD.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 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 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 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() { }
  • Serial Monitor hiển thị nội dung của file
COM6
Send
Created by newbiely.com Learn ESP32 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ Lưu ý:

Dữ liệu sẽ được thêm vào cuối file theo mặc định. Nếu bạn khởi động lại ESP32 với code ở trên, văn bản sẽ được thêm vào file một lần nữa ⇒ Serial Monitor sẽ hiển thị thêm dòng như dưới đây:

COM6
Send
Created by newbiely.com Learn ESP32 and SD Card Created by newbiely.com Learn ESP32 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Bạn cũng có thể tháo thẻ Micro SD Card ra khỏi module và mở nó trên PC để kiểm tra nội dung (cần đầu đọc thẻ SD USB)

ESP32 - Cách đọc file trên thẻ Micro SD Card từng dòng một

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp32/esp32-sd-card */ #include <SD.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 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 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 needed int 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 function Serial.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")); } } void loop() { }
  • Kết quả trên Serial Monitor
COM6
Send
SD CARD INITIALIZED. Line 1: Created by newbiely.com Line 2: Learn ESP32 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ Lưu ý:

Bạn có thể thấy thêm dòng trên Serial Monitor nếu nội dung của file không được xóa trước đó.

ESP32 - Cách ghi đè file trên thẻ Micro SD Card

Theo mặc định, nội dung sẽ được thêm vào cuối file. Cách đơn giản nhất để ghi đè file là: xóa file hiện tại và tạo file mới với cùng tên

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp32/esp32-sd-card */ #include <SD.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 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.")); 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 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() { }
  • Kết quả trên Serial Monitor
COM6
Send
SD CARD INITIALIZED. Created by newbiely.com Learn ESP32 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Khởi động lại ESP32
  • Kiểm tra xem nội dung của file trên Serial Monitor có được thêm vào hay không.

Bạn cũng có thể tháo thẻ Micro SD Card ra khỏi module và mở nó trên PC để kiểm tra nội dung (cần đầu đọc thẻ SD USB)

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.

Bình Luận