How to set up 2.8 TFT to read file from SD card

Hello,

I’m fairly new to working with microcontrollers and sheilds, etc. I’m trying to program a screen to display a quiz-like game where questions are displayed with answer choices for the user to select. I have a text file with the questions and answer on an SD card, but the file won’t open when I try to upload (at least that’s what I think is happening. Is there something wrong with my pinouts and/or setup code?

I’ve been testing the shield with an Uno and Sparkfun RedBoard.

#include <Wire.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Adafruit_STMPE610.h"
#include <SD.h>

// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
#define TFT_DC 9
#define TFT_CS 10

#define STMPE_CS 8
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
File myFile;
//various constants and functions...

void setup() {
  Serial.begin(9600);
  Serial.println("ILI9341 Test!"); 
 
  tft.begin();
  tft.fillScreen(ILI9341_BLACK);
   
    if (!ts.begin()) {
      Serial.println("Couldn't start touchscreen controller");
      while (1);
    }
  Serial.println("Touchscreen started");
  
  tft.setRotation(3);
  
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  myFile = SD.open("ques.txt", FILE_READ);
  if (!myFile){
    Serial.println("Couldn't open file");
  }
   else {
    tft.setCursor(20, tft.height()/3);
    tft.setTextSize(5);
    tft.setTextColor(ILI9341_RED);
    tft.print("Welcome!");
    delay(3000);
    tft.fillScreen(ILI9341_BLACK);
   }
   
  
}

This is what I get in the Serial Display:

ILI9341 Test!
Touchscreen started
Initializing SD card…initialization done.
Couldn’t open file

As you can probably tell, I’ve been using examples from the ILI9341 library and used the Arduino manual to come up with the code for reading from the SD card.

Thanks in advance for your time.

pinMode(10, OUTPUT);

This should be 4, not 10.

Next.

Thank you. I just changed it but I'm still getting the same output...

How did you name your file on the SD card? ques.txt or just ques?

Make a new .txt file and name it TEST, next add in Hello World! and save the file.

Run this code.

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_STMPE610.h>
#include <SD.h>

// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
#define TFT_DC 9
#define TFT_CS 10

#define STMPE_CS 8
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
File myFile;
//various constants and functions...

void setup() {
  Serial.begin(115200);
  Serial.println("ILI9341 Test!");

  tft.begin();
  tft.fillScreen(ILI9341_BLACK);
  
  tft.setRotation(1);
  
  tft.setCursor(0, 0);
  tft.setTextColor(0xffff);
  if (!ts.begin()) {
    tft.println("Couldn't start touchscreen controller");
    while (1);
  }
  tft.print("Touchscreen started");
  
  tft.setCursor(0,20);
  tft.print("Initializing SD card...");
  pinMode(4, OUTPUT);
  if (!SD.begin(4)) {
    tft.setCursor(0,40);
    tft.setTextColor(0xffff);
    tft.println("initialization failed!");
    return;
  }
  tft.println("initialization done.");

  myFile = SD.open("test.txt"); // don't need to say FILE_READ
  if (!myFile) {
    tft.println("Couldn't open file");
  }
  else {
    tft.setCursor(20, tft.height() / 3);
    tft.setTextSize(2);
    tft.setTextColor(ILI9341_RED);
    tft.print("Welcome! ");
    while (myFile.available()) { //Print out the content of that file
      tft.write(myFile.read());
    }
    delay(3000);
    tft.fillScreen(ILI9341_BLACK);
  }
}

void loop() {}

You should see in red, Welcome! Hello World!

I named the file "ques", and saved it as a .txt file on the SD card, so the extension was added to the name.

I ran your code and it worked fine.

anazareno: I named the file "ques", and saved it as a .txt file on the SD card, so the extension was added to the name.

I ran your code and it worked fine.

Great! The reason I asked how the file was named is because many people tend to add the .txt themselves, so their file ends up looking like Test.txt.txt instead of just Test.txt.

HazardsMind: Great! The reason I asked how the file was named is because many people tend to add the .txt themselves, so their file ends up looking like Test.txt.txt instead of just Test.txt.

Ok, makes sense. It was a little confusing for me because my file browser actually shows all file extensions. But yes, it did save correctly, and the file still won't open using my own code :(. Could the size of the text file have something to do with it?

Thank you so much for all your help so far.

How did you make the text file on your SD card? If you go into the root of the SD card and right click, go down to NEW, then Text Document, you should be able to write something inside that file, save it, then see it's contents on the display.

I wrote the text file using Notepad on my computer, saved it, then copied it over to the SD card.

If that didn't work then what you can try is this. Open up the ReadWrite example sketch, and make the file you want there. Just an empty file is all you need to make. Then you use your computer to populate that file. When you put it back into your Arduino, it should work.