Micro SD Card | Arduino Tutorial

  • TUTORIALS
  • HARDWARE & TOOLS
  • REFERENCES
  • FAQs
  • ABOUT US
Home Tutorials

SENSORS/ACTUATORS

  • Arduino - Software Installization
  • Arduino - Hardware Preparation
  • Arduino - Hello World
  • Arduino - Code Structure
  • Arduino - Serial Monitor
  • Arduino - Serial Plotter
  • Arduino - LED - Blink
  • Arduino - LED - Blink Without Delay
  • Arduino - Blink multiple LED
  • Arduino - LED - Fade
  • Arduino - RGB LED
  • Arduino - Traffic Light
  • Arduino - Button
  • Arduino - Button - Debounce
  • Arduino - Button - Long Press Short Press
  • Arduino multiple Button
  • Arduino - Switch
  • Arduino - Limit Switch
  • Arduino - DIP Switch
  • Arduino - Button - LED
  • Arduino - Button - Relay
  • Arduino - Button Toggle LED
  • Arduino - Button Toggle Relay
  • Arduino - Button - Piezo Buzzer
  • Arduino - Button - Servo Motor
  • Arduino - Potentiometer
  • Arduino - Potentiometer fade LED
  • Arduino - Potentiometer Triggers LED
  • Arduino - Potentiometer Triggers Relay
  • Arduino - Potentiometer Triggers Piezo Buzzer
  • Arduino - Potentiometer Triggers Servo Motor
  • Arduino - Rotary Encoder
  • Arduino - Rotary Encoder LED
  • Arduino - Rotary Encoder - Servo Motor
  • Arduino - Piezo Buzzer
  • Arduino - Buzzer
  • Arduino - Motor
  • Arduino - DC Motor
  • Arduino - DC Motor - Limit Switch
  • Arduino - Servo Motor
  • Arduino - MG996R
  • Arduino - Servo Motor controlled by Potentiometer
  • Arduino - Light Sensor
  • Arduino - LDR Module
  • Arduino - Light Sensor Triggers LED
  • Arduino - Light Sensor Triggers Relay
  • Arduino - Light Sensor Triggers Servo Motor
  • Arduino - Ultrasonic Sensor
  • Arduino - Ultrasonic Sensor - LED
  • Arduino - Ultrasonic Sensor - Relay
  • Arduino - Ultrasonic Sensor - Piezo Buzzer
  • Arduino - Ultrasonic Sensor - Servo Motor
  • Arduino - Ultrasonic Sensor - LCD
  • Arduino - Ultrasonic Sensor - OLED
  • Arduino - Motion Sensor
  • Arduino - Motion Sensor - LED
  • Arduino - Motion Sensor - Relay
  • Arduino - Motion Sensor - Piezo Buzzer
  • Arduino - Motion Sensor - Servo Motor
  • Arduino - Motion Sensor - MP3 Player
  • Arduino - Motion Sensor - LED Strip
  • Arduino - Relay
  • Arduino - Relay Shield
  • Arduino - 2-Channel Relay Module
  • Arduino - 4-Channel Relay Module
  • Arduino - Controls Fan
  • Arduino - Controls Heating Element
  • Arduino - Car
  • Arduino - Actuator
  • Arduino - Actuator with Feedback
  • Arduino - Joystick
  • Arduino - Joystick - Servo Motor
  • Arduino - Soil Moisture Sensor
  • Arduino - Soil Moisture Sensor Pump
  • Arduino - Automatic Irrigation System
  • Arduino - LCD
  • Arduino - LCD I2C
  • Arduino - LCD 20x4
  • Arduino - LCD Keypad Shield
  • Arduino - OLED
  • Arduino - TFT LCD Display
  • Arduino - TFT LCD Touch Screen Display
  • Arduino - Round Circular TFT LCD Display
  • Arduino - Button Count - OLED
  • Arduino - Button Count - LCD
  • Arduino - 74HC595 4-Digit 7-Segment Display
  • Arduino - TM1637 4-Digit 7-Segment Display
  • Arduino - LED Matrix
  • Arduino - Bluetooth LED Matrix
  • Arduino - LED Matrix via Web
  • Arduino - Keypad
  • Arduino - Keypad 1x4
  • Arduino - Keypad - LCD
  • Arduino - Keypad - Beep
  • Arduino - Keypad - Buzzer
  • Arduino - Keypad - Relay
  • Arduino - Keypad - Servo Motor
  • Arduino - Keypad - Solenoid Lock
  • Arduino - Keypad - Electromagnetic Lock
  • Arduino - Temperature Sensor
  • Arduino - Temperature Sensor - LCD
  • Arduino - Temperature Sensor - OLED
  • Arduino - Temperature Sensor - LED Matrix
  • Arduino - Temperature via Web
  • Arduino - Temperature Sensor - Servo Motor
  • Arduino - Temperature Sensor - Relay
  • Arduino - DHT11
  • Arduino - DHT22
  • Arduino - Temperature Humidity Sensor
  • Arduino - Temperature Humidity Sensor - LCD
  • Arduino - Temperature Humidity Sensor - OLED Display
  • Arduino - LM35 Temperature Sensor
  • Arduino - TMP36 Temperature Sensor
  • Arduino - Display Temperature from LM35 Sensor on OLED
  • Arduino - Display Temperature from LM35 Sensor on LCD
  • Arduino - Control Temperature
  • Arduino - Cooling System using DHT Sensor
  • Arduino - Cooling System using DS18B20 Temperature Sensor
  • Arduino - Heating System
  • Arduino - DHT11 - Relay
  • Arduino - DHT11 - OLED
  • Arduino - DHT11 - LCD
  • Arduino - DHT22 - Relay
  • Arduino - DHT22 - OLED
  • Arduino - DHT22 - LCD
  • Arduino - Touch Sensor
  • Arduino - Touch Sensor - LED
  • Arduino - Touch Sensor - Relay
  • Arduino - Touch Sensor Toggle LED
  • Arduino - Touch Sensor Toggle Relay
  • Arduino - Touch Sensor - Piezo Buzzer
  • Arduino - Touch Sensor - Servo Motor
  • Arduino - Touch Sensor - Solenoid Lock
  • Arduino - Door Sensor
  • Arduino - Door Sensor - LED
  • Arduino - Door Sensor - Relay
  • Arduino - Door Sensor Toggle LED
  • Arduino - Door Sensor Toggle Relay
  • Arduino - Door Sensor - Piezo Buzzer
  • Arduino - Door Sensor - Servo Motor
  • Arduino - Solenoid Lock
  • Arduino - Electromagnetic Lock
  • Arduino - Button Controls Electromagnetic Lock
  • Arduino - Keypad Door Lock
  • Arduino - Door Lock System using Password
  • Arduino - Controls Door Lock via Web
  • Arduino - RFID/NFC
  • Arduino - RFID/NFC - Relay
  • Arduino - RFID/NFC - Servo Motor
  • Arduino - RFID - Solenoid Lock
  • Arduino - RFID - Electromagnetic Lock
  • Arduino - RFID/NFC Door Lock System
  • Arduino - RTC
  • Arduino - DS1307 RTC Module
  • Arduino - OLED Clock
  • Arduino - LCD Clock
  • Arduino - 7-segment Clock
  • Arduino - Water Sensor
  • Arduino - Water Leak Detector
  • Arduino - Water Sensor Relay
  • Arduino - Water Sensor Pump
  • Arduino - Water/Liquid Valve
  • Arduino - Controls Pump
  • Arduino - Button - Pump
  • Arduino - Rain Sensor
  • Arduino - Rain Sensor - LED
  • Arduino - Rain Sensor - Relay
  • Arduino - Rain Sensor - Buzzer
  • Arduino - Rain Sensor - Servo Motor
  • Arduino - Measure Voltage
  • Arduino - Sound Sensor
  • Arduino - Sound Sensor - LED
  • Arduino - Sound Sensor - Relay
  • Arduino - Sound Sensor - Servo Motor
  • Arduino - Force Sensor
  • Arduino - Flame Sensor
  • Arduino Fire Alarm
  • Arduino - Gas Sensor
  • Arduino - Gas Sensor - Relay
  • Arduino - Infrared Obstacle Avoidance Sensor
  • Arduino - Stepper Motor
  • Arduino - Controls 28BYJ-48 Stepper Motor using ULN2003 Driver
  • Arduino - Controls Stepper Motor using L298N Driver
  • Arduino - DRV8825 Stepper Motor Driver
  • Arduino - Stepper Motor and Limit Switch
  • Arduino - GPS
  • Arduino - LED Strip
  • Arduino - NeoPixel LED Strip
  • Arduino - WS2812B LED Strip
  • Arduino - Dotstar Led Strip
  • Arduino - Micro SD Card
  • Arduino - Write Variable to SD Card
  • Arduino - Log Data with Timestamp to SD Card
  • Arduino - Read Config from SD Card
  • Arduino - Audio
  • Arduino - MP3 Player
  • Arduino - RFID MP3 Player
  • Arduino - Siren
  • Arduino - SoftwareSerial
  • Arduino RS232
  • Arduino RS485
  • Arduino RS422
  • Arduino Serial to WiFi Converter
  • Arduino RS232 to WiFi
  • Arduino RS485 to WiFi
  • Arduino RS422 to WiFi
  • Arduino - IR Remote Control
  • Arduino - Bluetooth
  • Arduino - BLE
  • Arduino controls Servo Motor via Bluetooth
  • Arduino controls LED via Bluetooth
  • How to get started with Arduino Uno R4
  • Arduino Uno R4 WiFi controls LED via Web
  • Arduino Uno R4 WiFi controls Relay via Web
  • Arduino Uno R4 LED Matrix displays Number Character

