Searching for a method to display the directory content of an SD card directly on the TFT screen did not lead to the necessary information. All I could find was a nice sketch to show the content on the Serial Monitor using the SdFat library ( => https://www.arduino.cc/en/Tutorial/CardInfo ). However, like in this sketch, using a second Arduino board containing an SD card could successfully redirect the input stream via Serial2 to the TFT display on the first board. But it did not work for redirection to the same board.
The solution: For the redirected input stream (&tft) to the TFT board an additional print command is necessary. So this looks like => tft.println(sd.ls(&tft,LS_R)); and this works!
The minimum requirement for my 3.5 inch 8-Bit ILI Display (0x9486) from Banggood is shown in the following sketch using the MCUFRIEND_kbv library:
#include <MCUFRIEND_kbv.h>
#include <SdFat.h>
MCUFRIEND_kbv tft; SdFat sd; SdFile root;
void setup() { tft.begin(0x9486); sd.begin(10); tft.println(sd.ls(&tft,LS_R)); }
void loop() {}
A commented more comfortable sketch is shown below. The Adafruit libraries are optional for different TFT screens.
//#include <Adafruit_GFX.h> // ** Core graphics library
//#include <Adafruit_TFTLCD.h> // ** Hardware-specific library
#include <MCUFRIEND_kbv.h> // ** Some kind of universal library, also includes the GFX library
#include "SdFat.h"
// ** Pin definitions of SD Card for UNO
//#define SPI_MOSI 11 // ** for MEGA 2560 => 51
//#define SPI_MISO 12 // ** for MEGA 2560 => 50
//#define SPI_SCK 13 // ** for MEGA 2560 => 52
//#define SPI_SS 10 // ** for MEGA 2560 => 48
// ** Control pins for the LCD
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RST A4 // LCD Reset goes to Analog 4
// ** When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// ** For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8 (Notice these are
// D1 connects to digital pin 9 NOT in order!)
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
#define TextColor 0xFFF0 // ** My favorite complementary color combination
#define Background 0x000F // **
// ** TFT wiring if used with identifiers 0x9341 or 0x9481, then "setRotation" has to be adjusted
//Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RST);
MCUFRIEND_kbv tft; // ** MCUFRIEND seems to be best for indentifier 0x9486 (Banggood TFTLCD 8-Bit 320x480)
const uint8_t SD_CS = 10; // ** = SPI_SS, for MEGA 2560 pin 48
SdFat sd; // ** File system
SdFile root; // ** Directory File
void setup()
{ tft.begin(0x9486);
tft.setRotation(0);
tft.setAddrWindow(0,0,320,480);
tft.fillScreen(Background);
tft.setTextColor(TextColor);
tft.setTextSize(1);
tft.setCursor(0,0);
sd.begin(SD_CS);
tft.println(F("List of files on the SD.\n"));
tft.println(sd.ls(&tft,LS_R)); // ** sd.ls(&tft,LS_R) produces a stream for tft,
// ** but this still has to be printed!
// ** As an option one can add file size and timestamp to the output list,
// ** and even specify a distinct subdirectory, eg. "myDir"
// tft.println(sd.ls(&tft, "/myDir", LS_R | LS_SIZE | LS_DATE ));
// ** Since the output produces a number "1" at the end if successful,
// ** this is blanked out with the following optional commands
int cursorPos = tft.getCursorY();
tft.fillRect(0, cursorPos-8, 319, 8, Background);
tft.setCursor(0,cursorPos);
// **
tft.println(F("Done!\n"));
}
void loop() {}
Have fun.