cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble using SD.h and TFT_eSPI.h at the same time

YVisHere
Associate

I am using an L432KC board and I am trying to read .txt files from my SD card through SPI, extract the bitmap array, and then push the bitmap through TFT_eSPI.h library.

I am able to work with SD card and the display individually, but not together. If I run SD.begin() first, I can communicate with the SD card until I initialize the display.

If I initialize the display first, SD card initialization fails. If I still try to run non-SD operations on my display like fillscreen, its too slow.

I was originally using the same SCLK, MOSI and MISO for both and different chip selects but I couldn't successfully initialize the SD until I connected it to different MOSI, MISO and SCLK.

pins for SD

MOSI = D11

MISO = D12

SCLK = D13

CS = D3

Pins for tft-espi

TFT_MOSI=A6
TFT_MISO=A5
TFT_SCLK=A4
TFT_CS=A3
TFT_DC=A2

This is my code to initialize my sd card. printdirectory() works as expected if the initialization does not fail.

void initSD() {
    // Initialize the SD card
    if (!SD.begin(D3)) {
        Serial.println("SD card initialization failed!");
        return;
    }

    File root = SD.open("/bsr");
    

    printDirectory(root, 0);
    Serial.println("SD card initialized successfully.");
}

This is my code that initializes both. After tft.begin() I fail to open the files I could previously open

//Initialize TFT display
void initDisp(){

    digitalWrite(A3, HIGH); 
    digitalWrite(D3, HIGH); 

    initSD(); // Initialize the SD card

    if (SD.exists("/bsr/JONATHAN.JPG")) {
        Serial.println("BSR folder exists on SD card.");
    } else {
        Serial.println("BSR folder does not exist on SD card.");
    }

    //---------Until this point, expected behavior
    tft.begin();
    tft.fillScreen(TFT_BLACK);   
}
2 REPLIES 2
YVisHere
Associate

I am using build flags so that tft_espi works with the pins mentioned. Should I use build flags for SD.h as well?

I don't know these particular code snippets, but I can make a guess at what the problem could be.

My initial guess is that the TFT display and the SD card need different SPI port settings, such as clock polarity and clock phase (the SPI mode). It could also be a problem with the clock frequency, if the TFT initialisation module sets an insanely high frequency. But I would begin with the SPI mode.