Arduino UNO R4 Thẻ Micro SD Card

Trong hướng dẫn này, chúng ta sẽ học cách sử dụng thẻ Micro SD Card với Arduino UNO R4. Chúng ta sẽ đề cập đến các chi tiết sau:

Arduino UNO R4 micro thẻ SD

Linh Kiện Cần Thiết

1×Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima
1×(Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi
1×Arduino UNO R4 Minima (Thay thế)
1×Cáp USB Type-C
1×Thẻ Micro SD Card
1×Module Micro SD Card
1×Dây Jumper
1×Đầu đọc thẻ SD USB 3.0
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Module Micro SD Card

Module Micro SD Card kết nối với Arduino UNO R4 và chứa thẻ Micro SD Card. Điều này có nghĩa là Module Micro SD Card hoạt động như một cầu nối giữa Arduino UNO R4 và thẻ Micro SD Card.

Sơ đồ chân

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

Module Micro SD Card có 6 chân:

  • Kết nối chân VCC với chân 5V trên Arduino UNO R4.
  • Kết nối chân GND với chân GND trên Arduino UNO R4.
  • Kết nối chân MISO với chân MISO trên Arduino UNO R4.
  • Kết nối chân MOSI với chân MOSI trên Arduino UNO R4.
  • Kết nối chân SCK với chân SCK trên Arduino UNO R4.
  • Kết nối chân SS với chân được định danh là chân SS trong code Arduino UNO R4.

Chuẩn bị

  • Cắm thẻ Micro SD Card vào máy tính của bạn bằng đầu đọc thẻ SD USB 3.0.
  • Kiểm tra xem thẻ Micro SD Card đã được định dạng là FAT16 hoặc FAT32 chưa. Bạn có thể tìm hiểu cách thực hiện điều này trên mạng.

Sơ đồ đấu nối

Arduino UNO R4 micro thẻ SD module sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

※ Lưu ý:

Nếu bạn có Ethernet shield 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 Micro SD Card. Chỉ cần cắm thẻ Micro SD Card vào khe cắm thẻ Micro SD Card trên shield.

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

Code Arduino UNO R4

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

Thực hiện theo các hướng dẫn từng bước sau:

  • Nếu đây là lần đầu tiên bạn sử dụng Arduino Uno R4 WiFi/Minima, hãy tham khảo hướng dẫn Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Cắm thẻ Micro SD Card vào module Micro SD Card
  • Theo sơ đồ đấu nối để kết nối module Micro SD Card với Arduino UNO R4
  • Kết nối bo mạch Arduino Uno R4 với máy tính của bạn bằng cáp USB.
  • Khởi động Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 WiFi) và cổng COM.
  • Mở Serial Monitor trong Arduino IDE
  • Sao chép code được cung cấp và dán vào Arduino IDE
/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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/arduino-uno-r4/arduino-uno-r4-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 // The Arduino UNO R4 pin connected to the CS pin of SDcard module File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD Card is either missing or has failed!")); while (1); // don't do anything more: } Serial.println(F("SD Card is ready")); if (!SD.exists("arduino.txt")) { Serial.println(F("arduino.txt doesn't exist. Creating arduino.txt file...")); // create a new file by opening a new file and immediately close it myFile = SD.open("arduino.txt", FILE_WRITE); myFile.close(); } // recheck if file is created or not if (SD.exists("arduino.txt")) Serial.println(F("arduino.txt exists on SD Card.")); else Serial.println(F("arduino.txt doesn't exist on SD Card.")); } void loop() { }
  • Nhấn nút Upload trong Arduino IDE để chuyển code lên Arduino UNO R4.
  • Kiểm tra Serial Monitor để xem kết quả sau lần upload đầu tiên.
COM6
Send
SD Card is ready arduino.txt doesn't exist. Creating arduino.txt file... arduino.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Kết quả hiển thị trên Serial Monitor cho các lần thử tiếp theo
COM6
Send
SD Card is ready arduino.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ Lưu ý:

Bạn có thể không thấy bất kỳ kết quả nào trên Serial Monitor nếu bạn mở nó sau khi upload lần đầu tiên.

  • Tháo thẻ Micro SD Card khỏi module
  • Đặt thẻ Micro SD Card vào đầu đọc thẻ SD USB
  • Kết nối đầu đọc thẻ SD USB với máy tính của bạn
  • Kiểm tra xem file có tồn tại hay không

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

Code này thực hiện các hành động sau:

  • Lưu thông tin vào file
  • Tải và hiển thị từng ký tự của file lên Serial Monitor
/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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/arduino-uno-r4/arduino-uno-r4-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 // The Arduino UNO R4 pin connected to the CS pin of SDcard module File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD Card is either missing or has failed!")); while (1); // don't do anything more: } Serial.println(F("SD Card is ready")); // open file for writing myFile = SD.open("arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to Arduino myFile.println("Learn Arduino and SD Card"); // write another line to Arduino myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } // open file for reading myFile = SD.open("arduino.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("Error: Unable to open file arduino.txt")); } } void loop() { }
  • Serial Monitor hiển thị nội dung của file.
COM6
Send
Created by newbiely.com Learn Arduino UNO R4 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ Lưu ý:

Theo mặc định, dữ liệu được thêm vào cuối file. Nếu bạn khởi động lại Arduino UNO R4 với code đã cho, văn bản sẽ được thêm lại vào file. Điều này có nghĩa là bạn sẽ thấy thêm các dòng như thế này trên Serial Monitor:

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

Bạn cũng có thể tháo thẻ Micro SD Card khỏi module và sử dụng đầu đọc thẻ SD USB để xem các file trên máy tính của bạn.

Arduino UNO R4 - Cách đọc file trên thẻ Micro SD Card từng dòng

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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/arduino-uno-r4/arduino-uno-r4-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 // The Arduino UNO R4 pin connected to the CS pin of SDcard module File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD Card is either missing or has failed!")); while (1); // don't do anything more: } Serial.println(F("SD Card is ready")); // open file for writing myFile = SD.open("arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to Arduino myFile.println("Learn Arduino and SD Card"); // write another line to Arduino myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } // open file for reading myFile = SD.open("arduino.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("Error: Unable to open file arduino.txt")); } } void loop() { }
  • Kết quả hiển thị trên Serial Monitor
COM6
Send
SD Card is ready Line 1: Created by newbiely.com Line 2: Learn Arduino UNO R4 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ Lưu ý:

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

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

Thông thường, nội dung được thêm vào cuối file. Để thay thế file, trước tiên hãy xóa file cũ và sau đó tạo file mới với cùng tên.

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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/arduino-uno-r4/arduino-uno-r4-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 // The Arduino UNO R4 pin connected to the CS pin of SDcard module File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD Card is either missing or has failed!")); while (1); // don't do anything more: } Serial.println(F("SD Card is ready")); SD.remove("arduino.txt"); // delete the file if existed // create new file by opening file for writing myFile = SD.open("arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to Arduino myFile.println("Learn Arduino and SD Card"); // write another line to Arduino myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } // open file for reading myFile = SD.open("arduino.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("Error: Unable to open file arduino.txt")); } } void loop() { }
  • Kết quả trên Serial Monitor
COM6
Send
SD Card is ready Created by newbiely.com Learn Arduino UNO R4 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Khởi động lại Arduino UNO R4
  • Kiểm tra xem nội dung file trong Serial Monitor đã được thêm vào chưa.

Bạn cũng có thể tháo thẻ Micro SD Card khỏi thiết bị và sử dụng đầu đọc thẻ SD USB để xem nội dung trên máy tính của bạn.

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

Bài hướng dẫn liên quan