Problem writing to SD card on Adafruit 1.44 Color TFT with Wemos Lolin ESP 32

I have a problem getting the SD card slot in the Adafruit 1.44 Color TFT to work with the Wemos Lolin ESP 32. I have tried many tutorials but am unable to get the Wemos to write a txt file on the SD card. In the current setup, the screen works fine but does not work with the SD card. I have attached the code I used below as well as my pin-to-pin connections.

Pin-to-Pin Connections
Adafruit 1.44 Color TFT -> Wemos Lolin ESP32
Vin -> 3V3
Gnd -> Gnd
SCK -> SCK/18
SO -> MISO/19
SI -> MOSI/23
TCS -> 17
RST -> 16
D/C -> 4
CCS -> SS/5/LED

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <SD.h>

// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins.  For Arduino Uno,
// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
#define TFT_CS  17  // Chip select line for TFT display
#define TFT_RST  16  // Reset line for TFT (or see below...)
#define TFT_DC   4 // Data/command line for TFT
#define SD_CS    5  // Chip select line for SD card

//Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
File logfile;

char filename[] = "test.txt";

void setup(void) {
  Serial.begin(9600);
  digitalWrite(5, LOW);
  
 
  // Init tft
  tft.initR(INITR_144GREENTAB);
 
  Serial.print("Initializing SD card...");
 
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
    return;
  }
  Serial.println("OK!");
}

void loop() {
 
  // only open a new file if it doesn't exist
  if (!SD.exists(filename)) {
    Serial.print("file doesnt exist. Trying to create... ");
    logfile = SD.open(filename, FILE_WRITE);
   
    if (!SD.exists(filename)) {
      Serial.println("couldnt create file");
      return;
    }
   
    logfile.println("A");
    logfile.println("B");
   
    logfile.close();
    Serial.println("file created");
  } else Serial.println("file existed");
 
  Serial.print("Logging to: ");
  Serial.println(filename);
 
  logfile = SD.open(filename, FILE_WRITE);
  if (logfile) {
    logfile.println("A");
    logfile.println("B");
    logfile.close();
  } else Serial.println("file still not exists");
 
  logfile = SD.open(filename);
  if (logfile) {
    Serial.println("test.txt:");
   
    // read from the file until there's nothing else in it:
    while (logfile.available()) {
      Serial.write(logfile.read());
    }
    // close the file:
    logfile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 
  delay(2000);
  Serial.println("###############################");
}

The thing is that when the code is modified for use with the Arduino Uno, the SD writing capabilities seems to work fine. Please help!

I'd remove the write pin 5.

I took it out, and it did not change anything. Still getting errors, unfortunately.

The errors must be a secret?

What do you mean?

Do you get a failure for the SD card initialization?

Does the file not exist printout occur?

Does the could not create file error occur?

try

void loop()
{
//comment the code you have in your loop() and add the line below.
String toFIle = String(millis());
appendFile(SD, "/data.txt", toFile.c_str() + '\n');
}

//add this to your project


void appendFile(fs::FS &fs, const char * path, const char * message) {
  //log_i("Appending to file: %s\n", path);
  File file = fs.open(path, FILE_APPEND);
  if (!file)
  {
    log_i("Failed to open file for appending");
    return;
  } else {
    file.print(message);
    file.close();
  }
}

Do you get a save to the SD card?

I tried out your code, and no file gets created. There have been on error messages in the past or with your code.

Sorry I could not help you out. Good luck.

Actually, I got it. I just had to import the FS.h library since that seems to be the file system library for ESP32 stuff. Thanks for the help, though!

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