I can't use SD card Reader in TFT LCD

Please, I want your help. I'm working on a project now.
I use the 3.5" TFT SPI 480*320. I want to use SD card reader in TFT LCD module.

I am using the code below.

#include <TFT_eSPI.h>
#define FS_NO_GLOBALS
#include <FS.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>

#define SD_CS 2

TFT_eSPI tft = TFT_eSPI();

void setup() {
  Serial.begin(115200);
  while(!Serial);
  pinMode(SD_CS, OUTPUT);
  digitalWrite(SD_CS, HIGH);
  pinMode(TFT_CS, OUTPUT);
  digitalWrite(TFT_CS, HIGH);

  Serial.println();   Serial.println();
  Serial.println("[LCD] Setup...");

  tft.init();
  tft.fillScreen(TFT_CYAN);
  Serial.println("[LCD] Success to set TFT_LCD");

  SPI.begin();
  Serial.println("[SD] Setup...");
  while(!SD.begin(SD_CS)){
    Serial.print(".");
    delay(200);
  }

  tft.setRotation(1);
  bmpDraw("/test_graphic.bmp", 0, 0);
  delay(1000);
}

void loop() {
  
}

#define BUFFPIXEL 20

void bmpDraw(char* filename, uint8_t x, uint16_t y) {

  File     bmpFile;
  int      bmpWidth, bmpHeight;   // W+H in pixels
  uint8_t  bmpDepth;              // Bit depth (currently must be 24)
  uint32_t bmpImageoffset;        // Start of image data in file
  uint32_t rowSize;               // Not always = bmpWidth; may have padding
  uint8_t  sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
  uint8_t  buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  bool     goodBmp = false;       // Set to true on valid header parse
  bool     flip = true;           // BMP is stored bottom-to-top
  int      w, h, row, col;
  uint8_t  r, g, b;
  uint32_t pos = 0, startTime = millis();

  if((x >= tft.width()) || (y >= tft.height())) return;

  Serial.println();
  Serial.print(F("Loading image '"));
  Serial.print(filename);
  Serial.println('\'');

  // Open requested file on SD card
  if ((bmpFile = SD.open(filename)) == NULL) {
    Serial.print(F("File not found"));
    return;
  }

  // Parse BMP header
  if(read16(bmpFile) == 0x4D42) { // BMP signature
    Serial.print(F("File size: ")); Serial.println(read32(bmpFile));
    (void)read32(bmpFile); // Read & ignore creator bytes
    bmpImageoffset = read32(bmpFile); // Start of image data
    Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
    // Read DIB header
    Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
    bmpWidth  = read32(bmpFile);
    bmpHeight = read32(bmpFile);
    if(read16(bmpFile) == 1) { // # planes -- must be '1'
      bmpDepth = read16(bmpFile); // bits per pixel
      Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
      if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed

        goodBmp = true; // Supported BMP format -- proceed!
        Serial.print(F("Image size: "));
        Serial.print(bmpWidth);
        Serial.print('x');
        Serial.println(bmpHeight);

        // BMP rows are padded (if needed) to 4-byte boundary
        rowSize = (bmpWidth * 3 + 3) & ~3;

        // If bmpHeight is negative, image is in top-down order.
        // This is not canon but has been observed in the wild.
        if(bmpHeight < 0) {
          bmpHeight = -bmpHeight;
          flip = false;
        }

        // Crop area to be loaded
        w = bmpWidth;
        h = bmpHeight;
        if((x + w - 1) >= tft.width()) w = tft.width() - x;
        if((y + h - 1) >= tft.height()) h = tft.height() - y;

        // Set TFT address window to clipped image bounds
        tft.setAddrWindow(x, y, x + w - 1, y + h - 1);

        for (row = 0; row < h; row++) { // For each scanline...

          // Seek to start of scan line.  It might seem labor-
          // intensive to be doing this on every line, but this
          // method covers a lot of gritty details like cropping
          // and scanline padding.  Also, the seek only takes
          // place if the file position actually needs to change
          // (avoids a lot of cluster math in SD library).
          if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
            pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
          else     // Bitmap is stored top-to-bottom
            pos = bmpImageoffset + row * rowSize;
          if(bmpFile.position() != pos) { // Need seek?
            bmpFile.seek(pos);
            buffidx = sizeof(sdbuffer); // Force buffer reload
          }

          for (col = 0; col < w; col++) { // For each pixel...
            // Time to read more pixel data?
            if (buffidx >= sizeof(sdbuffer)) { // Indeed
              bmpFile.read(sdbuffer, sizeof(sdbuffer));
              buffidx = 0; // Set index to beginning
            }

            // Convert pixel from BMP to TFT format, push to display
            b = sdbuffer[buffidx++];
            g = sdbuffer[buffidx++];
            r = sdbuffer[buffidx++];
            tft.pushColor(tft.color565(r,g,b));
          } // end pixel
        } // end scanline
        Serial.print(F("Loaded in "));
        Serial.print(millis() - startTime);
        Serial.println(" ms");
      } // end goodBmp
    }
  }

  bmpFile.close();
  if(!goodBmp) Serial.println(F("BMP format not recognized."));
}

// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.

uint16_t read16(File &f) {
  uint16_t result;
  ((uint8_t *)&result)[0] = f.read(); // LSB
  ((uint8_t *)&result)[1] = f.read(); // MSB
  return result;
}

uint32_t read32(File &f) {
  uint32_t result;
  ((uint8_t *)&result)[0] = f.read(); // LSB
  ((uint8_t *)&result)[1] = f.read();
  ((uint8_t *)&result)[2] = f.read();
  ((uint8_t *)&result)[3] = f.read(); // MSB
  return result;
}

I think I wired correctly myself.
But SD.begin() is not working.
How can I solve this problem?

Welcome! Nice job on posting the code. Sorry but I cannot see what you have done. Post an annotated schematic as how you have wired it. A frizzy picture is useless and is missing a lot of vital information. Also post links to "Technical Information" on the hardware items, links to sales outlets such as azon marketplace are useless.

I connected the pin as below with NodeMCU-32S.

TFT_MOSI = 23
TFT_MISO = 19
TFT_SCK = 18
TFT_CS = 5
TFT_DC = 21
TFT_RESET = 16

SD_MOSI = 23
SD_MISO = 19
SD_SCK = 18
SD_CS = 2

I don't use touch function in TFT_LCD module.
The link is a wiki site with information about the product I purchased.
http://www.lcdwiki.com/3.5inch_SPI_Module_ILI9488_SKU:MSP3520

I still cannot see and I will not spend the time to draw a schematic. PIn numbers mean absolutely nothing without association which a schematic gives. I have those same pin numbers on my HC05 processor with pins with the same name but different numbers associated with them. You can elect not to do the schematic etc but expect a long time to get an answer or you will give up.

Hi emv55555,

I have tinkered lately with some external SD card readers with the Arduino Nano, ESP8266 and ESP32. Maybe you will find information when you check:

Same with SD card reader on the back of TFT breakouts:

Maybe that will help!

regards, Photoncatcher

See post #4

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