Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » Using SD Card Module with Arduino | Read/Write/Data Logger
Arduino Projects

Using SD Card Module with Arduino | Read/Write/Data Logger

Mamtaz AlamBy Mamtaz AlamUpdated:August 20, 20226 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
SD Card Module Arduino
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

Overview: Using SD Card Module with Arduino

In this tutorial, we will learn to use of SD Card Module with an Arduino microcontroller to read, write, store data or make a data logger. We can use the SD Card Module to add the desired memory to the Arduino project to store the data, Media, etc.

The Micro SD Card Reader Module is also called a Micro SD Adaptor. The Module is a simple solution for transferring data to and from a standard SD card. Hence, the tutorial covers the reading of SD Card info and the method to read and write data to SD Card.

Similarly, Building a data logger using Arduino and SD Card is so easy. The DHT11 sensor is used to sense the relative humidity & temperature and the SD card is used to save the values of the humidity and the temperature every 1 second in txt format.

Earlier, we used the SD Card Module on the project like RFID Attendance System and on Surveillance CCTV project.



Bill of Materials

For this tutorial, we recommend to buy the following list of components directly from Amazon.

S.N.Components NameQuantityPurchase Links
1Arduino UNO Board1Amazon | AliExpress
2SD Card Module1Amazon | AliExpress
3SD Card 4-16 GB1Amazon | AliExpress
4USB Card Reader1Amazon | AliExpress
5DHT11 Sensor1Amazon | AliExpress
6Jumper Wires10Amazon | AliExpress

SD Card Module/Adapter

The SD card module is especially useful for projects that require data logging. There are actually two ways to interface with micro SD cards – SPI mode and SDIO mode. Hobbyists like us prefer SPI Mode for interfacing as it’s easy compared to SDO mode which is very complex.

SD Card Module

The operating voltage of micro SD Cards is 3.3 V. Therefore, we cannot SD Card directly with 5V logic. But the module has an onboard ultra-low dropout regulator that converts voltages from 3.3V – 6V down to ~3.3V. There is also a Logic Level converter IC 74LVC125A on the module which converts the interface logic from 3.3V-5V to 3.3V.




Micro SD Card Module Pinout

There are total of six pins (GND, VCC, MISO, MOSI, SCK, CS) on a SD-Card Adapter.

1. GND: Ground Pin
2. VCC: +5V power supply
3. MISO: SPI output
4. MOSI: SPI input
5. SCK: Accepts clock pulses for Data Synchronization
6. CS: Chip select signal pin to enable/disable line


Preparing the micro SD card

Before you insert the micro SD card into the module, you must properly format the card. You should format the SD card as FAT16 or FAT32.

To format the SD card, insert it into your computer. Go to My Computer and right-click on the SD card. A new window pops up. Select FAT32 as a Formatting option. Then press Start to initialize the formatting process

Now the SD Card is properly formatted and you can use the SD Card with SD Card Module to use in project applications.




Interfacing SD Card Module with Arduino

Now let us learn interfacing of SD Card Module with Arduino to read & write data or to make a data logger. Since the SD Card Module works on SPI Communication protocol, thus we need to connect it to SPI Pin of Arduino Board.

SD Card Module Arduino Interfacing

Connect the SD Card Module to Arduino as per the above Circuit Diagram. For Arduino boards like UNO/Nano, the SPI pins are 13 (SCK), 12 (MISO) and 11 (MOSI).

SD Card Module Arduino Connection

In case, if you are using other Arduino Boards then you follow the below table for connection.


Testing the SD Card Module

To make sure everything is wired correctly and the SD card is working properly, upload the following code to the Arduino Board.Make sure you have the right board and COM port selected.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// include the SD library:
#include <SPI.h>
#include <SD.h>
 
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
 
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = 10;
 
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
 
  Serial.print("\nInitializing SD card...");
 
  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    while (1);
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }
 
  // print the type of card
  Serial.println();
  Serial.print("Card type:         ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }
 
  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    while (1);
  }
 
  Serial.print("Clusters:          ");
  Serial.println(volume.clusterCount());
  Serial.print("Blocks x Cluster:  ");
  Serial.println(volume.blocksPerCluster());
 
  Serial.print("Total Blocks:      ");
  Serial.println(volume.blocksPerCluster() * volume.clusterCount());
  Serial.println();
 
  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("Volume type is:    FAT");
  Serial.println(volume.fatType(), DEC);
 
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize /= 2;                           // SD card blocks are always 512 bytes (2 blocks are 1KB)
  Serial.print("Volume size (Kb):  ");
  Serial.println(volumesize);
  Serial.print("Volume size (Mb):  ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Gb):  ");
  Serial.println((float)volumesize / 1024.0);
 
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
 
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}
 
void loop(void) {
}


Open the Serial Monitor at a baud rate of 9600 and if everything is working properly you’ll see the following message on the serial monitor.

If SD Card isn’t working properly or if it fails to mount to the Board, then you might see the following messages.


Reading & Writing the data

Let us learn how you can read and write the data to SD Card with Arduino microcontroller. The SD library provides useful functions for easily write in and read from the SD card.

Copy the following sketch and upload it to the Arduino Board.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <SPI.h>
#include <SD.h>
 
File myFile;
 
// change this to match your SD shield or module;
const int chipSelect = 10;
 
void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
 
  Serial.print("Initializing SD card...");
 
  if (!SD.begin()) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
 
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
 
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}
 
