Facing Issue with connect SD card of ILI9341 with ESP32

Hi All,

I am using 2.4" TFT Display with drive ILI9341 on ESP32. I am trying to read and write the SD card in TFT Display, but I am getting the same error that "SD Card initialization failed!".

Can you pls suggest how can it be resolved? Below is my pin configuration.

VCC - 5V
GND - GND
CS - G5
Reset - G4
DC - G19
SDI - G23
SCK - G18
LED- 3.3 V

SD_CS - G15
SD_MOSI - G13
SD_MISO - G12
SD_SCK - G14

I also tried connecting jumper J1, but that too is not working.

Could be a problem with your code.

See information on posting code etc here;

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966

which specific ESP32 and TFT display are you using ? give a link?

I connected to the SD card on a ESP32 TFT based display so

// TFT pins
// #define TFT_MISO 19  // (leave TFT SDO disconnected if other SPI devices share MISO)
// #define TFT_MOSI 23
// #define TFT_SCLK 18
// #define TFT_CS   15  // Chip select control pin
// #define TFT_DC   2  // Data Command control pin
// #define TFT_RST  4  // Reset pin (could connect to RST pin)
// VCC to 5V if J1 open else 3.3V

TFT_eSPI tft = TFT_eSPI();  // Invoke custom library

// SD card pins
#define HSPI_MISO 27  // note 12 causes problems when RST pressed
#define HSPI_MOSI 13
#define HSPI_SCLK 14
#define HSPI_SS 26

void setup() {
  Serial.begin(115200);
  hspi = new SPIClass(HSPI);                              // create SPI class
                                                          //hspi->begin();
  hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS);  //SCLK, MISO, MOSI, SS
  if (!SD.begin(HSPI_SS, *hspi)) {                        // mount card
    Serial.println("Card Mount Failed");
    return;
  }

I am using below ESP32. TFT is same as the link you had shared in the comment.

https://robu.in/product/esp32-38pin-development-board-wifibluetooth-ultra-low-power-consumption-dual-core/

I am still facing the same issue. Tried below 2 codes with different libraries. My J1 is closed so using 3.3 as VCC.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SD.h>

// Pin definitions
#define TFT_CS    15
#define TFT_RST   4
#define TFT_DC    2
#define SD_CS     16  // Changed CS to GPIO 16

// Initialize the TFT display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

void setup() {
  // Start Serial Monitor
  Serial.begin(115200);
  
  // Initialize the TFT display
  tft.begin();
  tft.setRotation(3);  // Adjust rotation if needed
  tft.fillScreen(ILI9341_WHITE);  // Clear the screen with white color
  tft.setTextColor(ILI9341_BLACK);
  tft.setTextSize(2);
  tft.setCursor(10, 10);
  tft.println("TFT Display Initialized!");

  // Initialize SPI settings for SD card
  SPI.begin(18, 19, 23, 16);  // SCK, MISO, MOSI, CS
  
  // Initialize the SD card
  if (!SD.begin(SD_CS)) {
    Serial.println("SD Card initialization failed!");
    tft.setCursor(10, 40);
    tft.println("SD Card Error");
    return;
  }
  Serial.println("SD Card initialized successfully.");
  tft.setCursor(10, 40);
  tft.println("SD Card Ready!");
}

void loop() {
  // Optional: Add any code to interact with the SD card here, e.g., read/write files
}
#include <TFT_eSPI.h>
#include <SPI.h>
#include <SD.h>

// Pin definitions
#define TFT_CS    15
#define TFT_RST   4
#define TFT_DC    2
#define SD_CS     16  // Changed CS to GPIO 16

// Initialize the TFT display
TFT_eSPI tft = TFT_eSPI();  // Create TFT object

void setup() {
  // Start Serial Monitor
  Serial.begin(115200);
  
  // Initialize the TFT display
  tft.begin();
  tft.setRotation(3);  // Adjust rotation if needed
  tft.fillScreen(TFT_WHITE);  // Clear the screen with white color
  tft.setTextColor(TFT_BLACK);
  tft.setTextSize(2);
  tft.setCursor(10, 10);
  tft.println("TFT Display Initialized!");

  // Initialize SPI settings for SD card
  SPI.begin(18, 19, 23, 16);  // SCK, MISO, MOSI, CS
  
  // Initialize the SD card
  if (!SD.begin(SD_CS)) {
    Serial.println("SD Card initialization failed!");
    tft.setCursor(10, 40);
    tft.println("SD Card Error");
    return;
  }
  Serial.println("SD Card initialized successfully.");
  tft.setCursor(10, 40);
  tft.println("SD Card Ready!");
}

void loop() {
  // Optional: Add any code to interact with the SD card here, e.g., read/write files
}

never attempted to run the TFT and SD card off the same SPI bus
if the SD card is sharing a SPI bus with other devices there can be problems - see Espressif: Sharing the SPI bus among SD card and other SPI devices
I tend to use a 5V supply (jumper J1 open)

So, for the clarification do you suggest to run the same set of code with 5V - J1 Open?

try it! it could be the ESP32 3.3V cannot supply sufficient power for the TFT and SD card

Thank you for the suggestion!

I tried, with 5V. removed the bridge. Ran both the codes, still the same issue. I am not sure where am I going wrong.

VCC 5V
GND GND
CS 15
RESET 4
DC 2
SDI 23
SCK 18
LED 5V
SD_SCK 14
SD_MISO 27
SD_MOSI 13
SD_CS 26

By default, the SD library uses the VSPI bus (not HSPI). Since your SD card is wired to HSPI (GPIO 12, 13, 14, 15), I guess you need to specify this in your code.

Some issues with the details I shared. Below is the correct pin config I am using.

VCC 5V
GND GND
CS 15
RESET 4
DC 2
SDI 23
SCK 18
LED 5V

SD_SCK 14
SD_MISO 27
SD_MOSI 13
SD_CS 26

Below code worked

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

// Pin definitions based on your updated configuration
#define TFT_CS    15
#define TFT_RST   4
#define TFT_DC    2
#define SD_CS     26  // Changed SD card CS to GPIO 26
#define SD_SCK    14  // Changed SD SCK to GPIO 14
#define SD_MISO   27  // Changed SD MISO to GPIO 27
#define SD_MOSI   13  // Changed SD MOSI to GPIO 13

// Initialize the TFT display
TFT_eSPI tft = TFT_eSPI();  // Create TFT object

void setup() {
  // Start Serial Monitor
  Serial.begin(115200);
  
  // Initialize the TFT display
  tft.begin();
  tft.setRotation(3);  // Adjust rotation if needed
  tft.fillScreen(TFT_WHITE);  // Clear the screen with white color
  tft.setTextColor(TFT_BLACK);
  tft.setTextSize(2);
  tft.setCursor(10, 10);
  tft.println("TFT Display Initialized!");

  // Initialize SPI settings for SD card
  SPI.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);  // SCK, MISO, MOSI, CS
  
  // Initialize the SD card
  if (!SD.begin(SD_CS)) {
    Serial.println("SD Card initialization failed!");
    tft.setCursor(10, 40);
    tft.println("SD Card Error");
    return;
  }
  Serial.println("SD Card initialized successfully.");
  tft.setCursor(10, 40);
  tft.println("SD Card Ready!");
}

void loop() {
  // Optional: Add any code to interact with the SD card here, e.g., read/write files
}


VCC	5V
GND	GND
CS	15
RESET	4
DC	2
SDI	23
SCK	18
LED	5V
SD_SCK	14
SD_MISO	27
SD_MOSI	13
SD_CS	26

Thank you so much everyone for your help!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.