INTERNET OF THING (IoT)

  • Arduino - Ethernet
  • Arduino - Ethernet Module
  • Arduino - Ethernet Shield 2
  • Arduino - PHPoC Shield
  • Arduino - WiFi
  • Arduino - TCP Client
  • Arduino - Web Server
  • Arduino - Web Server Multiple Pages
  • Arduino - Web Server Password
  • Arduino - WebSocket
  • Arduino - Web Plotter
  • Arduino - Controls Servo Motor via Web
  • Arduino - Controls Car via Web
  • Arduino - IoT
  • Arduino - MQTT
  • Arduino Shield
  • Arduino - HTTP Request
  • Arduino - HTTPS Request
  • Arduino - MySQL
  • Arduino - Modbus
  • Communication between two Arduino
  • Arduino to Arduino MQTT Communication
  • Arduino - IFTTT
  • Arduino - Make Voice Phone Call
  • Arduino - Send SMS Message
  • Arduino - Gmail
  • Arduino - Door Open Email Notification
  • Arduino - Motion Sensor Email Notification
  • Arduino - Water Leak Email Notification
  • Arduino - Secure Your Belongings: Detect Theft and Send Email Alerts
  • Arduino - Send Email
  • Arduino - Door Open - Send Email Notification
  • Arduino - Temperature - Send Email Notification

