ILI9341 and SD Card not working

Hi
I am using an Arduino Nano (3.3 V, nano33IOT or RP2040 Wifi) at hand) and am trying to use an ILI9341 display and a SD card at the same time.
Adafruit ILI9341 and Arduino SD libraries
Individually they work (sdinfo or similar programs work, I can display stuff on my display). I am calling the display library first, as soon as the SD library is initialized, the display is no longer working. The code continues to execute, it writes to the SD card but there's no visual feedback any more.
I am aware of several threads asking a similar (the same) question and I have tried (most all) the things I came across.
(3V3 regulator bridged (J1 soldered in as display/SD are driven by 3V3, I even soldered a pullup Resistor on the display CS, that didn't help; there is a 10 k pullup on the SD CS on board)

I have a small code to demonstrate,
on the display I read
"Hello
I2C Done
Serial up!
starting SD
(and then it should continue with "SD online"...)

on the serial port I read

" Coffee machine I/F booting
Initializing SD Card
SD Card Initialized
finished SD
Display should have reset now"
but the display doesn't blank before the last line.

I tried manually setting cs of the display and of the sd card high/low before changing to the relevant part of the code.

If someone has an Idea where to look, I would be very grateful.
Picture of the display hanging at line before SD initializes here

//debug defines
#define FTDI

//I2C
//#include <Wire.h>
#include <WiFiNINA.h>


#include <avr/dtostrf.h>

//Display and SD card
#include <SPI.h>
#include <Adafruit_GFX.h>

#include <Adafruit_ILI9341.h>
#include <Adafruit_ILI9341_Controls.h>
#define TFT_DC 9
#define TFT_CS 7
#define TFT_BACKLIGHT 10
#define TFT_MOSI 11
#define TFT_CLK 13
#define TFT_RST 8
#define TFT_MISO 12

#include <SD.h>
#define SD_CCS 6
char fileName[16];
uint16_t fileNumber = 0;
File dataFile;


#define RST_PIN -1


// create the display object
Adafruit_ILI9341 Display = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
//Adafruit_ILI9341 Display(TFT_CS, TFT_DC, TFT_RST);



void setup() {
  //Pins
  pinMode(SD_CCS, OUTPUT);
  pinMode(TFT_CS, OUTPUT);


#if defined(TFT_BACKLIGHT)
  pinMode(TFT_BACKLIGHT, OUTPUT);
  // digitalWrite(TFT_BACKLIGHT, HIGH);
  analogWrite(TFT_BACKLIGHT, 64);
#endif


  // fire up the display
  Display.begin();
  Display.setRotation(3);
  Display.fillScreen(C_BLACK);
  Display.setTextSize(2);
  Display.println("Hello");

  Wire.begin();
  Display.println("I2C Done");

  //Serial
  Serial.begin(115200);
  Serial.println("Coffee machine I/F booting");
  Display.println("Serial up!");

  Display.println("starting SD");
  Serial.println("Initializing SD Card");
  if (!SD.begin(SD_CCS)) {
    Serial.println("SD Fail");
    Display.println("SD Fail, Card?");
  } else {
    Serial.println("SD Card Initialized");
    Display.println("SD Online");
  }

  //Checking for Files on SD, increment Filename by 1
  snprintf(fileName, sizeof(fileName), "data%03d.dat", fileNumber);  //data000.dat first file
  while (SD.exists(fileName)) {
    fileNumber++;
    Serial.println(fileNumber);
    snprintf(fileName, sizeof(fileName), "data%03d.txt", fileNumber);
  }
  Display.println(fileName);
  Serial.println("finished SD");


  //I2C

  delay(2000);
  Display.fillScreen(C_BLACK);
  Serial.println("Display should have reset now");
  Display.setTextSize(1);
  Display.println("More");
}

void loop() {
}

They both use SPI, right? Do they have separate SPI select lines, and do the devices allow multiple devices on the SPI bus?

How are you SD and display wired? Pinout? Diagram? Please read

Hello aarg

Thank you very much for your answer.

I am sorry, they are both on the same SPI bus, SD_CS D6, TFT_CS D7, I attached the schematic. I would hope the nano (as well as the ILI9341 having a SD onboard allow several devices)
I tested with the nano33 and the nanoRP2040
Schematic here:
output.pdf (122.4 KB)

Board and code will be published on github as soon as I get everything working, the I2C devices (culled from code) are all working fine :slight_smile:

And here is the output from SD / cardinfo to show that the sd card individually works, too:

Card type:         SDHC
Clusters:          124544
Blocks x Cluster:  64
Total Blocks:      7970816

Volume type is:    FAT32
Volume size (Kb):  3985408
Volume size (Mb):  3892
Volume size (Gb):  3.80

Files found on the card (name, date and size in bytes): 
PRIVATE/      2021-03-13 12:29:48
  SONY/         2021-03-13 12:29:50
    SONYCARD.IND  1980-00-00 00:00:00 0
  AVCHD/        2021-03-13 12:29:50
    BDMV/         2021-03-13 12:29:50
      MOVIEOBJ.BDM  2021-03-13 12:29:50 58
      INDEX.BDM     2021-03-13 12:29:50 562
      PLAYLIST/     2021-03-13 12:29:50
      CLIPINF/      2021-03-13 12:29:50
      STREAM/       2021-03-13 12:29:50
AVF_INFO/     2021-03-13 12:29:50
  AVIN0001.INT  2021-03-13 12:29:50 2457600
  AVIN0001.BNP  2021-03-13 12:29:52 25165824
  AVIN0001.INP  2021-03-13 12:29:52 25165824
  PRV00001.BIN  2021-03-13 12:29:54 2160472
DATA000.TXT   2000-01-01 01:00:00 11492

If I get your code right it seems that your display is configured on software SPI while your SD card is defined as hardware SPI. I havn't tried that but I am not sure if that would work well. I suggest to search in that direction.
It would in any case be easier to have both on hardware SPI with all lines in parallel but different CS lines.

1 Like

Oh thank you very much, @MidiMan !

For the test code, a change to

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

solved it. I will check the main code and report back!

Thank you very much, that really did it. logging to SD card works!
(just took file and plotted it in xmgrace)

0	0.55	0.13	0.12	0.00	0.00	0.14
105	0.55	0.11	0.11	3.28	0.57	0.15
210	0.55	0.10	0.09	3.18	0.56	0.15
314	0.55	0.09	0.09	3.18	0.56	0.15
418	0.55	0.08	0.08	3.18	0.56	0.15
523	0.55	0.07	0.07	3.28	0.57	0.14
627	0.55	0.07	0.06	3.28	0.56	0.14
731	0.55	0.07	0.07	3.18	0.56	0.14
836	0.55	0.08	0.09	3.28	0.56	0.14
940	0.55	0.08	0.08	3.18	0.56	0.14
1040	0.55	0.09	0.09	3.43	0.60	0.12
1144	0.55	0.10	0.11	3.28	0.56	0.12
1248	0.55	0.11	0.11	3.18	0.56	0.12
...

You are welcome!

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