IoTSimulator

MicroSD Card: How It Works, Wiring, and Example Code

A microSD card breakout module used for storing data, loading files, and expanding Arduino projects with removable flash storage.
M
Muhammad Ichsan
MicroSD Card

This article is a guide about the MicroSD Card module. We will explain how it works, show how it is wired, and walk through an Arduino example you can use in your own projects.

The MicroSD Card is useful when a project needs to save data or read files. It gives a small project a place to store information that can be opened again later instead of disappearing when the board turns off.

Before we jump into the details, it helps to look at the real module first so the card slot, connector, and board shape are easier to understand.

Description

MicroSD card module
MicroSD card module

The MicroSD Card module is a small breakout board that lets Arduino read from and write to removable flash storage. It is commonly used for data logging, audio projects, image loading, configuration files, and any project that needs more storage than the microcontroller itself provides.

Instead of acting like a processing board, this module acts like a storage device. That means the Arduino sends commands, and the card responds with file data, storage blocks, or card information depending on what the sketch asks for.

Because the card is removable, it is also convenient for testing. You can record data, remove the card, and open the files on a computer to check what the Arduino saved.

Some module versions look a little different, but the idea stays the same. The important part is the card socket and the small SPI-style pin set, because that is what lets the Arduino talk to the storage card in a predictable way.

Features

Here are the main things to know about the MicroSD Card module:

FeatureWhat it means
Removable storageLets Arduino store files on a microSD card.
SPI interfaceUses MOSI, MISO, SCK, and CS for communication.
Data loggingUseful for sensor records and project history.
File accessCan create, read, and write files with the SD library.
Card detectOptional pin for checking whether a card is inserted.
Beginner friendlyGood introduction to storage and file handling.

The most useful part is that Arduino can write data in a way that you can later open and inspect on a computer.

How Does It Work?

How the MicroSD card module works
How the MicroSD card module works

The MicroSD module connects to the Arduino over SPI. The Arduino uses the CS pin to select the card, the DI and DO lines to send and receive data, and SCK to keep the communication synchronized.

When the sketch opens a file or writes a line of text, the library turns that high-level request into SD card commands. The card then stores the data in flash memory so it stays there even after power is removed.

SPI Data Flow

The SPI bus is the important idea here. The Arduino sends bytes quickly over DI and SCK, and the card returns data on DO. That makes the card fast enough for reading files or logging sensor values without making the wiring too complicated.

Card Detect

The CD pin is optional, but it is handy if you want the sketch to know when a card is inserted or removed. Some holders use a different detect polarity, so it is worth checking the module before relying on that pin.

Technical Notes

For this Arduino workflow, the safest cards are the ones formatted as FAT16 or FAT32. Arduino's SD library is documented as supporting FAT16 and FAT32 on standard microSD and microSDHC cards, and the SD Association classifies cards up to 2GB as SD with FAT12 or FAT16 and cards from 2GB up to 32GB as SDHC with FAT32.

That means a small card is often the least surprising choice when you are building a simple logger. In practice, 2GB and 32GB are the important capacity markers to remember: SD cards up to 2GB are the older SD family, SDHC covers 2GB to 32GB, and cards above that fall into the SDXC family, which the SD Association says uses exFAT. For this sketch, the practical choice is to stay with a properly formatted SD or SDHC card so the file workflow remains predictable.

Another detail that matters is speed. The card itself can be much faster than the sketch needs to be, but the Arduino still talks to it through the SPI pins and the library. That is why storage feels simple from the code side even though the card is doing low-level work behind the scenes.

Arduino With MicroSD Card

SCL
SDA
AREF
GND.1
D13
D12
D11
D10
D9
D8
D7
D6
D5
D4
D3
D2
D1
D0
IOREF
RESET
3V3
5V
GND.2
GND.3
VIN
A0
A1
A2
A3
A4
A5
CD
DO
GND
SCK
VCC
DI
CS

This circuit preview shows the MicroSD card module connected to an Arduino Uno so you can understand the SPI layout before reading the sketch. The chip select line matters just as much as the data lines because it tells the card when to listen.

Pin Connection

The pin map below shows the common Arduino Uno wiring for this storage module. If the lines are mixed up, the card often fails to initialize or the sketch cannot open a file reliably.

