SD bmp on TFT 480x320 ILI9486 using a Mega R3

I know this is a recurrent question...but still is very hard to find some working sketch.
I already tried tens of samples, with differente libraries (UTFT, Arduino, MCUFriend, HT...something), but all of these give some sort of errors possibly due to the various dependencies required and/or for a wrong pin definition.
In example: into the sketch 'showBMP_not_uno' from MCUFriend I do not understand these roes:

SoftSpiDriver<50,51,52> softSpi; //Bit-Bang on the Shield pins SDFat.h v2
SdFat SD;
#define SD_CS SdSpiConfig(10, DEDICATED_SPI, SD_SCK_MHZ(0), &softSpi)

I mean, where the pin 10 comes from? Is it correct for my HW?
I try to attach a picture of my TFT module; I see 4 lines going from the SD to pins 16,17,18,19.
Should I redefine the pins (where)? Specifically, how to define the CS pin?
Thanks

No. I see them on 10,11,12,13. If you connect your display correctly. Not rotated 180 degrees.

Make sure you use the actual version: showBMP_not_Uno.ino.

// NEW: install <SdFat.h> v2.x.x library with the Arduino Library Manager
// NEW: edit the src/SdFatConfig.h file to #define SPI_DRIVER_SELECT 2
SoftSpiDriver<12, 11, 13> softSpi; //Bit-Bang on the Shield pins SDFat.h v2
SdFat SD;
#define SD_CS SdSpiConfig(10, DEDICATED_SPI, SD_SCK_MHZ(0), &softSpi)

SD_CS should be named e.g. SD_BEGIN_PARAMETER to be understandable.

SdSpiConfig is defined at line 104 in SdFat/SdSpiDriver.h at master · greiman/SdFat · GitHub.

I'm sorry, I got the wrong pins from the picture; actually you are right, the pins are 10,11,12,13, so this section should be correct:

SoftSpiDriver<12, 11, 13> softSpi; //Bit-Bang on the Shield pins SDFat.h v2
SdFat SD;
#define SD_CS SdSpiConfig(10, DEDICATED_SPI, SD_SCK_MHZ(0), &softSpi)

I downloaded a fresh copy of the sketch and I just modified the bmp file name.
The SPI_DRIVER_SELECT is 2.

Your note is not clear to me.:

`SD_CS` should be named e.g. `SD_BEGIN_PARAMETER` to be understandable.

Question: I got the SdFatConfig.h, SdSpiDriver.h and sdios.h files from GitHub as standalone files, copied inside the sketch's folder. I'm not sure this is the correct mode, but I do not know any other method since there was not a zip folder containing all these libraries. Anyway, the IDE shows them, togheter with SdFat.h in the top files bar and I was able to change the SPI_DRIVER_SELECT to 2.
Compiling the '#if SPI_DRIVER_SELECT != 2' test is correct.

The final result is this on the serial monitor:

Show BMP files on TFT with ID:0x9486
cannot start SD

I got the blue screen on TFT and that's all.

Ok, I see you are missing some fundamental knowledge about use of libraries with the Arduino IDE.

There are libraries that are "known" by the Library Manager of the Arduino IDE. These should always be installed using the Library manager. SdFat and MCUFriend_kbv fall into that category. Just open Library Manager, type in the library name in the search box, and install.
Other, less common, libraries, may be available on GitHub. These can be downloaded as ZIP files, and then installed with the Library Manager using Add .ZIP Library....
Libraries should only be copied to the libraries folder, if there is no other means.
Never copy just single files from a library. Most likely this will then not compile.

BTW: I think there are tutorials about this.

Jean-Marc

I did not understand that piece of code in your post. I had to look at both libraries to find out.
Originally, the method begin() of SdFat took a pin number as parameter for the CS pin.
This method still exists.
But now there is an additional (overloaded) method:

  bool begin(SdSpiConfig spiConfig) {
    return cardBegin(spiConfig) && Vol::begin(m_card);
  }

Well, thanks for yuor support; as you see I'm new to Arduino.
I studied the tutorial and wow the libraries handling is much clear; I decided to insert the SDfat zipped folder inside my sketchbook after the mod to the SdFatConfig.h to change the SPI_DRIVER_SELECT=2. and then I added the zipped library; now I see it in the menu.

A secondary note: then sentence
'SD_CSshould be named e.g.SD_BEGIN_PARAMETER` to be understandable'
was in your first replay, not in the code...anyway, let's forget it.

