SD Card Interfacing with STM32 using Arduino IDE

This is the Series of tutorials on the STM32 Microcontroller. The aim of this series is to provide easy and practical examples that anyone can understand. In our last tutorial, we saw the basic STM32 SPI Communication using the Arduino IDE. In this tutorial, we will see SD Card Interfacing with STM32 using Arduino IDE.

Why use SPI for SD Card Communication?

SD cards are commonly used for storing data in embedded systems due to their small form factor and high storage capacity. The SPI interface is a popular choice for communicating with SD cards because it requires fewer pins compared to other interfaces like the SDIO interface. This makes it ideal for microcontrollers with limited GPIO pins, such as the STM32. SPI, or Serial Peripheral Interface, is a common protocol used for communicating with SD cards. It is preferred for several reasons:

  1. Simplicity and Ease of Use: SPI is simpler to implement than other communication protocols, especially for small-scale projects or with microcontrollers that have limited resources.
  2. Speed: While not as fast as the SDIO (Secure Digital Input Output) for SD card communication, SPI still offers a reliable speed that is sufficient for many applications.
  3. Availability: Many microcontrollers come with built-in SPI support, making it accessible and convenient to develop devices that require SD card interaction.
  4. Control: With SPI, you have more control over the data exchange process, which can be beneficial in systems where precise data management is crucial.

SD Card Interfacing with STM32 using Arduino IDE

Hardware Requirements

To get started, you will need the following hardware:

  • An STM32 microcontroller board  (e.g., STM32F446ZE)
  • An SD card module with SPI interface
  • SD Card
  • Jumper wires

Software Requirements

Before we begin, make sure you have the following software installed:

  • Arduino IDE
  • STM32 Arduino Core

Connecting the Hardware

To connect the SD card module to the STM32 microcontroller, follow these steps:

  1. Connect the VCC pin of the SD card module to the 5V pin of the STM32 board.
  2. Connect the GND pin of the SD card module to the GND pin of the STM32 board.
  3. Connect the CS (Chip Select) pin of the SD card module to the PF10 of the STM32 board.
  4. Connect the SCK (Serial Clock) pin of the SD card module to the PB3 of the STM32 board.
  5. Connect the MOSI (Master Out Slave In) pin of the SD card module to the PB5 of the STM32 board.
  6. Connect the MISO (Master In Slave Out) pin of the SD card module to the PB4 of the STM32 board.

Use the SDFormatter to format the SD Card. Then insert the SD Card to the SD card module.

Setting up the Software

Once the hardware is connected, open the Arduino IDE and install the STM32 Arduino Core, and SD Library if you haven’t already. Then, create a new sketch and include the necessary libraries:

Writing the code

Include the below header files.

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

#define SD_MOSI_PIN   PB5     //MOSI Pin
#define SD_MISO_PIN   PB4     //MISO Pin
#define SD_SCLK_PIN   PB3     //Clock Pin
#define SD_SS_PIN     PF10    //Chip Select or Slave Select Pin  

We are using the PD9 and PD8 for the debug prints (UART).

HardwareSerial SerialPrint(PD9, PD8);

In the setup() function, initialize the SD card interface and the Serial for debug prints. And we are doing the below operations.

  • Then create the new file called “test.txt“.
  • Then we are writing “Welcome to EmbeTronicX!!!” to the “test.txt” file.
  • Close that file and reopen it.
  • Read the file and print the contents.
void setup()
{
  SerialPrint.begin(115200);

  SerialPrint.print("Initializing SD card...");

  SPI.setMISO(SD_MISO_PIN);
  SPI.setMOSI(SD_MOSI_PIN);
  SPI.setSCLK(SD_SCLK_PIN);

  if (!SD.begin(SD_SS_PIN)) 
  {
    SerialPrint.println("SD card Initialization failed!");
    while (1);
  }
  SerialPrint.println("SD Card Initialization done.");

  myFile = SD.open("test.txt", FILE_WRITE);

  if (myFile) 
  {
    SerialPrint.print("Writing to test.txt...");
    myFile.println("Welcome to EmbeTronicX!!!");

    myFile.close();
    SerialPrint.println("done.");
  } 
  else 
  {
    SerialPrint.println("SD Card - Error opening test.txt");
  }

  myFile = SD.open("test.txt");
  if (myFile) 
  {
    SerialPrint.println("Reading from test.txt:");
    while (myFile.available()) 
    {
      SerialPrint.write(myFile.read());
    }
    myFile.close();
  } 
  else 
  {
    SerialPrint.println("Error opening test.txt");
  }
}

Full source code – Arduino STM32 SD card Example

You can find the full source code below.

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

#define SD_MOSI_PIN   PB5     //MOSI Pin
#define SD_MISO_PIN   PB4     //MISO Pin
#define SD_SCLK_PIN   PB3     //Clock Pin
#define SD_SS_PIN     PF10    //Chip Select or Slave Select Pin  

File myFile;

HardwareSerial SerialPrint(PD9, PD8);

void setup()
{
  SerialPrint.begin(115200);

  SerialPrint.print("Initializing SD card...");

  SPI.setMISO(SD_MISO_PIN);
  SPI.setMOSI(SD_MOSI_PIN);
  SPI.setSCLK(SD_SCLK_PIN);

  if (!SD.begin(SD_SS_PIN)) 
  {
    SerialPrint.println("SD card Initialization failed!");
    while (1);
  }
  SerialPrint.println("SD Card Initialization done.");

  myFile = SD.open("test.txt", FILE_WRITE);

  if (myFile) 
  {
    SerialPrint.print("Writing to test.txt...");
    myFile.println("Welcome to EmbeTronicX!!!");

    myFile.close();
    SerialPrint.println("done.");
  } 
  else 
  {
    SerialPrint.println("SD Card - Error opening test.txt");
  }

  myFile = SD.open("test.txt");
  if (myFile) 
  {
    SerialPrint.println("Reading from test.txt:");
    while (myFile.available()) 
    {
      SerialPrint.write(myFile.read());
    }
    myFile.close();
  } 
  else 
  {
    SerialPrint.println("Error opening test.txt");
  }
}

void loop() 
{
}

Testing the Code

Upload the code to your STM32 board and open the Serial Monitor in the Arduino IDE. You should see the messages indicating the success or failure of the SD card initialization and the writing of data to the SD card. Check the below image for the output.

By following these steps, you can successfully interface an SD card with an STM32 microcontroller using the Arduino framework and the SPI interface. This allows you to easily store and retrieve data from an SD card in your STM32-based projects.

Remember to properly handle error conditions and ensure that the SD card module is compatible with the SPI interface of your STM32 board.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials – STM32Zephyr RTOS Tutorials – ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Table of Contents