Arduino Nano 33 IoT - SD Card

This guide will show you how to use a Micro SD Card with the Arduino Nano 33 IoT. We will look at these topics:

Arduino Nano 33 IoT Micro SD Card Module

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Micro SD Card
1×Micro SD Card Module
1×Jumper Wires
1×Breadboard
1×Optionally, MicroSD to SD Memory Card Adapter
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

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 works with the Arduino Nano 33 IoT and is designed to hold a Micro SD Card. Simply put, the module connects the Arduino Nano 33 IoT to the Micro SD Card.

Pinout

Micro SD Card Module Pinout

The Micro SD card module comes with six pins.

  • VCC pin: connect to the Arduino Nano 33 IoT's 5V pin.
  • GND pin: connect to the Arduino Nano 33 IoT's ground (GND) pin.
  • MISO pin (Master In Slave Out): connect to the Arduino Nano 33 IoT's MISO pin.
  • MOSI pin (Master Out Slave In): connect to the Arduino Nano 33 IoT's MOSI pin.
  • SCK pin: connect to the Arduino Nano 33 IoT's SCK pin.
  • SS pin (Slave Select): connect to the pin set as SS in the Arduino Nano 33 IoT code.

Preparation

  • Plug the micro SD card into your computer using a USB 3.0 SD card reader.
  • Make sure the micro SD card is set to FAT16 or FAT32 format (look it up online if you need help).

Wiring Diagram

  • This diagram shows how to connect the wires when using the USB port to power the Arduino Nano 33 IoT board.
The wiring diagram between Arduino Nano and 33 IoT Micro SD Card Module

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram for powering the Arduino Nano 33 IoT board using the Vin pin.
The wiring diagram between Arduino Nano and 33 IoT Micro SD Card Module external power

This image is created using Fritzing. Click to enlarge image

Arduino Nano 33 IoT - How to open a file on Micro SD Card and create if not existed

Arduino Nano 33 IoT Code

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 // The Arduino Nano 33 IoT pin connected to the CS pin of SD card module 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("/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() { }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Insert the Micro SD Card into the Micro SD Card module.
  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Open the Serial Monitor in the Arduino IDE.
  • Copy the code from above and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano 33 IoT.
  • Check the Serial Monitor to see the results from the first run.
COM6
Send
SD CARD INITIALIZED. arduino.txt doesn't exist. Creating arduino.txt file... arduino.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • The Serial Monitor will display the results in the next tests.
COM6
Send
SD CARD INITIALIZED. 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 the first time because you uploaded your code before opening it.

  • Remove the Micro SD Card from the device
  • Put the Micro SD Card into a USB SD Card reader
  • Plug the USB SD Card reader into your computer
  • See if the file is there or not

Arduino Nano 33 IoT - How to write/read data to/from a file on Micro SD Card

Here's what the code does:

  • Save information into a file
  • Open the file, read it one letter at a time, and show it on the Serial Monitor
/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 // The Arduino Nano 33 IoT pin connected to the CS pin of SD card module 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("/arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to arduino.txt myFile.println("Learn Arduino Nano 33 IoT and SD Card"); // write another line to arduino.txt myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the 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("SD Card: Issue encountered while attempting to open the file arduino.txt")); } } void loop() { }
  • The Serial Monitor shows what is inside the file.
COM6
Send
Created by newbiely.com Learn Arduino Nano 33 IoT and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

By default, data is added to the end of the file. If you restart the Arduino Nano 33 IoT with the code above, the text will be added again, and the Serial Monitor will show extra lines like this:

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

You can also remove the Micro SD Card from the module and open it on your computer to see the files (you need a USB SD Card reader).

Arduino Nano 33 IoT - How to read a file on Micro SD Card line-by-line

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 // The Arduino Nano 33 IoT pin connected to the CS pin of SD card module 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("/arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to arduino.txt myFile.println("Learn Arduino Nano 33 IoT and SD Card"); // write another line to arduino.txt myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the 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("SD Card: Issue encountered while attempting to open the file arduino.txt")); } } void loop() { }
  • The answer is shown on the Serial Monitor.
COM6
Send
SD CARD INITIALIZED. Line 1: Created by newbiely.com Line 2: Learn Arduino Nano 33 IoT and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

If you don't erase the file content first, you might see extra lines on the Serial Monitor.

Arduino Nano 33 IoT - How to overwrite a file on Micro SD Card

Normally, new data is added to the end of the file. The easiest way to replace a file is to remove the old file and create a new one with the same name.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 // The Arduino Nano 33 IoT pin connected to the CS pin of SD card module 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("/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.txt myFile.println("Learn Arduino Nano 33 IoT and SD Card"); // write another line to arduino.txt myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the 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("SD Card: Issue encountered while attempting to open the file arduino.txt")); } } void loop() { }
  • The result is shown on the Serial Monitor.
COM6
Send
SD CARD INITIALIZED. Created by newbiely.com Learn Arduino Nano 33 IoT and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Restart the Arduino Nano 33 IoT.
  • Make sure the file's data is added to the Serial Monitor.

You can remove the micro SD card from the module and view it on your computer. You'll need a USB card reader for that.

Video Tutorial

Function References

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!