Zbotic Logo Zbotic Logo
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

  • Shop
  • About Us
  • Contact Us
  • Reseller
  • Blogs
020 69134444
1800 209 0998
[email protected]
Help Desk
Facebook Twitter Instagram Linkedin YouTube
Zbotic Logo Zbotic Logo
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

All departments
  • 3D Print Service
  • 3D Printer
  • Batteries & Chargers
  • Development Boards
  • Drone Parts
  • EBike parts
  • Sensor Modules
  • Electronic Components
  • Electronic Modules
  • IoT and Wireless
  • Mechanical Parts and Workbench Tools
  • Motors & Drivers & Pumps & Actuators
  • DIY and Robot Kits
  • Show more
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
Return to previous page
Home Development Boards & SBCs

SD Card Interface with SPI: Arduino, ESP32 and STM32 Guide

SD Card Interface with SPI: Arduino, ESP32 and STM32 Guide

March 11, 2026 /Posted byJayesh Jain / 0

The SD card SPI interface for Arduino, ESP32, and STM32 is one of the most universally useful peripherals for data logging, file storage, and firmware loading. This comprehensive guide covers wiring, library usage, and practical tips for reliable SD card operation across all three popular Indian maker platforms.

Table of Contents

  • SD Card SPI Protocol Basics
  • Wiring Guide for All Three Boards
  • Arduino SD Card Code
  • ESP32 SD Card with SPI
  • STM32 FatFS SD Card
  • Reliability Tips for India
  • Frequently Asked Questions

SD Card SPI Protocol Basics

SD cards operate in two modes: native SD bus (4-bit) and SPI mode. All microcontrollers use SPI mode for compatibility. The SPI interface uses four signals: MOSI (Master Out Slave In), MISO (Master In Slave Out), SCK (Serial Clock), and CS (Chip Select). SD cards run at 3.3V — connecting directly to 5V Arduino SPI pins can damage the card; always use a level converter or SD module with onboard 3.3V regulation.

Most Indian SD module boards (₹30–80 from local shops) include a 3.3V voltage regulator and level shifters, making them safe with both 5V Arduino and 3.3V ESP32/STM32.

Recommended: Arduino Uno R3 CH340G ATMega328PB Development Board — Arduino Uno R3 CH340G — the most popular SD card data logging platform in India; pairs directly with SD shield or module for weather station and sensor logging projects.

Wiring Guide for All Three Boards

SD Module Pin Arduino Uno ESP32 (WROOM) STM32F103
VCC 5V 3.3V 3.3V
GND GND GND GND
SCK D13 GPIO18 PA5 (SPI1_SCK)
MOSI D11 GPIO23 PA7 (SPI1_MOSI)
MISO D12 GPIO19 PA6 (SPI1_MISO)
CS D4 (or D10) GPIO5 PA4 or PB0
Recommended: Multifunction Shield For Arduino Uno / Leonardo — Multifunction Shield — includes SD card slot variant available in India for quick data logging on Arduino Uno without separate wiring.

Arduino SD Card Code

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

const int CS_PIN = 4; // SS/CS pin