Now, every should be correct, I hope, but actually the program still does not proceed.
I added a debug message into the setup to print the SPI_DRIVER_SELECT, correct, and the SD_CS parameter, with an absurd value (I was expecting 10); this is the output I get:

SPI_Driver_Select=2, SD_CS=266
Show BMP files on TFT with ID:0x9486
cannot start SD

It seems that the problem is in this piece of code:

#if SPI_DRIVER_SELECT != 2
#error edit SdFatConfig.h .  READ THE SKETCH INSTRUCTIONS
#endif
SoftSpiDriver<12, 11, 13> softSpi; //Bit-Bang on the Shield pins SDFat.h v2
SdFat SD;
#define SD_CS SdSpiConfig(10, DEDICATED_SPI, SD_SCK_MHZ(0), &softSpi)

I would really appreciate if you are so kind to support me a little more

relates to:

The meaning of SD_CS was not immediately clear. David should have used a different name.
SD_CS is an instance of the class SdSpiConfig, constructed with four parameters.

class SdSpiConfig {
 public:
   /** SdSpiConfig constructor.
   *
   * \param[in] cs Chip select pin.
   * \param[in] opt Options.
   * \param[in] maxSpeed Maximum SCK frequency.
   * \param[in] port The SPI port to use.
   */
  SdSpiConfig(SdCsPin_t cs, uint8_t opt, uint32_t maxSpeed, SpiPort_t* port) :
    csPin(cs), options(opt), maxSck(maxSpeed), spiPort(port) {}

  /** SdSpiConfig constructor.
   *
   * \param[in] cs Chip select pin.
   * \param[in] opt Options.
   * \param[in] maxSpeed Maximum SCK frequency.
   */
  SdSpiConfig(SdCsPin_t cs, uint8_t opt, uint32_t maxSpeed) :
    csPin(cs), options(opt), maxSck(maxSpeed) {}
  /** SdSpiConfig constructor.
   *
   * \param[in] cs Chip select pin.
   * \param[in] opt Options.
   */
  SdSpiConfig(SdCsPin_t cs, uint8_t opt) : csPin(cs), options(opt) {}
  /** SdSpiConfig constructor.
   *
   * \param[in] cs Chip select pin.
   */
  explicit SdSpiConfig(SdCsPin_t cs) : csPin(cs) {}

  /** Chip select pin. */
  const SdCsPin_t csPin;
  /** Options */
  const uint8_t options = SHARED_SPI;
  /** Max SCK frequency */
  const uint32_t maxSck = SD_SCK_MHZ(50);
  /** SPI port */
  SpiPort_t* spiPort = nullptr;
};

I would not have expected you can Serial.print(SD_CS), I'd expected a compiler error.

Did you put an SD card into the SD card holder, preformatted with Fat filesystem?
For the example it should have a copy of tiger.BMP, or any other BMP file(s).

I think I have to give up with the SD.
I tried almost everything I found in the example; even the simpler (and usuful I believe) which is the QuickStart.ino inside the SDfat examples folder fails, testing with CS=10 as in:

#define PIN_SPI_SS    (10)
#define PIN_SPI_MOSI  (11)
#define PIN_SPI_MISO  (12)
#define PIN_SPI_SCK   (13)

(I also tried to swap MISO and MOSI)

The fact is that I never pass then initialization phase, The test sketch always returns with

errorCode: 0x2b, errorData: 0x0

regardless of any mod attempt.
The SD is formatted in Fat32, with 3 bmp files; my PC reads and writes correctly the SD; unfortunately I do not have another SD to try, but I do not think the problem is with the card.

I think I have to give up trying to help.

QuickStart.ino may not work with SW SPI, but I am not sure.
You may need to configure SdFat for Fat32, but I don't know.

I am confident that MCUFriend_kbv and showBMP_not_Uno.ino should work correctly on Arduino MEGA with your display.
My TFT displays are not yet stored in a sorted way, so I don't know if I have this display.

Something magical happened !
I finally got some pics on the screen, but..
Firstly I made a loop in the SD initialization phase; I discovered that the very first attempt always fails. Typically it takes 2 or 3 retry before the bmp appears.

After I saw the first image I was so happy....but then...
I think to have an hardware problem somewhere since sometimes the show does not start even after tens of attempts. Moving slightly up and down the display board over the Mega seems to fix the problem. So, it works, but is unreliable. Now I ordered an SD breakout to test the SPI communication with Mega without the display, using the SD as a datalogger.
Thanks for your help.

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