SD card access via SPI

Post here first, or if you can't find a relevant section!
Post Reply
cagz
Posts: 6
Joined: Thu Aug 03, 2023 12:26 pm

SD card access via SPI

Post by cagz »

I am trying to get SD Card working, using SPI on an STM32L052K8 board (custom built).

Following code is simply giving initialisation error (simply fails at SD.begin stage).

Do I need to do anything else STM32 specific here ? Especially in terms of which SPI pins this MPU uses (but I suppose that's defined in STM32duino headers ?

I appreciate any idea on how to troubleshoot next. Wirings are double-checked but attaching a copy of it as well in case you see something obvious.

Code: Select all

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

File myFile;

void setup() {

  Serial.begin(9600);

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

  if (!SD.begin(PA8)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

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

  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");

    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening test.txt");
                                                                                                                                                                                }

  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    while (myFile.available()) {
      Serial.write(myFile.read());
    }

    myFile.close();
  } else {

    Serial.println("error opening test.txt");
  }
}

void loop() {
 
}
Screenshot 2023-08-03 at 13.41.42.jpeg
Screenshot 2023-08-03 at 13.41.42.jpeg (63.61 KiB) Viewed 733 times
by fpiSTM » Fri Aug 04, 2023 7:51 pm
If you used generic then you probably need to redefine pins used for default SPI instance. See the wiki for this:
https://github.com/stm32duino/Arduino_C ... tance-pins
Go to full post
cagz
Posts: 6
Joined: Thu Aug 03, 2023 12:26 pm

Re: SD card access via SPI

Post by cagz »

Seems like I am the only one on the forum who had an SD card problem, I must be doing something really silly.

So far I tried smaller SD cards, a battery instead of USB thinking it might have been a voltage issue, but no luck.

There is a passive buzzer just next to the SD card slot. When the SD card is in, I hear a hiss from the buzzer and click when the device powers up. Can't tell if it is EMI or voltage drop causing this, but it is not resulting in a brownout. Tried to power the board from a 2S LiPo battery instead of USB to let it take whatever amps it needs but no difference at all
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: SD card access via SPI

Post by fpiSTM »

If you used generic then you probably need to redefine pins used for default SPI instance. See the wiki for this:
https://github.com/stm32duino/Arduino_C ... tance-pins
cagz
Posts: 6
Joined: Thu Aug 03, 2023 12:26 pm

Re: SD card access via SPI

Post by cagz »

It was exactly this and it only needed:

Code: Select all

SPI.setMISO(PB4);
SPI.setMOSI(PB5);
SPI.setSCLK(PB3);
Thanks a lot for your help !
Post Reply

Return to “General discussion”