Micro SD Card Module interfacing with Arduino

Imagine working on a project like a weather monitoring system that records temperature and humidity every few seconds, or an automated irrigation setup that logs soil moisture levels throughout the day. In applications like these, the internal memory of an Arduino quickly becomes insufficient for storing large amounts of data.

 

So how can you add more storage to your Arduino project?

 

A practical and cost-effective solution is to use a microSD card. These compact memory cards offer storage capacities ranging from a few megabytes to several gigabytes. By integrating a microSD card module, you can significantly extend your Arduino’s data-logging capability.

 

In this tutorial, you’ll learn how to connect and interface a microSD card module with Arduino, enabling reliable external data storage for your projects.

 

Micro SD Card Module Overview

 

A typical microSD card module consists of three main functional components:


 

Micro SD Card Socket

This slot is used to insert the microSD card. Most modules include markings or arrows near the socket to indicate the correct card orientation, helping prevent improper insertion.

 

3.3V LDO Voltage Regulator

MicroSD cards operate at 3.3V, while Arduino boards usually supply 5V. Directly powering a microSD card with 5V can permanently damage it. To avoid this, the module includes a low-dropout (LDO) voltage regulator that converts the 5V supply from the Arduino (or another power source) down to a stable 3.3V. This ensures safe and reliable operation even if the input voltage varies slightly.

 

Logic Level Shifter

The module also includes a 74LVC125A logic level shifter, which acts as a voltage translator. Since Arduino SPI signals operate at 5V and the microSD card expects 3.3V logic levels, the level shifter converts the incoming signals to a safe voltage range. This allows proper SPI communication without risking damage to the card.

 

Micro SD Card Module Pinout

 

 

GND: This is the ground connection and must be connected to the Arduino’s GND pin.

 

VCC: This is the power input pin. It is typically connected to the Arduino’s 5V output, which is internally regulated down to 3.3V by the onboard voltage regulator.

 

MISO (Master In Slave Out): This pin carries data from the microSD card module to the Arduino during SPI communication.

 

MOSI (Master Out Slave In): This pin is used to send data from the Arduino to the microSD card module.

 

SCK (Serial Clock): The clock signal generated by the Arduino that synchronizes data transfer between the master and the slave device.

 

CS (Chip Select / Slave Select): This pin enables or disables the microSD card module on the SPI bus. The Arduino pulls this pin LOW to initiate communication with the card.

 

Wiring Micro SD Card Module with Arduino

 

 

The microSD card module communicates with the Arduino using the SPI (Serial Peripheral Interface) protocol. Because of this, it relies on the SPI-related pins—MOSI, MISO, SCK, and CS—to exchange data with the microcontroller. 

 

In addition to the SPI signals, the module requires two power connections. The VCC pin of the SD card module is connected to the 5V output of the Arduino to supply power, while the GND pin is connected to the Arduino’s ground. The onboard voltage regulator on the module converts this 5V input into the required 3.3V for safe SD card operation.

 

When interfacing the SD card reader with an Arduino Uno, the Chip Select (CS) pin of the module is connected to digital pin 10 on the Arduino. The Serial Clock (SCK) pin of the module is wired to digital pin 13, which provides the SPI clock signal. The MOSI (Master Out Slave In) pin of the SD card reader connects to digital pin 11 on the Arduino, allowing data to be sent from the Arduino to the card. Similarly, the MISO (Master In Slave Out) pin of the module is connected to digital pin 12, enabling the SD card to send data back to the Arduino. 

 

Connecting an I2C LCD to the Arduino is straightforward and requires only four wires. The VCC and GND pins of the I2C LCD are connected directly to the VCC and GND pins of the Arduino to provide power. The SCL (clock) pin of the LCD is connected to the Arduino’s SCL pin, while the SDA (data) pin of the LCD is connected to the Arduino’s SDA pin. On the Arduino Uno, these SCL and SDA lines correspond to the A5 and A4 analog input pins, respectively. These same signals are also available on the dedicated SDA and SCL header pins, which are internally connected to A4 and A5.

 

Before using the I2C LCD, it is important to ensure that the A0, A1, and A2 address jumpers on the LCD module are not shorted. In this setup, the program uses the I2C address 0x27, which is assigned only when these address jumpers remain open. If any of these jumpers are shorted, the I2C address of the LCD will change, and the code will need to be updated accordingly.

 

To learn more visit www.playwithcircuit.com

 

CODE
/* Code to read and Write data on SD card using library functions
by playwithcircuit.com */
#include "string.h"
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>  // I2C LCD
// create an object called datafile
File datafile;
char const dataArray[10] = "0123456789";
char cArray[11] = { 0 };
char index = 0;
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define Chip Select or slave select (CS/SS) pin used by library
const int chipSelect = 10;
void setup() {
  // initialize the LCD
  lcd.init();
  // Turn ON the Backlight
  lcd.backlight();
  // Clear the display buffer
  lcd.clear();
  // Print a message to the LCD
  lcd.setCursor(0, 0);
  lcd.print("Initializing");
  lcd.setCursor(0, 1);
  lcd.print("SD Card...");
  delay(2000);
  if (!SD.begin()) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Initialization");
    lcd.setCursor(0, 1);
    lcd.print("Failed");
    while (1)
      return;
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Initialization");
  lcd.setCursor(0, 1);
  lcd.print("Successfull");
  delay(2000);
  // if the file already exist
  if (SD.exists("myData.txt")) {
    // remove that file
    SD.remove("myData.txt");
  }
  // Creation and opening a file in write mode
  datafile = SD.open("myData.txt", FILE_WRITE);
  // if the file opened okay, write to it:
  if (datafile) {
    datafile.print(dataArray);
    // close the file
    datafile.close();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Data Written");
    lcd.setCursor(0, 1);
    lcd.print("Successfully");
    delay(2000);
  } else {
    // if the file didn't open/create, print an error
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("File Creation");
    lcd.setCursor(0, 1);
    lcd.print("Failure");
    while (1)
      ;
  }
  // re-open the file for reading:
  datafile = SD.open("myData.txt");
  if (datafile) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Data Read :");
    lcd.setCursor(0, 1);
    // read from the file until there's nothing else in it:
    while (datafile.available()) {
      cArray[index++] = datafile.read();
      if (index >= 10)
        break;
    }
    lcd.print(cArray);
    // close the file
    datafile.close();
    delay(3000);
    if (!(memcmp(dataArray, cArray, sizeof(dataArray)))) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Data");
      lcd.setCursor(0, 1);
      lcd.print("Matches");
      while (1)
        ;
    } else {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Data");
      lcd.setCursor(0, 1);
      lcd.print("Doesn't Match");
      while (1)
        ;
    }
  } else {
    // if the file didn't open, print an error
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("File Opening");
    lcd.setCursor(0, 1);
    lcd.print("Failure");
    while (1)
      ;
  }
}
void loop() {
  // nothing happens after setup
}
HARDWARE LIST
1 Arduino Uno
1 Micro SD Card
1 LCD 16x2
Jumper Wires
1 Micro SD card Module
1 12V Supply Adapter
License
All Rights
Reserved
licensBg
0