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.
Table of Contents
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:
- 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.
- 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.
- Availability: Many microcontrollers come with built-in SPI support, making it accessible and convenient to develop devices that require SD card interaction.
- 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:
- Connect the VCC pin of the SD card module to the 5V pin of the STM32 board.
- Connect the GND pin of the SD card module to the GND pin of the STM32 board.
- Connect the CS (Chip Select) pin of the SD card module to the PF10 of the STM32 board.
- Connect the SCK (Serial Clock) pin of the SD card module to the PB3 of the STM32 board.
- Connect the MOSI (Master Out Slave In) pin of the SD card module to the PB5 of the STM32 board.
- 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.
Embedded Software | Firmware | Linux Devic Deriver | RTOS
Hi, I am a tech blogger and an Embedded Engineer. I am always eager to learn and explore tech-related concepts. And also, I wanted to share my knowledge with everyone in a more straightforward way with easy practical examples. I strongly believe that learning by doing is more powerful than just learning by reading. I love to do experiments. If you want to help or support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning!