Hello, I have this TFT which I am trying to run from an Arduino Nano.
I have had success with the Adafruit_ILI9341 library for the screen and the XPT2046_Touchscreen library for the touch interface. I modified an XPT library example to use my project's pins, remove the fonts, and use the adafruit library instead of whatever was default. Here's the code and pin configuration below.
ILI9341test.ino
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <XPT2046_Touchscreen.h>
#include <SPI.h>
#define CS_PIN A1
#define TFT_DC A3
#define TFT_CS A0
// MOSI=11, MISO=12, SCK=13
XPT2046_Touchscreen ts(CS_PIN);
#define TIRQ_PIN 2
//XPT2046_Touchscreen ts(CS_PIN); // Param 2 - NULL - No interrupts
//XPT2046_Touchscreen ts(CS_PIN, 255); // Param 2 - 255 - No interrupts
//XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); // Param 2 - Touch IRQ Pin - interrupt enabled polling
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
// Backlight
pinMode( 9, OUTPUT );
digitalWrite( 9, HIGH );
Serial.begin(38400);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
ts.begin();
ts.setRotation(1);
while (!Serial && (millis() <= 1000));
}
boolean wastouched = true;
void loop() {
boolean istouched = ts.touched();
if (istouched) {
TS_Point p = ts.getPoint();
if (!wastouched) {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(60, 80);
tft.print("Touch");
}
tft.fillRect(100, 150, 140, 60, ILI9341_BLACK);
tft.setTextColor(ILI9341_GREEN);
tft.setCursor(100, 150);
tft.print("X = ");
tft.print(p.x);
tft.setCursor(100, 180);
tft.print("Y = ");
tft.print(p.y);
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.println(p.y);
} else {
if (wastouched) {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_RED);
tft.setCursor(120, 50);
tft.print("No");
tft.setCursor(80, 120);
tft.print("Touch");
}
Serial.println("no touch");
}
wastouched = istouched;
delay(100);
}
Pin config
Arduino 2.4" TFT
Nano SPI 240x320
+-------+ +------------+
| 5V|-------------------------------|VCC |
| GND|-------------------------------|GND |
| A0|----5v{VOLTAGE_DIVIDER}3.3v----|CS |
| 3V3|-------------------------------|RESET |
| A3|----5v{VOLTAGE_DIVIDER}3.3v----|DC |
| D11|-+--5v{VOLTAGE_DIVIDER}3.3v----|SDI(MOSI) |
| | `-----------------------------|T_DIN |
| D13|-+--5v{VOLTAGE_DIVIDER}3.3v----|SCK |
| | `-----------------------------|T_CLK |
| D9|-------------------------------|LED |
| D12|----------------------------+--|SDO(MISO) |
| | `--|T_DO |
| A1|-------------------------------|T_CS |
| | unassigned|T_IRQ |
+-------+ +------------+
However, I'm not sure if the way it's set up right now is correct. These devices are sharing pins with the Arduino, and while they both seem to work, there are some oddities.
-
Both libraries want the initial SCLK to be two different things (touch interface is 2MHz, LCD is 16MHz). Right now I'm getting some noticeable flickering, so that could be due to a slower clock than usual. I bet if I moved the touchscreen init to happen before the lcd init, the 16MHz would be set last and persist. But then would there be issues with clocking the touch chip too fast?
-
Will sharing MISO or MOSI clash in any way here? For the display, it wouldn't usually send back any data and only needs to receive, and the opposite is true for the touch interface. That said, I couldn't get the touch to work without both T_DO and T_DIN set up, so it's probably not that simple.
Ultimately, how are you supposed to configure this thing?
If the answer involves moving the clashing pins away from SPI, I have another issue - the touch library I'm using currently seems to be SPI-only, and I haven't had success with any other touch libraries. I can post more detail about that if things seem to be moving that way.
Thanks,