Cant read file from sdcard

hi im curently testing project to read data from sdcard module and i have 4GB of micro sd

here is my code

`


#include "FS.h"
#include "SD.h"
#include "SPI.h"

#include <SPIFFS.h>
#include <TFT_eSPI.h>      // Install this library with the Arduino Library Manager
                           // Don't forget to configure the driver for the display!

#include <AnimatedGIF.h>   // Install this library with the Arduino Library Manager

#define SD_CS_PIN 5 // Chip Select Pin (CS) for SD card Reader

AnimatedGIF gif;
File gifFile;              // Global File object for the GIF file
TFT_eSPI tft = TFT_eSPI(); 

const char *filename = "/x_wing.gif";   // Change to load other gif files in images/GIF
void setup()
{
  Serial.begin(115200);

 if (!SD.begin(SD_CS_PIN)) {
    Serial.println("Card Mount Failed");
    return;
  }
  Serial.println("Card initialized.");

  // List files in root directory
  //listFiles("/");













  tft.begin();
  tft.setRotation(2); // Adjust Rotation of your screen (0-3)
  tft.fillScreen(TFT_BLACK);

  // Initialize SD card
 // Serial.println("SD card initialization...");
 // SD.begin(SD_CS_PIN);




  // Initialize SPIFFS
  Serial.println("Initialize SPIFFS...");
  if (!SPIFFS.begin(true))
  {
    Serial.println("SPIFFS initialization failed!");
  }

  // Reformmating the SPIFFS to have space if a large GIF is loaded
  // You could run out of SPIFFS storage space if you load more than one image or a large GIF
  // You can also increase the SPIFFS storage space by changing the partition of the ESP32
  //
  Serial.println("Formatting SPIFFS...");
  SPIFFS.format(); // This will erase all the files, change as needed, SPIFFs is non-volatile memory
  Serial.println("SPIFFS formatted successfully.");

  // Open GIF file from SD card
  Serial.println("Openning GIF file from SD card...");
 // File sdFile = SD.open(filename);

File sdFile = SD.open(filename);





  // Create a file in SPIFFS to store the GIF
  File spiffsFile = SPIFFS.open(filename, FILE_WRITE, true);
  if (!spiffsFile)
  {
    Serial.println("Failed to copy GIF in SPIFFS!");
    return;
  }

  // Read the GIF from SD card and write to SPIFFS
  Serial.println("Copy GIF in SPIFFS...");
  byte buffer[512];
  while (sdFile.available())
  {
    int bytesRead = sdFile.read(buffer, sizeof(buffer));
    spiffsFile.write(buffer, bytesRead);
  }

  spiffsFile.close();
  sdFile.close();

  // Initialize the GIF
  Serial.println("Starting animation...");
  gif.begin(BIG_ENDIAN_PIXELS);
}

void loop()
{


  
}

// Callbacks for file operations for the Animated GIF Lobrary
void *fileOpen(const char *filename, int32_t *pFileSize)
{
  gifFile = SPIFFS.open(filename, FILE_READ);
  *pFileSize = gifFile.size();
  if (!gifFile)
  {
    Serial.println("Failed to open GIF file from SPIFFS!");
  }
  return &gifFile;
}

void fileClose(void *pHandle)
{
  gifFile.close();
}

int32_t fileRead(GIFFILE *pFile, uint8_t *pBuf, int32_t iLen)
{
  int32_t iBytesRead;
  iBytesRead = iLen;
  if ((pFile->iSize - pFile->iPos) < iLen)
    iBytesRead = pFile->iSize - pFile->iPos;
  if (iBytesRead <= 0)
    return 0;

  gifFile.seek(pFile->iPos);
  int32_t bytesRead = gifFile.read(pBuf, iLen);
  pFile->iPos += iBytesRead;

  return bytesRead;
}

int32_t fileSeek(GIFFILE *pFile, int32_t iPosition)
{
  if (iPosition < 0)
    iPosition = 0;
  else if (iPosition >= pFile->iSize)
    iPosition = pFile->iSize - 1;
  pFile->iPos = iPosition;
  gifFile.seek(pFile->iPos);
  return iPosition;
}




void listFiles(String dirname) {
  Serial.println("Listing files in directory: " + dirname);

  File root = SD.open(dirname);
  if (!root) {
    Serial.println("Failed to open directory");
    return;
  }
  if (!root.isDirectory()) {
    Serial.println("Not a directory");
    return;
  }

  File file = root.openNextFile();
  while (file) {
    if (file.isDirectory()) {
      Serial.print("  DIR : ");
      Serial.println(file.name());
    } else {
      Serial.print("  FILE: ");
      Serial.print(file.name());
      Serial.print("  SIZE: ");
      Serial.println(file.size());
    }
    file = root.openNextFile();
  }
}
`

