Arduino UNO R4 - Micro SD Card

In this guide, we will learn how to use a Micro SD Card with the Arduino UNO R4. We will cover the following details:

Arduino UNO R4 Micro SD Card

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Micro SD Card
1×Micro SD Card Module
1×Jumper Wires
1×USB 3.0 SD Card Reader
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Micro SD Card Module

The Micro SD Card Module connects with the Arduino UNO R4 and holds a Micro SD Card. This means the Micro SD Card Module acts as a link between the Arduino UNO R4 and the Micro SD Card.

Pinout

Micro SD Card Module Pinout

The Micro SD Card Module has 6 pins:

  • Connect the VCC pin to the 5V pin on the Arduino UN to R4.
  • Connect the GND pin to the GND on the Arduino UNO R4.
  • Connect the MISO pin to the MISO pin on the Arduino UNO R4.
  • Connect the MOSI pin to the MOSI pin on the Arduino UNO R4.
  • Connect the SCK pin to the SCK pin on the Arduino UNO R4.
  • Connect the SS pin to the pin identified as the SS pin in the Arduino UNO R4 code.

Preparation

  • Plug the Micro SD Card into your computer using a USB 3.0 SD Card Reader.
  • Check that the Micro SD Card is formatted as FAT16 or FAT32. You can search online to find out how to do this.

Wiring Diagram

The wiring diagram between Arduino UNO R4 Micro SD Card Module

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

If you have an Ethernet shield or any shield with a Micro SD Card Holder, you don't need to use the Micro SD Card Module. Just put the Micro SD Card into the Micro SD Card Holder on the shield.

Arduino UNO R4 - How to open a file on Micro SD Card and create if not existed

Arduino UNO R4 Code

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Insert the Micro SD Card into the Micro SD Card module
  • Follow the wiring diagram to connect the Micro SD Card module to the Arduino UNO R4
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Open the Serial Monitor in the Arduino IDE
  • Copy the provided code and paste it into the Arduino IDE
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/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() { }
  • Press the Upload button in the Arduino IDE to transfer the code to the Arduino UNO R4.
  • Check the Serial Monitor for the outcome after the first upload.
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  
  • The outcome displayed on the Serial Monitor for subsequent attempts
COM6
Send
SD Card is ready arduino.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

You might not see any output on the Serial Monitor if you open it after uploading for the first time.

  • Remove the Micro SD Card from the module
  • Place the Micro SD Card into a USB SD Card reader
  • Attach the USB SD Card reader to your computer
  • Verify whether the file is there or not

Arduino UNO R4 - How to write/read data to/from a file on Micro SD Card

This code performs the following actions:

  • Save information in a file
  • Load and display each character of a file one by one on the Serial Monitor
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/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() { }
  • The Serial Monitor displayed the file's contents.
COM6
Send
Created by newbiely.com Learn Arduino UNO R4 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

By default, the data is added to the end of the file. If you restart the Arduino UNO R4 with the given code, the text will be added again to the file. This means you will see additional lines like these on the 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  

You can also remove the Micro SD Card from the module and use a USB SD Card reader to view the files on your computer.

Arduino UNO R4 - How to read a file on Micro SD Card line-by-line

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/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() { }
  • The outcome displayed on the 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  

※ NOTE THAT:

If you don’t delete the file content before, you might see additional lines on the Serial Monitor.

Arduino UNO R4 - How to overwrite a file on Micro SD Card

Normally, the content adds to the end of the file. To replace a file, first delete the old file and then make a new one with the same name.

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/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() { }
  • The output on the 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  
  • Restart Arduino UNO R4
  • Verify if the file's content in Serial Monitor has been added to.

You can also remove the Micro SD Card from the unit and use a USB SD Card reader to view its contents on your computer.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • 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!