Interface SD Card via SPI (Polling & DMA)

In this tutorial we will cover how to interface the SD Card with STM32 using the SPI protocol. The library used in this tutorial also supports the use of DMA for data transfer, increasing the SD card read and write speeds.
The library supports majority of the SD Card types, that includes SDSC (Standard Capacity), SDHC (High Capacity) and SDXC (eXtended Capacity).

Table of Contents

Introducing SD Card Module

An SD Card SPI Module is a compact interface board used to connect an SD or microSD card to a microcontroller (like STM32 or Arduino) via the SPI (Serial Peripheral Interface) protocol. It provides level shifting and power regulation to safely operate the card at 3.3V, while allowing 5V-tolerant microcontrollers to communicate with it. The module enables easy reading and writing of data to SD cards, making it ideal for projects involving data logging, storage, or file handling.

SD Card Modue

Some of its features are:

FeatureDetails
SPI Interface SupportCommunicates with microcontrollers using the standard SPI protocol.
Voltage Level ShiftingIncludes built-in resistors or ICs to step down 5V signals to 3.3V.
Onboard Voltage RegulatorConverts 5V input from the microcontroller to 3.3V required by SD cards.
Standard SD & microSD SlotCompatible with standard SD or microSD cards for flexible usage.

WIRING DIAGRAM

Below is the image showing the connection between SD Card Module and STM32F103C8. I have also connected a FT232 USB-to-UART converted with the MCU. This is to send the logs to the serial monitor using the UART.

SD Card SPI STM32 Connection

As per the image, the connection is as follows:

PeripheralPeripheral PinSTM32 PinFunction
SD Card ModuleCSPB6Chip Select
 SCKPB3SPI Clock
 MOSIPB5Master Out Slave In (Data to card)
 MISOPB4Master In Slave Out (Data from card)
 VCC5VPower Supply
 GNDGNDGround
FTDI ModuleTXPA9UART RX (to receive data on STM32)

Note: Only the RX from FTDI is connected in this diagram, it’s used primarily for receiving data from the STM32.

CUBEMX CONFIGURATION

I am going to use the STM32F103C8 microcontroller for this project. We will start with the clock configuration first.

Clock Configuration

Below is the image showing the clock set up in F103C8.

STM32 Clock Configuration

The system is clocked by the 8Mhz HSE crystal and we will run it at maximum 72Mhz.

Leave a Comment

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

Recent Posts

Interface GC9A01 Round Display with STM32

Learn how to interface the GC9A01 round TFT display with STM32 via SPI and implement LVGL for rich embedded GUIs. Step-by-step guide to wiring, code, and rendering graphics with LVGL. The project is available for download at the end of the post

Read More »

Interface SD Card via SPI (Polling & DMA)

Step-by-step guide to interface an SD card with STM32 using SPI and DMA. Tis tutorial covers CubeMX setup, FATFS init, SPI‑DMA config, and read/write file operations. The project is available to download at the end of the post.

Read More »

Internal RTC in STM32

Learn how to configure the built‑in RTC on STM32 using HAL and CubeMX. Includes setting time, date, alarms, LCD display, VBAT backup – step‑by‑step guide. The project is available for download at the end of the post.

Read More »
Scroll to Top