and the questions is why the log shows

16:53:01.813 -> Openning GIF file from SD card...
16:53:02.304 -> [ 9200][W][sd_diskio.cpp:109] sdWait(): Wait Failed
16:53:02.351 -> [ 9204][E][sd_diskio.cpp:128] sdSelectCard(): Select Failed
16:53:02.351 -> [ 9210][E][sd_diskio.cpp:595] ff_sd_status(): Check status failed
16:53:02.825 -> [ 9716][W][sd_diskio.cpp:109] sdWait(): Wait Failed
16:53:02.825 -> [ 9720][E][sd_diskio.cpp:128] sdSelectCard(): Select Failed
16:53:03.329 -> [ 10226][W][sd_diskio.cpp:109] sdWait(): Wait Failed
16:53:03.371 -> [ 10230][E][sd_diskio.cpp:128] sdSelectCard(): Select Failed

16:53:03.371 -> [ 10236][E][vfs_api.cpp:99] open(): /sd/x_wing.gif does not exist, no permits for creation

otherways i described to read " const char *filename = "/x_wing.gif"; "

and the log monitor shows "/sd/x_wing.gif "

and im already place file x_wing.gif already on root of my sdcard

Welcome to the forum

You started a topic in the Uncategorised category of the forum when its description explicitly tells you not to

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

@ultima9999 have you tried any of the example SD sketches that come with the SD library? Did they work or did they also show errors when trying to access the SD card?

Is there any particular reason you are using pin 5? It's advisable to use pin 10 as that stops the SPI hardware from accidentally switching into slave mode.

not yet im use pin5 cause i have esp32 devkit v1 and im read diagrams that recommends to connect with pin 5 ? its there problem if i connect that pins ?
on my esp32 doesnt have pin 10

Does the SD card initialize or fail at the SD.begin() function call?

intialized sucess already but after intialize and formating spiffs
its show log like this

16:51:15.593 -> E (2164) task_wdt: esp_task_wdt_reset(763): task not found

16:51:15.593 -> E (2165) task_wdt: esp_task_wdt_reset(763): task not found

16:51:15.593 -> E (2166) task_wdt: esp_task_wdt_reset(763): task not found

16:51:15.626 -> E (2179) task_wdt: esp_task_wdt_reset(763): task not found

16:51:15.626 -> E (2180) task_wdt: esp_task_wdt_reset(763): task not found

16:51:15.626 -> E (2182) task_wdt: esp_task_wdt_reset(763): task not found

16:51:15.626 -> E (2195) task_wdt: esp_task_wdt_reset(763): task not found

16:51:15.626 -> E (2196) task_wdt: esp_task_wdt_reset(763): task not found

16:51:15.626 -> E (2197) task_wdt: esp_task_wdt_reset(763): task not found

whats wrong??

My apologies - I wrongly assumed you had an Arduino UNO.

Is your TFT display connected? It also uses SPI.
What pin does it use for CS?
Are you changing that pin to an OUTPUT and set it HIGH?

yes already on pin 21 for tft espi
how and where i need to set high

Is the SD card on the TFT display?

Why are you using SPIFFS?

This caused me concern. Opening the file twice?

  File sdFile = SD.open(filename);

  // Create a file in SPIFFS to store the GIF
  File spiffsFile = SPIFFS.open(filename, FILE_WRITE, true);

not twice i use spiffs for opening copied file from sd card and render to internal memory.

Are they the same file or are they two files with the same name in two different places ?

Now I haven't a clue. Don't know what the goal is.

the main problem is conflict between reading lcd and tft_espi
i was try lowering freq but stil not working

As a test, can you remove all the tft related code from the sketch, so that all it does is copy the file from the SD card to SPIFFS? Once that is verified to work, then add back the tft display to see if there is some conflict, verify that works then add back the code for the AnimatedGif.