void setup() {
  Serial.begin(115200);
  if (!SD.begin(CS_PIN)) {
    Serial.println("SD card initialisation failed!");
    return;
  }
  Serial.println("SD card ready.");
  
  // Write data to file
  File dataFile = SD.open("/datalog.csv", FILE_WRITE);
  if (dataFile) {
    dataFile.println("timestamp,temperature,humidity");
    dataFile.printf("1710000000,28.5,65.2
");
    dataFile.close();
    Serial.println("Data written.");
  }
  
  // Read file
  File readFile = SD.open("/datalog.csv");
  while (readFile.available()) {
    Serial.write(readFile.read());
  }
  readFile.close();
}

ESP32 SD Card with SPI

#include "SD.h"
#include "SPI.h"

#define SD_CS   5
#define SD_SCK  18
#define SD_MOSI 23
#define SD_MISO 19

void setup() {
  Serial.begin(115200);
  SPI.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
  
  if (!SD.begin(SD_CS)) {
    Serial.println("SD Mount Failed!");
    return;
  }
  
  // Get SD card type and size
  uint8_t cardType = SD.cardType();
  Serial.printf("Card Type: %s
", 
    cardType == CARD_SDHC ? "SDHC" : "SD");
  Serial.printf("Card Size: %llu MB
", SD.cardSize() / (1024*1024));
  
  // Append sensor reading with timestamp
  File f = SD.open("/sensors.csv", FILE_APPEND);
  if (f) {
    f.printf("%ld,%.2f,%.1f
", millis(), temperature, humidity);
    f.close();
  }
}
Recommended: ESP32-WROOM-32E Development Board Module for Arduino — ESP32-WROOM-32E — ESP32’s SPI with SD card is the most popular data logging solution for Indian IoT projects, supporting 32GB+ SDHC cards.

STM32 FatFS SD Card

STM32 uses the FATFS middleware (configured in STM32CubeMX) for SD card filesystem access. CubeMX generates boilerplate; add these to your user code:

/* STM32 FatFS example */
#include "fatfs.h"

FATFS SDFatFS;
FIL SDFile;
char sdPath[4];

void sdCard_init() {
  MX_FATFS_Init(); // Generated by CubeMX
  if(f_mount(&SDFatFS, (TCHAR const*)sdPath, 0) != FR_OK) {
    Error_Handler();
  }
}

void sdCard_write(const char *data) {
  UINT bytesWritten;
  if(f_open(&SDFile, "data.txt", FA_OPEN_APPEND | FA_WRITE) == FR_OK) {
    f_write(&SDFile, data, strlen(data), &bytesWritten);
    f_close(&SDFile);
  }
}

Reliability Tips for India

  • Use Class 10 microSD cards from reputable brands (SanDisk, Samsung) — cheap unbranded cards from Indian street markets cause random failures
  • Format cards as FAT32 (not exFAT) for maximum compatibility across all platforms
  • Use file.flush() after writes to ensure data is committed before power failure — important given India’s unpredictable power supply
  • Capacitors (100 µF electrolytic + 100 nF ceramic) on the SD module power rail reduce noise from India’s noisy 230V supply
  • SPI clock: start at 4 MHz for initialisation, increase to 25 MHz after successful init

Frequently Asked Questions

What is the maximum SD card size supported by Arduino?

Arduino’s SD library supports FAT16 (up to 2 GB) and FAT32 (up to 32 GB SDHC). SDXC cards (64 GB+) use exFAT which requires the SdFat library (recommended for large cards).

Why does my ESP32 SD card fail to initialise intermittently?

Common causes: insufficient decoupling capacitors, SPI clock too high at init, card not fully inserted, or power supply glitches (common with cheap Indian chargers). Add 100 µF capacitor to SD module VCC, reduce SPI clock to 4 MHz for init.

Can I use two SD cards simultaneously on one microcontroller?

Yes. Use two separate CS pins. Both cards share MOSI/MISO/SCK. Pull the unused CS high when accessing the other card. Both Arduino SD library and ESP32 SD library support this with separate SD objects.

Shop Development Boards & SBCs at Zbotic →

Tags: arduino sd card, ESP32 SD card, SD card data logging India, SD card SPI, SPI SD card tutorial, STM32 FatFS
Share Post
  • Facebook
  • Linkedin
  • Whatsapp
Wind Turbine for Home in India...
blog wind turbine for home in india small wind system guide 599105
blog smart irrigation system automated garden watering india 599117
Smart Irrigation System: Autom...

Related posts

Svg%3E
Read more

Battery Charger Module TP4056: LiPo and 18650 Charging Guide

April 1, 2026 0
The TP4056 battery charger module is one of the most essential components for any battery-powered electronics project. Costing under ₹30,... Continue reading
Svg%3E
Read more

Buck Converter vs Boost Converter: Voltage Regulation Guide

April 1, 2026 0
Understanding buck converters vs boost converters is essential for every electronics project involving power management. Whether you are stepping down... Continue reading
Svg%3E
Read more

Google Coral TPU: Accelerating AI Projects on Raspberry Pi

April 1, 2026 0
The Google Coral TPU (Tensor Processing Unit) transforms a Raspberry Pi from a sluggish AI hobbyist tool into a real-time... Continue reading
Svg%3E
Read more

NVIDIA Jetson Nano Projects India: Getting Started Guide

April 1, 2026 0
The NVIDIA Jetson Nano is the most accessible GPU-accelerated AI computer for developers in India. With 128 CUDA cores, a... Continue reading
Svg%3E
Read more

ATtiny85 Projects: Tiny Microcontroller for Space-Constrained Builds

April 1, 2026 0
The ATtiny85 is the Swiss Army knife of tiny microcontrollers — just 8 pins, 8 KB of flash, and a... Continue reading

Add comment Cancel reply

Your email address will not be published. Required fields are marked

Facebook Twitter Instagram Pinterest Linkedin Youtube

Get the latest deals and more.

Download on Google Play Download on the App Store

Call us: 020 69134444 / 1800 209 0998

Monday - Saturday 09:30 AM - 06:00 PM
For Technical Supports Email: [email protected]
For Sales / Enquiries Email: [email protected]

  • My Account

    • Cart

    • Wishlist

    • Checkout

    • My Orders

    • Track Order

    • My Account

  • Information

    • FAQs

    • Blogs

    • Career

    • About Us

    • Contact Us

    • Payment Options

  • Policies

    • Privacy Policy

    • Terms & Conditions

    • GST Input Tax Credit

    • Shipping Return Policy

    • E-Waste Collection Points

    • Our Sitemap

© Zbotic.in is registered trademark of Moxie Supply Pvt Ltd – All Rights Reserved
Login
Use Phone Number
Use Email Address
Not a member yet? Register Now
Reset Password
Use Phone Number
Use Email Address
Register
Already a member? Login Now