LIBRARIES

  • Arduino - Button Library
  • Example - 01.Single Button
  • Example - 02.Single Button Events
  • Example - 03.Single Button Debounce
  • Example - 04.Single Button All
  • Example - 05.Multiple Button All
  • Example - 06.Button Count
  • Example - 07.Button Array
  • Arduino - LED Library
  • Example - LED Blink
  • Example - LED Blink In Period
  • Example - LED Blink Number Of Times
  • Example - LED Fade In Fade Out
  • Example - LED On Off
  • Example - LED Toggle
  • Example - Multiple LED
  • Example - LED Array
  • Arduino - Output Library
  • Example - 01.On Off
  • Example - 02.Toggle
  • Example - 03.Single Blink Without Delay
  • Example - 04.Single Blink Change Frequency
  • Example - 05.Multiple Blink Without Delay
  • Example - 06.Multiple Blink With Offset
  • Example - 07.Blink In Period
  • Example - 08.Pulse
  • Arduino - Buzzer Library
  • Example - Beep
  • Example - Melody
  • Example - Melody Repeat
  • Arduino - Analog Keypad Library
  • Example - Analog Keypad
  • Example - Analog Button Array
Arduino - Micro SD Card

In this tutorial, we are going to learn how to use the Micro SD Card with Arduino. In detail, we will learn:

  • Arduino - How to open a file on Micro SD Card and create if not existed
  • Arduino - How to write data to a file on Micro SD Card
  • Arduino - How to read a file on Micro SD Card character-by-character
  • Arduino - How to read a file on Micro SD Card line-by-line
  • Arduino - How to append content to an exsiting file on Micro SD Card
  • Arduino - How to overwrite a file on Micro SD Card