Pin Connection Map
MicroSD Pin
VCC
Arduino Uno
5V
Explanation

Powers the module so the card socket and logic can work.

MicroSD Pin
GND
Arduino Uno
GND
Explanation

Shares the same electrical reference as the Arduino.

MicroSD Pin
SCK
Arduino Uno
D13
Explanation

Provides the clock signal that keeps SPI communication in step.

MicroSD Pin
DO
Arduino Uno
D12
Explanation

Carries data from the card back to the Arduino.

MicroSD Pin
DI
Arduino Uno
D11
Explanation

Carries data from the Arduino to the card.

MicroSD Pin
CS
Arduino Uno
D10
Explanation

Selects the card so the library knows when to talk to it.

MicroSD Pin
CD
Arduino Uno
D8
Explanation

Optional card-detect input used to check whether a card is inserted.

Once the wiring matches the SPI pins, the card usually becomes straightforward to use. If it still refuses to initialize, the first thing worth checking is the CS line, because that pin decides whether the card actually listens to the Arduino.

About SD Library

The SD library handles the file workflow so the sketch can work with storage in a simple way. It gives you a small set of core functions that cover the full life cycle of working with a card.

  • SD.begin(csPin): starts the card on the chosen chip-select pin.
  • SD.open(filename, mode): opens or creates a file for reading or writing.
  • File.print() and File.println(): write text or values into the file.
  • File.read() and File.available(): read data back from an existing file.
  • File.close(): closes the file so the data is saved properly.

That matters because the card itself does not behave like a simple sensor. It needs commands, file names, and a proper write flow. The library keeps that process readable, which is why the code can stay short even when the storage task is more complex underneath.

When the wiring is correct, the library becomes the part that lets you focus on the project output instead of the card protocol. That is useful for logs, settings files, and any sketch that needs to save something and read it back later.

Arduino Code

This example initializes the card, creates a log file if needed, and writes a short message to it. It is a simple first test that proves the storage module is working.

C++ Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

Once this works, you can write sensor readings, timestamped events, or longer project logs to the card instead of just one test line.

How The Code Works, Part By Part

Let's break the sketch into smaller pieces so the storage workflow is easier to understand and easier to modify later.

Libraries

The sketch loads SPI for the bus protocol and SD for file access. Those two libraries handle most of the low-level card communication for you.

C++ Source
1
2

Initialize The Card

The setup block starts Serial Monitor, configures the chip-select pin, and tries to initialize the card. If initialization fails, the sketch stops so you know the card or wiring needs attention.

C++ Source
1
2
3
4
5
6
7
8
9
10
11
12
13

Open A File

The sketch opens log.txt for writing. If the file does not exist yet, the library can create it for you.

C++ Source
1

Write And Close

After the file is open, the sketch writes a line of text and closes the file. Closing the file is important because it helps make sure the data is saved properly.

C++ Source
1
2
3
4
5

Repeat

This starter example does not need to keep writing forever. In a real project, the loop would add more records whenever a sensor changes or a timer event happens.

Wrapping Up

The MicroSD Card is a simple way to add storage and file saving to an Arduino project, but it becomes much more useful once you treat it as part of the project workflow instead of just a place to dump text.

Once you understand the SPI pins, the chip-select line, and the file workflow, you can use it for data logging, config files, saved measurements, and any project that needs removable storage. In real use, the card is most helpful when the data matters later, because you can pull it out and inspect the file instead of guessing what happened during the run.

From here, a good next step is to log temperature, motion, or button events into a file and open it afterward to see the pattern. That makes the module feel practical right away and gives the storage workflow a clear job in the circuit.

New

Wait! We're building more...

Our laboratory is currently preparing a lot of exciting new projects using MicroSD Card. Stay tuned for the upcoming massive update!

Projects Using MicroSD Card

Explore practical implementations

No official projects found for this component yet.

Muhammad Ichsanul Fadhil
About Writer

Muhammad Ichsanul Fadhil

"I'm a developer and hardware enthusiast with a passion for IoT. I love experimenting with new components and writing down everything I learn to help others build their own projects."

Share this article

Share it with your favorite channel.