1

I have an Arduino NANO clone and a 2.4" ILI9341 TFT display. The VCC and LCD pins of the display are connected to the 5V pin of the board. The GND to the GND. All the logic pins are connected to voltage dividers (2k2 and 4k3 resistors), their connections checked and delivering 3.3V when 5V is supplied to them.

If I understand the source correctly, if I specify all the pins to the constructor, then I use software SPI, but if I don't, then I use hardware SPI.

Now, if I use the software SPI, everything is working fine, I can get the info, I can draw, etc. However if I use the hardware SPI then it is not giving me anything.

#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>

// Adafruit_ILI9341 tft = Adafruit_ILI9341(10, 9);              // NOT WORKING!
Adafruit_ILI9341 tft = Adafruit_ILI9341(10, 9, 11, 13, 8, 12);  // WORKING!

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

    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);
}

void
loop()
{
    // pass
}

However, what really makes me totally confused is when I use the SD card reader with the SD Library alone (the SD card reader is on the ILI9341 board), then that is working fine as well (as if the hardware SPI would be working, unless the SD Library is switching to software SPI magically). Anyway, the following is the working code:

#include <SD.h>
#include <SPI.h>

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

    Serial.println("ILI9341 Test!");

    if (!SD.begin(7))
    {
        Serial.println("Cannot initialise SD card");
        while (true);
    }

    File file;

    if ((file = SD.open("file.txt", FILE_WRITE)))
    {
        Serial.print("Writing to file...");
        file.println("Hello, World!");
        file.close();
        Serial.println(" DONE");
    }
    else
        Serial.println("Cannot open file for writing");

    if ((file = SD.open("file.txt")))
    {
        Serial.println("Opening file...");
        while (file.available())
            Serial.write(file.read());
        file.close();
        Serial.println("DONE");
    }
    else
        Serial.println("Cannot open file for reading");
}

void
loop()
{
    // pass
}

So what exactly is going on here? Am I doing something obviously wrong? Or is the board's hardware SPI not working? Or is there a problem with the Adafruit library?


Extra info:

I'm using the arduino-cli to upload sketches the following ways:

# To compile:
arduino-cli                                     \
    compile                                     \
    --fqbn arduino:avr:nano:cpu=atmega328old    \
    --warnings all                              \
    MyArduinoSketch;

# To upload
sudo arduino-cli                                \
     upload                                     \
     --verify                                   \
     --verbose                                  \
     --port /dev/ttyUSB0                        \
     --fqbn arduino:avr:nano:cpu=atmega328old   \
     MyArduinoSketch;

I installed the mentioned libraries via arduino-cli.

  • if you test the display on hardware SPI without SD library, is SD card inserted? – Juraj Dec 31 '18 at 14:00
  • the adafruit display has internal logic level conversion and arduino will read 3.3 V as HIGH (even if it is out of limit specified in the datasheet). you don't need voltage dividers. – Juraj Dec 31 '18 at 14:06
  • Hint: You might need to set the SD chip select pin to OUTPUT and HIGH when using the TFT only with HW SPI. Especially if a SD card is inserted. – Mikael Patel Dec 31 '18 at 14:12
  • @MikaelPatel, only if the card is inserted. without the card there is no device – Juraj Dec 31 '18 at 14:41
  • 1
    I wouldn't use a resistive divider on SPI. The resistors coupled with the gate capacitance form a low-pass filter limiting the maximum frequency you can reliably communicate at. Using software SPI you are going a lot slower, so it works better. You should use a proper active level shifter. – Majenko Dec 31 '18 at 21:54
2
+100

Use constructor Adafruit_ILI9341(10, 9, 8); to make the reset that works with software SPI.

To SD card as second device on the bus:

Every SPI device on SPI bus must be controlled by slave select pin. If a device is connected to the bus and the SS pin is left floating the device disturbs the bus.

The empty SD card adapter is not a device on the bus, only the inserted card is a device. If it is on the bus the corresponding SS pin must be set HIGH. If you use the SD libray SD.begin(SD_SS_PIN), the library initializes the pin. If you don't use the library in the sketch (because it takes much memory), remove the SD card or set the SS pin to OUTPUT and HIGH.

0

Hi Peter First Adafruit spends a lot of time creating their libraries and i do appreciate that but my luck is that I never get anything working with their libraries it is just me

I use this library:

https://github.com/Bodmer/TFT_ILI9341

and it works wonders if I want to add other SPI devices like your SD card I edit the User_Setup File that is included in your sketch right at the bottom of the file you can find the following line:


// #define SUPPORT_TRANSACTIONS

uncomment that line and your Touch part as well as SD card will functional normally I have had a setup working with the screen, touch controller, RFID board and a SD card all working on SPI each with their own SS or Chip select pin

NB! if you are going to try the library it is best you remove the ADAfruit one first because there are some files with the same names and the IDE tends to include the first one it finds AKA Adafruit

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.