ILI9341 2.2"TFT & SD writing to SD card

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
nanomax
 
Posts: 18
Joined: Wed Dec 13, 2017 8:38 am

ILI9341 2.2"TFT & SD writing to SD card

Post by nanomax »

the tft works well with adafruit 9341 lib for graphics
but when adding SD tasks, the SD card throws error after one loop.
using ESP32 pico / Arduino IDE and PlatfomIO.

when code TFT only is active ( SD code commented out ) graphics OK
when code SD only is active ( TFT code commented out) SD reading / writing / appending OK

but not both together
pls provide code example for TFT & SD writing together

thankls
rudi

User avatar
adafruit_support_mike
 
Posts: 67391
Joined: Thu Feb 11, 2010 2:51 pm

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by adafruit_support_mike »

The two are independent of each other.

The only problem I can think of is making sure you've set the CS pins properly.. make sure the ILI9341's CS is high when you want to talk to the SD card, the SD card's CS is high when you want to talk to the SD card, and each device's CS pin is low when you want to talk to it.

User avatar
nanomax
 
Posts: 18
Joined: Wed Dec 13, 2017 8:38 am

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by nanomax »

dear mike

as you can see CS is set to low -but error.
pls provide simple code :

TFT fill screen
sd append to SD
also its strange: TFT is only working when called again in loop tft.begin();
(but then SD is not working )

actually such simple example is expected to be in your lib.


thank you
rudolf
Attachments
ada tft sd.jpg
ada tft sd.jpg (302.02 KiB) Viewed 770 times

User avatar
nanomax
 
Posts: 18
Joined: Wed Dec 13, 2017 8:38 am

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by nanomax »

dear mike

any suggestions to solve this case ?

thanks
rudi

User avatar
nanomax
 
Posts: 18
Joined: Wed Dec 13, 2017 8:38 am

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by nanomax »

dear mike

any suggestions to solve this case ?

thanks
rudi

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by adafruit_support_carter »

Move the call to tft.begin() to setup(). It should only be called once in the sketch. You have it in the loop.

Do not directly control the TFT_CS pin with digitalWrite. The TFT library will take care managing that pin.

User avatar
adafruit_support_bill
 
Posts: 88040
Joined: Sat Feb 07, 2009 10:11 am

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by adafruit_support_bill »

Example code for both SD and ILI9341 use pin 10 for CS as a default. You must change one of them to use a different CS pin to avoid conflict. Be sure to change your code to match the wiring also.

User avatar
nanomax
 
Posts: 18
Joined: Wed Dec 13, 2017 8:38 am

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by nanomax »

hi bill,
using ESP32 pico , platformio

changed to 10 for TFT_CS:

#define SCK 18
#define MISO 19
#define MOSI 23
#define CS 5

#define TFT_DC 9
#define TFT_CS 10
#define TFT_MOSI 23
#define TFT_CLK 18
#define TFT_RST 15
#define TFT_MISO 19

first loop is running OK
at second loop throws error:

