ESP32 TFT GC9A01 and SD Card adapter

I'm want to display GIF using ESP32 Devkit V1, TFT GC9A01 and SD Card Adapter. I've tested the parts and working properly by individual example project on Arduino IDE. Unfortunately, everytime i put them all together the GIF not open by spiff or the SD card just not initialize.

I'm following this project to learn how it's work:

But it's not working properly.

Please extract code and schematics and post them here.

if the SD card is sharing a SPI bus with other devices there can be problems, see Sharing the SPI bus among SD card and other SPI devices
the simplest approach could be to use VSPI for the TFT GC9A01 and HSPI for the SD card

Using ESP32 Devkit V1, TFT GC9A01 and SD Card Adaptor.

TFT Pins:
VCC 3.3 with 100uF capacitor
GND, DC > D2, RST > D4, SCL > D18, CS > D22, SDA > D23.

Sd Card Adapter pins:
VCC on 3.3 with 100uF capacitor
GND, CS > D5,
SCK > D18 shared with TFT
MISO > D19
MOSI > D23 shared with TFT

#include <SD.h>
#include <SPI.h>
#include <FS.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 = "/ninja1.gif";   // Change to load other gif files in images/GIF

void listSPIFFSFiles() {
  // List all files in SPIFFS
  Serial.println("Listing files in SPIFFS...");
  File root = SPIFFS.open("/");
  File file = root.openNextFile();
  while (file) {
    Serial.print("File: ");
    Serial.println(file.name());
    file = root.openNextFile();
  }
}

void setup() {
  Serial.begin(115200);
  tft.begin();
  tft.setRotation(2); // Adjust Rotation of your screen (0-3)
  tft.fillScreen(TFT_BLACK);

  // Initialize SD card
  Serial.println("SD card initialization...");
  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("SD card initialization failed!");
    return;
  }

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

  // List files in SPIFFS for debugging
  listSPIFFSFiles();

  // Open GIF file from SD card
  Serial.println("Opening GIF file from SD card...");
  File sdFile = SD.open(filename);
  if (!sdFile) {
    Serial.println("Failed to open GIF file from SD card!");
    return;
  }

  // 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("Copying GIF to SPIFFS...");
  byte buffer[512];
  while (sdFile.available()) {
    int bytesRead = sdFile.read(buffer, sizeof(buffer));
    spiffsFile.write(buffer, bytesRead);
  }

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

  // List files again to confirm GIF is copied
  listSPIFFSFiles();

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

void loop() {
  if (gif.open(filename, fileOpen, fileClose, fileRead, fileSeek, GIFDraw)) {
    tft.startWrite(); // The TFT chip select is locked low
    while (gif.playFrame(true, NULL)) {
    }
    gif.close();
    tft.endWrite(); // Release TFT chip select for the SD Card Reader
  }
}

// Callbacks for file operations for the Animated GIF Library
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 = 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;
}

and the results in the serial monitor;

Failed to open GIF file from SPIFFS!

if the TFT supports I2C why not use the ESP32 Devkit V1 default I2C pins GPIO 21 SDA and GPIO22 SCL ?

Pin numbers have no meaning.

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