Arduino Micro SD Card

Hardware Required

1×Official Arduino Uno
1×Alternatively, DIYables STEM V3, Fully Compatible with Arduino Uno R3
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Micro SD Card
1×Micro SD Card Module
1×Jumper Wires
1×Optionally, MicroSD to SD Memory Card Adapter
1×Recommended: Screw Terminal Block Shield for Arduino Uno
1×Recommended: Breadboard Shield for Arduino Uno
1×Recommended: Enclosure for Arduino Uno
1×Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V3 Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.Additionally, some links direct to products from our own brand, DIYables .

About Micro SD Card Module

The Micro SD Card Module can interface with Arduino and it can carry a Micro SD Card. In another word, the Micro SD Card Module is a bridge between Arduino and Micro SD Card

Pinout

Micro SD Card Module Pinout

Micro SD Card Module includes 6 pins:

  • VCC pin: connect to the Arduino's 5V pin.
  • GND pin: connect this pin to the Arduino's GND.
  • MISO pin: (Master In Slave Out) connect this pin to the Arduino's MISO pin.
  • MOSI pin: (Master Out Slave In) connect this pin to the Arduino's MOSI pin.
  • SCK pin: connect this pin to the Arduino's SCK pin.
  • SS pin: (Slave Select) connect this pin to the pin specified in Arduino code as a SS pin.

Preparation

  • Connect the Micro SD Card to the PC via USB 3.0 SD Card Reader
  • Make sure that the Micro SD Card is formatted FAT16 or FAT32 (Google for it)

Wiring Diagram

Arduino Micro SD Card Module Wiring Diagram

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

If you use an Ethernet shield or any shield that has a Micro SD Card Holder, you do not need to use the Micro SD Card Module. You just need to insert the Micro SD Card to the Micro SD Card Holder on the shield.

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

Arduino Code

Quick Steps

  • Insert the Micro SD Card to the Micro SD Card module
  • Do the wiring between the Micro SD Card module and Arduino as the above wiring diagram
  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Open Serial On Arduino IDE
  • Copy the below code and paste it to Arduino IDE
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } 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() { }
  • Click Upload button on Arduino IDE to upload code to Arduino
  • The result on Serial Monitor for 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 result on Serial Monitor for the next runs
COM6 Send SD CARD INITIALIZED. arduino.txt exists on SD Card. Autoscroll Show timestamp Clear output 9600 baud   Newline  

※ NOTE THAT:

You may NOT see the output on Serial Monitor for the first run of your first upload is done before opening Serial Monitor.

  • Detach the Micro SD Card from the module
  • Insert the Micro SD Card to an USB SD Card reader
  • Connect the USB SD Card reader to the PC
  • Check if the file exists or not

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

