ILI9341 Touchscreen doesn't work with Teensy 4.0

I am using Teensy 4.0 and library ILI9341_T4 which is written especially for this display and this microcontroller. The display itself works, it displays image properly. However, I haven't succeed in implementing touch feedback to my program. I connected the display this way:
#define PIN_SCK 13
#define PIN_MISO 12
#define PIN_MOSI 11
#define PIN_DC 10
#define PIN_CS 9
#define PIN_RESET 6
#define PIN_BACKLIGHT 255
#define PIN_TOUCH_IRQ 1
#define PIN_TOUCH_CS 2
IMAGE 2021-09-27 21:51:09.jpgIMAGE 2021-09-27 21:50:57.jpg
When I try to debug x, y, z values in the Serial Monitor I see random values.
I connected T_CLK to the same pin as SCK; T_DIN to MOSI (SDI); T_DO to MISO (SDO). Probably that's why it does not detect touch properly.
 
In all cases like this it helps if for example you say which display it is... One direct from PJRC? I assume not Adafruit version...
Product page: https://www.pjrc.com/store/display_ili9341_touch.html
Shows proper wiring, plus points you to which Touch library (XPT2046)

Also would help if you showed the code that is not working. For example which library are you using?
That is some of the original examples were setup for Adafruit library with Adafruit displays which use a different touch library.

And for example the touchpaint example sketch for the ili9341_t3 library is still setup this way.
using the library.: #include <Adafruit_STMPE610.h>

I had/have a version of this that was converted to the PJRC display, that I issued a PR for it awhile ago (2017), but closed it out a year or so ago as not sure what state it was in after a few years...
Note: It was probably pretty close to the one I have in my ili9341_t3n library: https://github.com/KurtE/ILI9341_t3n/blob/master/examples/touchpaint_xpt2046/touchpaint_xpt2046.ino
 
Hi,

The ILI9341_T4 library borrows code from Paul's XPT2046 library to drive.the.touchscren. I did not test it fully but it did work for the touch screens I have.

Did you specify that you are using the touchscreen in the TFT.begin() method (by specifying the pin for TOUCH_CS) ? Can you try the "touchscreen_calibration" example bundled with the library to see if it works ?

You can also try setting TOUCH_IRQ to 255 (to disable it) and see if this helps.
 
That type of ILI9341 screen does not have a touch panel. Photo of the back of your TFT or link to the site where you purchased it
 
The ILI9341_T4 library borrows code from Paul's XPT2046 library to drive.the.touchscren. I did not test it fully but it did work for the touch screens I have.

Hello Vindar,
I'm using your nice ILI9341_T4 library on T4.1 with a 2.8" ILI9341 + XPT2046 touch.

I'm now looking for a simple 2D canvas lib (I need a few buttons and a couple of textboxes for my simple GUI) and have found your TGX library.

Unfortunately, I did not find any example which integrates both simple 2D primitives and touch coordinates reading.
The only example I have found uses lvgl (lvgl_minimal_touch), and I would gladly avoid messing with it again. :p

Do you have sample code which reads from touch and draws say a circle with ILI9341 + TGX?

Thanks!
 
Hello Vindar,
Do you have sample code which reads from touch and draws say a circle with ILI9341 + TGX?
Thanks!

Hi !

Here is a example using TGX + ILI9341_T4 of a button that switches color when pressed.

Code:
#include <ILI9341_T4.h>
#include <tgx.h>
#include <font_tgx_OpenSans_Bold.h> // predefined TGX font

using namespace tgx; // we don't want to add tgx:: everywhere...

// DEFAULT WIRING USING SPI 0 ON TEENSY 4/4.1
#define PIN_SCK     13
#define PIN_MISO    12
#define PIN_MOSI    11
#define PIN_DC      10
#define PIN_CS      9 
#define PIN_RESET   6 
#define PIN_BACKLIGHT 255
#define PIN_TOUCH_IRQ 3
#define PIN_TOUCH_CS  2

#define SPI_SPEED       30000000

#define LX  320
#define LY  240


ILI9341_T4::ILI9341Driver tft(PIN_CS, PIN_DC, PIN_SCK, PIN_MOSI, PIN_MISO, PIN_RESET, PIN_TOUCH_CS, PIN_TOUCH_IRQ);

ILI9341_T4::DiffBuffStatic<6000> diff1;
ILI9341_T4::DiffBuffStatic<6000> diff2;

uint16_t internal_fb[LX * LY];  // internally used by ILI9341_T4 for buffering
uint16_t fb[LX * LY]; // the main framebuffer we draw onto.

Image<RGB565> im(fb, LX, LY);  // encapsulates the framebuffer inside this image: drawing on 'im' directly draws inside 'fb'. 


void setup()
    {
    Serial.begin(9600);
    tft.output(&Serial);
    while (!tft.begin(SPI_SPEED));
    tft.setRotation(3);
    tft.setFramebuffer(internal_fb);
    tft.setDiffBuffers(&diff1, &diff2);
    tft.setDiffGap(4);
    tft.setRefreshRate(120);
    tft.setVSyncSpacing(2);
    if (PIN_BACKLIGHT != 255)
        {
        pinMode(PIN_BACKLIGHT, OUTPUT);
        digitalWrite(PIN_BACKLIGHT, HIGH);
        }
    
    tft.calibrateTouch();                           // run calibration...
    //int touch_calib[4] = {3776, 314, 3941, 440 }; // ...or directly load calibration data
    //tft.setTouchCalibration(touch_calib);         // if the values are already known for this screen...
    }



iBox2 B(80, 240, 80, 160); // bounding box for the button

bool need_redraw = true; // should we redraw the screen ? 
bool touched = false;    // is the screen currently being touched
int cc = 0; // circle color: 0 = Red 1 = Blue

void loop()
    {
    int z;
    iVec2 tpos; 
    if (tft.readTouch(tpos.x, tpos.y, z))
        { // screen is being touched
        if ((!touched)&&(B.contains(tpos)))
            {
            cc = 1 - cc; 
            need_redraw = true; 
            touched = true; 
            }
        }
    else touched = false;

    if (need_redraw)
        { // redraw the screen when required. 
        im.clear(RGB565_Black);
        im.fillThickRoundRectAA(B, 20, 3,  (cc == 0) ? RGB565_Red : RGB565_Blue, RGB565_White);
        im.drawTextEx("Touch me !", B.center(), Anchor::CENTER, font_tgx_OpenSans_Bold_24, false, false, RGB565_White, 1.0f);
        tft.update(fb);
        need_redraw = false;
        }
    }

However, if you want to try this code, you must pull the 'improved-drawing-primitives' branch of TGX on Github (and not the 'main' branch) as I have almost completed a major upgrade for TGX which creates several breaking changes... I will merge this branch with main in a few days if all goes well.

The good news is that the new version of the library will come with an extensive online documentation :) Here is the link to the preliminary version.
 
Back
Top