void loop()
{
  // nothing happens after setup
}

Once the code is uploaded & if everything is ok, the following will appear on the serial monitor.

SD Card arduino Read Write Data

This indicates reading writing is done successfully. If you want to check about the files on SD Card, then you can open the SD Card file on your computer and see the following data in txt format.


Code Explanation

To write and read from the SD card, we include the SPI and SD libraries.

1
2
#include <SPI.h>
#include <SD.h>

We have to initialize the SD card module at the Chip Select (CS) pin 10.

1
SD.begin(10);

To open a new file in the SD card, we then create a file object that refers to the data file.

1
dataFile = SD.open("data.txt", FILE_WRITE);

We use the following line to write data to the currently opened file.

1
dataFile.write(data);

We can also use the print() or println() functions to print data into the file.

1
2
dataFile.print(data);
dataFile.println(data);

Using the following line, we read the data saved on file.

1
dataFile.read();

We can only write within a file at once, so we need to close a file before proceeding to the next one.

1
SD.close("data.txt");



Arduino data logger using SD card & DHT11 sensor

Building a data logger using Arduino and SD Card is so easy. This tutorial explains how to build a simple temperature and humidity data logger with a DHT11 sensor.

The DHT11 Sensor is used to sense the relative humidity and temperature and the SD card is used to save the values of the humidity and the temperature every 1 second. The values of the temperature and humidity are saved in .TXT file on the SD card.

SD Card Arduino Data Logger

Connect a DHT11 Sensor to the above circuit. Here is a circuit diagram and connection for making a data logger.

SD Card Arduino Data Logger

Copy the following code and upload it to the Arduino Board. The code requires DHT11 Library for compilation.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Arduino data logger with SD card and DHT11 humidity and temperature sensor
#include <SPI.h>        // Include SPI library (needed for the SD card)
#include <SD.h>         // Include SD library
#include <DHT.h>        // Include DHT sensor library
File dataFile;
#define DHTPIN 4            // DHT11 data pin is connected to Arduino pin 4
#define DHTTYPE DHT11       // DHT11 sensor is used
DHT dht(DHTPIN, DHTTYPE);   // Initialize DHT library
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial)
    ; // wait for serial port to connect. Needed for native USB port only
  Serial.print("Initializing SD card...");
  if (!SD.begin())
  {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  delay(2000);
  dht.begin();
}
uint16_t line = 1;
void loop() {
  delay(1000);
  // Read humidity
  byte RH = dht.readHumidity();
  //Read temperature in degree Celsius
  byte Temp = dht.readTemperature();
  
  dataFile = SD.open("DHT11Log.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (dataFile)
  {
    Serial.print(line);
    Serial.print(":    Temperature = ");
    Serial.print(Temp);
    Serial.print("°C,    Humidity = ");
    Serial.print(RH);
    Serial.println("%");
    // Write data to SD card file (DHT11Log.txt)
    dataFile.print(line++);
    dataFile.print(":    Temperature = ");
    dataFile.print(Temp);
    dataFile.print("°C,    Humidity = ");
    dataFile.print(RH);
    dataFile.println("%");
    dataFile.close();
    
  }
  // if the file didn't open, print an error:
  else
    Serial.println("error opening DHT11Log.txt");
}



After uploading the code, open the Serial Monitor and you will see the following messages with the temperature and humidity data.

SD Card Arduino DHT11

Open the SD Card on windows using the Card Reader. Then open the txt file for the data logger. You will see the following data of temperature and humidity logged on the text file.

This is how you can use the SD Card Module with Arduino to read write data and make a data logger.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticleWiFi Controlled Robot using ESP8266 & Android App
Next Article Getting Started with Arduino IoT Cloud with ESP8266

Related Posts

DC Energy Meter using Arduino

Build a DC Energy Meter using Arduino – 32V/5A

Updated:August 26, 20252K
Interfacing ADXL375 Accelerometer with Arduino

Interfacing ADXL375 Accelerometer with Arduino (±200g)

Updated:June 28, 2025
PZEM-004T Arduino Energy Meter

DIY AC Energy Meter using PZEM-004T & Arduino

Updated:March 6, 20258K
Interfacing BMI160 Accelerometer & Gyroscope with Arduino

Interfacing BMI160 Accelerometer & Gyroscope with Arduino

Updated:February 2, 20259K
Password Based Door Lock Security System Using Arduino & Keypad

Password Based Door Lock Security System Using Arduino & Keypad

Updated:February 2, 20252436K
Earthquake Detector Alarm with with Accelerometer & Arduino

Earthquake Detector Alarm with Accelerometer & Arduino

Updated:February 2, 2025661K
Add A Comment

CommentsCancel reply

Latest Posts
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

May 31, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

February 1, 2026
Top Posts & Pages
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • ECG Graph Monitoring with AD8232 ECG Sensor & Arduino
    ECG Graph Monitoring with AD8232 ECG Sensor & Arduino
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • How to use INA226 DC Current Sensor with Arduino
    How to use INA226 DC Current Sensor with Arduino
  • ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
    ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
  • How to use Modbus RTU with ESP32 to read Sensor Data
    How to use Modbus RTU with ESP32 to read Sensor Data
  • IoT Based ECG Monitoring with AD8232 ECG Sensor & ESP32
    IoT Based ECG Monitoring with AD8232 ECG Sensor & ESP32
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (204)
    • ESP32 MicroPython (7)
    • ESP32 Projects (81)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.