The below code does:

  • Write data to a file
  • Read the content of the a file character-by-character and print it to Serial Monitor
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); // open file for writing myFile = SD.open("arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by ArduinoGetStarted.com"); // write a line to Arduino myFile.println("Learn Arduino and SD Card"); // write another line to Arduino myFile.close(); } else { Serial.print(F("SD Card: error on opening 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: error on opening file arduino.txt")); } } void loop() { }
  • The Serial Monitor shown the content of the file
COM6 Send Created by ArduinoGetStarted.com Learn Arduino and SD Card Autoscroll Show timestamp Clear output 9600 baud   Newline  

※ NOTE THAT:

The data will be appended to the end of file by default. If you reboot Arduino with above code, the text will be appended to the file again ⇒ the Serial Monitor will shows more lines as below:

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

You can also detach the Micro SD Card from the module, and open it on your PC to check the content (USB SD Card reader is needed)

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

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); // open file for writing myFile = SD.open("arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by ArduinoGetStarted.com"); // write a line to Arduino myFile.println("Learn Arduino and SD Card"); // write another line to Arduino myFile.close(); } else { Serial.print(F("SD Card: error on opening 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: error on opening file arduino.txt")); } } void loop() { }
  • The result on Serial Monitor
COM6 Send SD CARD INITIALIZED. Line 1: Created by ArduinoGetStarted.com Line 2: Learn Arduino and SD Card Autoscroll Show timestamp Clear output 9600 baud   Newline  

※ NOTE THAT:

You may see more lines on Serial Monitor if the content of the file is not deleted before.

Arduino - How to overwrite a file on Micro SD Card

By default, the content will append to the end of the file. The simplest way to overwrite a file is: delete the exsiting file and create new one with the same name

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } 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 ArduinoGetStarted.com"); // write a line to Arduino myFile.println("Learn Arduino and SD Card"); // write another line to Arduino myFile.close(); } else { Serial.print(F("SD Card: error on opening 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: error on opening file arduino.txt")); } } void loop() { }
  • The result on Serial Monitor
COM6 Send SD CARD INITIALIZED. Created by ArduinoGetStarted.com Learn Arduino and SD Card Autoscroll Show timestamp Clear output 9600 baud   Newline  
  • Reboot Arduino
  • Check if the content of the file on Serial Monitor is appended or not.

You can also detach the Micro SD Card from the module, and open it on your PC to check the content (USB SD Card reader is needed)

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Function References

  • Serial
  • Arduino SD Card Library Reference

The Best Arduino Starter Kit

  • See the best Arduino kit for beginner

See Also

  • Arduino - Write Variable to SD Card
  • Arduino - Log Data with Timestamp to SD Card
  • Arduino - Read Config from SD Card

※ OUR MESSAGES

  • We are AVAILABLE for HIRE. See how to hire us to build your project
  • If this tutorial is useful for you, please give us motivation to make more tutorials.
  • You can share the link of this tutorial anywhere. Howerver, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!
PREVIOUS NEXT DISCLOSURE ArduinoGetStarted.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com, Amazon.it, Amazon.fr, Amazon.co.uk, Amazon.ca, Amazon.de, Amazon.es, Amazon.nl, Amazon.pl and Amazon.se Copyright © 2018 - 2025 ArduinoGetStarted.com. All rights reserved. Terms and Conditions | Privacy Policy Email: [email protected]

TABLE OF CONTENTS

  • Hardware Required
  • About Micro SD Card Module
  • Wiring Diagram
  • Arduino - How to open a file on Micro SD Card and create if not existed
  • Arduino - How to write/read data to/from a file on Micro SD Card
  • Arduino - How to read a file on Micro SD Card line-by-line
  • Arduino - How to overwrite a file on Micro SD Card
  • Video
  • Function References
  • The Best Arduino Starter Kit
  • See Also
×

Tag » Arduino Micro Sd Card Adapter Tutorial