ld! happy World! happy World! happy World! World! World! World! happy World! happy World! happy World! happy World! World! happy World! World! World! happy World! World! World! World! happy World! World! very happy World! World! very happy World! World! very happy World! loop
Appending to file: /hello.txt
[ 25242][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x0d
[ 25243][E][sd_diskio.cpp:621] ff_sd_status(): Check status failed
[ 25544][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00
[ 25845][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00
[ 26146][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00
[ 26447][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00
[ 26447][E][vfs_api.cpp:314] VFSFileImpl(): fopen(/sd/hello.txt) failed
Failed to open file for appending
Reading file: /hello.txt
[ 26759][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00
[ 27060][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00
[ 27060][E][vfs_api.cpp:104] open(): /sd/hello.txt does not exist, no permits for creation
Failed to open file for reading
loop
--------------------------------------------------------------------------------------------------------
when commenting line 106 out
SD working well but TFT red ... dont show up.

thanks
rudi
Attachments
main.cpp
(3.06 KiB) Downloaded 10 times

User avatar
adafruit_support_bill
 
Posts: 88040
Joined: Sat Feb 07, 2009 10:11 am

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by adafruit_support_bill »

Sounds like some conflict between how the two devices are using the SPI interface.
Which SD library are you using? You are passing 3 parameters to SD.begin():

Code: Select all

if (!SD.begin(CS,SPI,80000000)) 
Arduino only documents one optional parameter to begin() for the CS pin:
https://www.arduino.cc/reference/en/libraries/sd/begin/

User avatar
nanomax
 
Posts: 18
Joined: Wed Dec 13, 2017 8:38 am

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by nanomax »

thank you bill,

YESSS the correct SD lib did the job.
and there is no need to set Chip Select to LOW when using.

proposal: add sd_logging of temp to your examples.

regards
rudi

User avatar
adafruit_support_bill
 
Posts: 88040
Joined: Sat Feb 07, 2009 10:11 am

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by adafruit_support_bill »

Good to hear that solved it. Thanks for the follow-up!

User avatar
nanomax
 
Posts: 18
Joined: Wed Dec 13, 2017 8:38 am

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by nanomax »

HI BOB;
unfortunately - NOT SOLVED.

the error messages were gone but data recording was not done.
so new setup:
ESP32 dev board, platformio

1) the example code is working only in setup but not in loop.
in loop tft.fill will not start without
tft.begin calling inside loop
( but thereafter in second loop error of SD card is thrown )

2)when commenting out tft.begin();
SD CARD data logging works, but tft screen fill is black...

pls see attached


thanks
rudi
Attachments
SD CARD recording of dataMessage.docx
data recording
(507.19 KiB) Downloaded 17 times

User avatar
adafruit_support_bill
 
Posts: 88040
Joined: Sat Feb 07, 2009 10:11 am

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by adafruit_support_bill »

Please post your complete code. Use the "</>" button above the edit window and paste your code between the (code) tags.

User avatar
nanomax
 
Posts: 18
Joined: Wed Dec 13, 2017 8:38 am

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by nanomax »

Code: Select all

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

#define TFT_MOSI  23
#define TFT_MISO  19
#define TFT_CLK   18
#define TFT_RST    4
#define TFT_DC    16
#define TFT_CS    17 

#define SD_CS      5

uint16_t loop_counter   =   0;
String   dataMessage;

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

  void screenplay()
  {
    tft.begin(20000);
    Serial.println(" ... TFT  ...");
    delay(3555);
    tft.fillScreen(ILI9341_BLACK); 
    tft.fillCircle(100, 100, 100, ILI9341_RED);
    tft.fillScreen(ILI9341_RED); 
  }
void cardplay()
{
    //----------------------------------------------------     SD    ---------------------------------------------
if(!SD.begin(5)){
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();
  delay(3333);
  if(cardType == CARD_NONE){
    Serial.println("No SD card attached");
    return;
  }
  Serial.print("SD Card Type: ");
  if(cardType == CARD_MMC){
    Serial.println("MMC");
  } else if(cardType == CARD_SD){
    Serial.println("SDSC");
  } else if(cardType == CARD_SDHC){
    Serial.println("SDHC");
  } else {
    Serial.println("UNKNOWN");
  }
  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);
}  
//----------------------------------------------  SD   ----------------------------------------
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
  Serial.printf("Listing directory: %s\n", dirname);
 
  File root = fs.open(dirname);
  if(!root){
    Serial.println("Failed to open directory");
    return;
  }
  if(!root.isDirectory()){
    Serial.println("Not a directory");
    return;
  }

  File file = root.openNextFile();
  while(file){
    if(file.isDirectory()){
      Serial.print("  DIR : ");
      Serial.println(file.name());
      if(levels){
        listDir(fs, file.name(), levels -1);
      }
    } else {
      Serial.print("  FILE: ");
      Serial.print(file.name());
      Serial.print("  SIZE: ");
      Serial.println(file.size());
    }
    file = root.openNextFile();
  }
}
void createDir(fs::FS &fs, const char * path){
  Serial.printf("Creating Dir: %s\n", path);
  if(fs.mkdir(path)){
    Serial.println("Dir created");
  } else {
    Serial.println("mkdir failed");
  }
}
void removeDir(fs::FS &fs, const char * path){
  Serial.printf("Removing Dir: %s\n", path);
  if(fs.rmdir(path)){
    Serial.println("Dir removed");
  } else {
    Serial.println("rmdir failed");
  }
}
void readFile(fs::FS &fs, const char * path){
  Serial.printf("Reading file: %s\n", path);
  File file = fs.open(path);
  if(!file){
    Serial.println("Failed to open file for reading");
    return;
  }
  Serial.print("Read from file: ");
  while(file.available()){
    Serial.write(file.read());
  }
  file.close();
}
void writeFile(fs::FS &fs, const char * path, const char * message){
  Serial.printf("Writing file: %s\n", path);
  File file = fs.open(path, FILE_WRITE);
  if(!file){
    Serial.println("Failed to open file for writing");
    return;
  }
  if(file.print(message)){
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}
void appendFile(fs::FS &fs, const char * path, const char * message){
  Serial.printf("Appending to file: %s\n", path);
  File file = fs.open(path, FILE_APPEND);
  if(!file){
    Serial.println("Failed to open file for appending");
    return;
  }
  if(file.print(message)){
      Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}

void renameFile(fs::FS &fs, const char * path1, const char * path2){
  Serial.printf("Renaming file %s to %s\n", path1, path2);
  if (fs.rename(path1, path2)) {
    Serial.println("File renamed");
  } else {
    Serial.println("Rename failed");
  }
}
void deleteFile(fs::FS &fs, const char * path){
  Serial.printf("Deleting file: %s\n", path);
  if(fs.remove(path)){
    Serial.println("File deleted");
  } else {
    Serial.println("Delete failed");
  }
}
void testFileIO(fs::FS &fs, const char * path){
  File file = fs.open(path);
  static uint8_t buf[512];
  size_t len = 0;
  uint32_t start = millis();
  uint32_t end = start;
  if(file){
    len = file.size();
    size_t flen = len;
    start = millis();
    while(len){
      size_t toRead = len;
      if(toRead > 512){
        toRead = 512;
      }
      file.read(buf, toRead);
      len -= toRead;
    }
    end = millis() - start;
    Serial.printf("%u bytes read for %u ms\n", flen, end);
    file.close();
  } else {
    Serial.println("Failed to open file for reading");
  }
  file = fs.open(path, FILE_WRITE);
  if(!file){
    Serial.println("Failed to open file for writing");
    return;
  }
  size_t i;
  start = millis();
  for(i=0; i<2048; i++){
    file.write(buf, 512);
  }
  end = millis() - start;
  Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
  file.close();
}

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

  // read diagnostics (optional but can help debug problems)
  uint8_t x = tft.readcommand8(ILI9341_RDMODE);
  Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDMADCTL);
  Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDPIXFMT);
  Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDIMGFMT);
  Serial.print("Image Format: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDSELFDIAG);
  Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX); 
  
  tft.fillScreen(ILI9341_RED);
  tft.fillScreen(ILI9341_GREEN);
  tft.fillScreen(ILI9341_BLUE);
  tft.fillScreen(ILI9341_BLACK);
 
  //----------------------------------------------------     SD    ---------------------------------------------
if(!SD.begin(5)){
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();
  delay(3333);

  if(cardType == CARD_NONE){
    Serial.println("No SD card attached");
    return;
  }

  Serial.print("SD Card Type: ");
  if(cardType == CARD_MMC){
    Serial.println("MMC");
  } else if(cardType == CARD_SD){
    Serial.println("SDSC");
  } else if(cardType == CARD_SDHC){
    Serial.println("SDHC");
  } else {
    Serial.println("UNKNOWN");
  }

  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);
  
}


void loop(void)
{
  Serial.println(  "  .........   loop    ...........");
  loop_counter = loop_counter +1;
 
  listDir(SD, "/", 0);
  delay(999);
  dataMessage =  String(loop_counter) + "\r\n";
  Serial.print(dataMessage);
  appendFile(SD, "/hello.txt", dataMessage.c_str());
  appendFile(SD, "/hello.txt", "loops : ");
  delay(999);
   tft.begin(10000000);
  tft.fillScreen(ILI9341_BLACK); 
  tft.fillCircle(100, 100, 100, ILI9341_RED);
  tft.fillScreen(ILI9341_MAGENTA);
} 

User avatar
adafruit_support_bill
 
Posts: 88040
Joined: Sat Feb 07, 2009 10:11 am

Re: ILI9341 2.2"TFT & SD writing to SD card

Post by adafruit_support_bill »

I suspect that the problem may be related to the SPI clock speed. When you call tft.begin(10000000) it is changing the clock speed for the SD interface as well. Some cards have trouble at higher SPI clock speeds. Try specifying a lower clock rate to see if the SD starts working again.

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”