microSD card module and 2.2" 18-bit color TFT LCD display not working together

When attempting to write to the external microSD module fails when the screen is also driven. Only during initialisation before writing to the screen can write to the sd card. I cannot currently find a cause for this behaviour.

#include <Arduino.h>

// clock module
#include "clock.h"

// LCD
void LCDSetup();

#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "SPI.h"

#define TFT_DC 7
#define TFT_CS 3
#define TFT_RST 5

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

void writeLine1();
void writeLine2();
void writeLine3();

char previousLine1[100];
char previousLine2[100];
char previousLine3[100];

unsigned long previousMillis = 0;

uint8_t previousSecond = 0;
void writeDataToScreen();

// SD
const int chipSelect = 4;
#include <SdFat.h>
SdFat sd;
SdFile myFile;

void SDSetup();
void testWriteToSd();

void setup() {
  Serial.begin(9600);
  while (!Serial) {
  }
  Serial.println();

  clockSetup();
  LCDSetup();
}

void loop() {
  writeDataToScreen();
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= 1000) {
    testWriteToSd();
    previousMillis = currentMillis;
  }
}

void writeDataToScreen() {
  writeLine1();
}

void writeLine1() {
  uint16_t year = getYear();
  uint8_t month = getMonth();
  uint8_t day = getDay();
  uint8_t hour = getHour24();
  uint8_t minute = getMinute();
  uint8_t second = getSecond();

  char line1[100];
  snprintf(line1, 100, "%04d-%02d-%02d T%02d:%02d:%02d", year, month, day, hour,
           minute, second);

  if (previousSecond != second) {
    previousSecond = second;
    tft.setCursor(0, 0);
    tft.setTextColor(ILI9341_BLACK);
    tft.setTextSize(2);
    tft.println(previousLine1);
    strcpy(previousLine1, line1);
    tft.setCursor(0, 0);
    tft.setTextColor(ILI9341_GREEN);
    tft.setTextSize(2);
    tft.println(line1);
  }
}

void LCDSetup() {
  tft.begin();
  tft.fillScreen(ILI9341_BLACK);
}

void SDSetup() {
  if (!sd.begin(chipSelect, SPI_EIGHTH_SPEED)) {
    sd.initErrorHalt();
  }
}

void testWriteToSd() {
  if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt(F("opening test.txt for write failed"));
  }
  Serial.println(F("Writing to test.txt..."));
  myFile.println(F("testing 1, 2, 3."));
  myFile.close();
  Serial.println(F("done writing."));
}

They are both connected to the SPI pins, right? Did you verify that it doesn't confict? Please post a good wiring diagram (no Fritzing or verbal descriptions please...).

The display is connected via the Adafruit EYESPI Breakout Board.

Thank you. Where in the code is the SD chip select signal CS on pin D4 specified? Never mind, I see it now...

Which Arduino?

The TFT and SD libraries use up a lot of RAM, and you may be running out.

1 Like

Arduino Mega 2560 Rev3

Is there a way to make it work?

Basically since they share the same SPI port, you will have to manually enable these devices. easiest is o do this in the functions that deal with one of the devices and i suggest the sd card.
so

void SDSetup() {
  digitalWrite(TFT_CS, HIGH);  // the chipselect is active LOW
  if (!sd.begin(chipSelect, SPI_EIGHTH_SPEED)) {
    sd.initErrorHalt();
  }
  digitalWrite(chipselect, HIGH);
  digitalWrite(TFT_CS, LOW);
}

and

void testWriteToSd() {
  digitalWrite(TFT_CS, HIGH);  
  digitalWrite(chipselect, LOW);
  if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt(F("opening test.txt for write failed"));
  }
  Serial.println(F("Writing to test.txt..."));
  myFile.println(F("testing 1, 2, 3."));
  myFile.close();
  Serial.println(F("done writing."));
  digitalWrite(chipselect, HIGH);
  digitalWrite(TFT_CS, LOW);
}

I tried this solution but it doesn't work. I think the libraries implement this by default.

I get the SdError: 0X2B,0X0 error in the sdfat library.

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