Touchscreen problem Teensy 4.1, ILI9341t3n library and XPT2046 touchscreen

SteveW

Active member
Hi all, wonder if the experts here can help me get the ILI9341 touchscreen working. I bought a couple of chinese IL9341 boards from ebay, visually pretty identical to the part sold by PJRC in the US. I checked that the board contains a 2046 touchscreen controller, it does, though its a compatible type, HR2046. The prototype setup is using wire wrap/solder, I'm still learning the technique and have gained a better strip tool since it was constructed! I've treble beep-checked all wiring and its ok, including repeated connectivity checks topside between the Teensy 4.1 and display board. There are no adjacent pin shorts either. The SPI0 interface pins are standard as per my code comments and according to the Teensy 9341 guidance on this site. Reset is via 1k/0.1u cap rather than software. The board backlight LED is wired to a Teensy pin. Default SPI0 pins are used for the ILI9341 and controller both, SPI1 is wired to a DPS310 barometric sensor breakout. The touchscreen, display and DPS310 all have individual CS pins.

I developed some DPS310 code that outputs nicely to the ILI9341 display, using the t3n library, no problems seen. I can run the display's SPI at 100MHz fine even with the touchscreen in parallel on the bus. I've slowed that rate in the test code below though. Display refresh becomes invisible when I use buffered writes with t3n screenUpdate() but I've also not used those features in the test code below. When I ran into problems adding touchscreen code to my code I switched to trying the PS library example code below instead, it doesn't set up SPI1 so that hardware should not be relevant here.

Whenever I run XPT2046 code the touchscreen acts like its being touched even when it has not been, right from startup. It opens up with a confirmed touch response, always at -4096 x/y, ts.touched() *always* returns true even when there is no touch based on adding tests to the code. See images for output. My orientation setting is 3, I tried 1, that gave be coordinates both 8191, same false touch response, coordinates now 8191 though. The LED on CLK 13 flickers pretty constantly showing the polling. I also ran the IRQ mode test code, same result, permanent false touch response from startup. This happened both within my full code and after switching to the display board only library test code below. I've pictured both my screen and serial output. I've seen threads here implying its fine to use the t3n display library version, also I cant seem to get the t3 version to compile even though the Adafruit original version is installed here also.

I'm wondering why this is happening given the basic default type hardware and use of a library combination already documented here. There are threads online mentioning similar "false touch" responses, though no clear solutions. I've tried different display SPI rates on SPI0, no effect. Looking at both libraries they appear to be transactional, cant see why there would be a hardware SPI problem, particularly given that my board seems to run fine at 100 MHz in display mode. Considered whether noise might be a problem, does anyone here have any thoughts on adding further bypass capacitors or filtering? All ideas welcomed, I really want to use the KurtE t3n library vesion in my code and it would be great to get concurrent touchscreen use,

Steve


Code:
// Hardware:
// Teensy 4.1
//
// SPI0 interface:
// ILI9341 TFT display pins info: Vcc+5v(1), GND(2),CS(3), Reset(4) (via 1k to +3v, 0.1u cap),DC(5),SDI(6),SCK(7),Backlight LED(8),SDO(9) 
// ILI9341 touch controller pins: TCLK(10), TCS(11), TDIN(12), TDO(13), TIRQ(14)              // ILI9341 pin 1 is top, SD card skt uppermost
// Connections on Teensy:
// Teensy TFT pins: CLK to SCK 13, DI to MOSI 11, TDO to MISO 12, CS to i/o 10(CS only used in software), DC to i/o 9, LED to 7
// Teensy touch pins: TCLK to SCK 13, TCS to i/o 8, TDIN to MOSI 11, TDO to MISO 12, TIRQ to i/o 2
//
// SPI1 interface:
// DPS310 barometric sensor board, Vcc +5v, SCK 27, SDI 26, SDO 39, CS 38 (CS used only in software)


#include <ILI9341_t3n.h>
#include <ili9341_t3n_font_Arial.h>                               // from ILI9341_t3n lib
#include <XPT2046_Touchscreen.h>
#include <SPI.h>

#define LED 13                                      // LED is on pin 13 Teensy board
#define TFT_BACKLIGHT 7                      // TFT backlight
#define TFT_RESET 255                          // 255 = not controlled by library
#define TFT_MOSI 11
#define TFT_SCLK 13
#define TFT_MISO 12
#define TFT_SPI_RATE 30000000

#define CS_PIN  8
#define TFT_DC  9
#define TFT_CS 10
// 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

// ILI9341_t3n tft = ILI9341_t3n (TFT_CS, TFT_DC, TFT_RESET, TFT_MOSI, TFT_SCLK, TFT_MISO); 
ILI9341_t3n tft = ILI9341_t3n(TFT_CS, TFT_DC);

void setup() {

  pinMode(TFT_BACKLIGHT, OUTPUT);                        // TFT display backlight
  digitalWrite(TFT_BACKLIGHT, 1);                        // On

  
  Serial.begin(38400);
  tft.begin(TFT_SPI_RATE,200000);
  tft.setRotation(3);
  tft.fillScreen(ILI9341_BLACK);
  ts.begin();
  ts.setRotation(3);
  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.setFont(Arial_60);
      tft.setCursor(60, 80);
      tft.print("Touch");
    }
    tft.fillRect(100, 150, 140, 60, ILI9341_BLACK);
    tft.setTextColor(ILI9341_GREEN);
    tft.setFont(Arial_24);
    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.setFont(Arial_48);
      tft.setCursor(120, 50);
      tft.print("No");
      tft.setCursor(80, 120);
      tft.print("Touch");
    }
    Serial.println("no touch");
  }
  wastouched = istouched;
  delay(100);
}

Output from my full code:
IMG_1.jpg

Test code here output:
IMG_2.jpg

IMG_3.jpg

IMG_4.jpg
 
@SteveW:

I happen to have a project on my workbench at the moment using the Teensy4.0 + PJRC ILI9341 (+ Audio Shield, which you don't have, so I know that we are not completely comparing apples to apples). Because I'm using the Audio Shield in my project, some of my pins are required to be different from those that you are using.

Just a couple of observations/suggestions:

- since you're using the "XPT2046_Touchscreen ts(CS_PIN);" form of the TS definition/initialization (which is specifically *not* using interrupts), you should not have anything connected to the T_IRQ pin on the ILI9341 (& you can eliminate the "#define TIRQ_PIN 2"

- on your ILI9341, the following pins should be joined together (common SPI for both TFT & TS):

SDI (MOSI) <==> T_DIN
SCK <==> T_CLK
SDO (MISO) <==> T_DO


I took your code as posted & commented out the following section:

Code:
        #define LED 13                                      // LED is on pin 13 Teensy board
        #define TFT_BACKLIGHT 7                      // TFT backlight
        #define TFT_RESET 255                          // 255 = not controlled by library
        #define TFT_MOSI 11
        #define TFT_SCLK 13
        #define TFT_MISO 12
        #define TFT_SPI_RATE 30000000

        #define CS_PIN  8
        #define TFT_DC  9
        #define TFT_CS 10
        // 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

        // ILI9341_t3n tft = ILI9341_t3n (TFT_CS, TFT_DC, TFT_RESET, TFT_MOSI, TFT_SCLK, TFT_MISO); 
        ILI9341_t3n tft = ILI9341_t3n(TFT_CS, TFT_DC);

And simply substituted the following (to match my particular hardware/wiring) in its place:

Code:
#define TFT_BACKLIGHT 7                      // TFT backlight
#define TFT_SPI_RATE 30000000

const int TFT_CHIP_SELECT = 14;
const int TFT_DATA_COMMAND = 9;
ILI9341_t3n tft = ILI9341_t3n(TFT_CHIP_SELECT, TFT_DATA_COMMAND);

// when used w/ Audio Adapter, must use an alternate CS pin for the touchscreen
#define TS_CS_PIN  5

XPT2046_Touchscreen ts(TS_CS_PIN, 255);

With those few changes, everything (particularly detecting touches correctly) works as expected, so you can be relatively sure that the body of your code is not likely causing any problems. With this positive result, I would start by double-checking your wiring (again, as probably you already have, several times !!).

If you can do it without too much heartache, you might try re-wiring using the same pins (& of course, changing the definitions in your code to match) that are currently working here as follows:

10 ==> 14
08 ==> 05

Sorry I can't be of more direct help, but at least we've likely eliminated a good chunk of your test code (the main body) as a potential culprit . . .

Good luck & have fun !!

Mark J Culross
KD5RXT
 
@SteveW:

I happen to have a project on my workbench at the moment using the Teensy4.0 + PJRC ILI9341 (+ Audio Shield, which you don't have, so I know that we are not completely comparing apples to apples). Because I'm using the Audio Shield in my project, some of my pins are required to be different from those that you are using.

Just a couple of observations/suggestions:

- since you're using the "XPT2046_Touchscreen ts(CS_PIN);" form of the TS definition/initialization (which is specifically *not* using interrupts), you should not have anything connected to the T_IRQ pin on the ILI9341 (& you can eliminate the "#define TIRQ_PIN 2"

- on your ILI9341, the following pins should be joined together (common SPI for both TFT & TS):

SDI (MOSI) <==> T_DIN
SCK <==> T_CLK
SDO (MISO) <==> T_DO


I took your code as posted & commented out the following section:

Code:
        #define LED 13                                      // LED is on pin 13 Teensy board
        #define TFT_BACKLIGHT 7                      // TFT backlight
        #define TFT_RESET 255                          // 255 = not controlled by library
        #define TFT_MOSI 11
        #define TFT_SCLK 13
        #define TFT_MISO 12
        #define TFT_SPI_RATE 30000000

        #define CS_PIN  8
        #define TFT_DC  9
        #define TFT_CS 10
        // 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

        // ILI9341_t3n tft = ILI9341_t3n (TFT_CS, TFT_DC, TFT_RESET, TFT_MOSI, TFT_SCLK, TFT_MISO); 
        ILI9341_t3n tft = ILI9341_t3n(TFT_CS, TFT_DC);

And simply substituted the following (to match my particular hardware/wiring) in its place:

Code:
#define TFT_BACKLIGHT 7                      // TFT backlight
#define TFT_SPI_RATE 30000000

const int TFT_CHIP_SELECT = 14;
const int TFT_DATA_COMMAND = 9;
ILI9341_t3n tft = ILI9341_t3n(TFT_CHIP_SELECT, TFT_DATA_COMMAND);

// when used w/ Audio Adapter, must use an alternate CS pin for the touchscreen
#define TS_CS_PIN  5

XPT2046_Touchscreen ts(TS_CS_PIN, 255);

With those few changes, everything (particularly detecting touches correctly) works as expected, so you can be relatively sure that the body of your code is not likely causing any problems. With this positive result, I would start by double-checking your wiring (again, as probably you already have, several times !!).

If you can do it without too much heartache, you might try re-wiring using the same pins (& of course, changing the definitions in your code to match) that are currently working here as follows:

10 ==> 14
08 ==> 05

Sorry I can't be of more direct help, but at least we've likely eliminated a good chunk of your test code (the main body) as a potential culprit . . .

Good luck & have fun !!

Mark J Culross
KD5RXT

Hi Mark, thanks so much for tinkering with my test code. Though I’ve beep tested the connections many times I again retested MOSI,SCK,MISO just now and they are connected fine, checking on the top side of both boards to be sure there are no bad pins, both the tft and ts are definitely on the same bus. I forgot to say that in my early testing I tried all likely useful calls for the initialisation using XPT2046_Touchscreen ts(CS_pin). I’ve just now re compiled with: XPT2046_Touchscreen ts(CS_pin,255) which explicitly disables IRQ use, this gives no change in the faulty touch return. I also then commented out the #define for the IRQ to pin 2, in case the library was picking it up, again no change in the fault. If you look at the library .cpp it’s pretty clear that IRQ use only gets set up if you call with a valid IRQ pin. So, no progress so far.

That seems to leave your suggestion of changing the ts CS_PIN from Teensy 2 to 5 and the (known working) ILI9341 CS from Teensy 10 to 14. The pins I used were the same as those already tested based on ILI9341 board setup advice here, they are also just different digital I/o pins that should not really affect the result. I’d certainly swap the wiring if I though it would help but ......why would it, surely I can use any free digital pin for CS as long as each SPI device is different. It’s hard to be confident that the rewiring would help?

I’m interested that you tested on a T4.0 when my board is a 4.1. Not sure that should affect anything though.

I suppose something I should have possibly considered is that my board was supplied faulty, but I’m always reluctant to blame the tools :). Support is appreciated,

Steve
 
Steve:

Just to be sure (not wanting to take anything for granted), you typed "changing the ts CS_PIN from Teensy 2 to 5", but I'm fairly certain that you meant "changing the ts CS_PIN from Teensy 8 to 5". Assuming that the pinout on your particular ILI9341 TFT is exactly the same as the one sold by PJRC (you can check it <here>, best I can tell from your photos, nothing looks particularly out of order. I've used both T4.0 & T4.1 on previous projects, & from that limited personal experience, they should be 100% interchangeable. Since your TFT is working just fine, the only connection that would be different/unique from the pins used for the TFT would be the CS pin for the TS . . . maybe you could temporarily try changing only that one wire on the off chance that pin 8 has somehow suffered some damage in the process.

Sorry I still can't be of more direct help !!

Mark J Culross
KD5RXT
 
Steve:

Just to be sure (not wanting to take anything for granted), you typed "changing the ts CS_PIN from Teensy 2 to 5", but I'm fairly certain that you meant "changing the ts CS_PIN from Teensy 8 to 5". Assuming that the pinout on your particular ILI9341 TFT is exactly the same as the one sold by PJRC (you can check it <here>, best I can tell from your photos, nothing looks particularly out of order. I've used both T4.0 & T4.1 on previous projects, & from that limited personal experience, they should be 100% interchangeable. Since your TFT is working just fine, the only connection that would be different/unique from the pins used for the TFT would be the CS pin for the TS . . . maybe you could temporarily try changing only that one wire on the off chance that pin 8 has somehow suffered some damage in the process.

Sorry I still can't be of more direct help !!

Mark J Culross
KD5RXT


Thanks again for checking Mark! Yes a late at night typo there sorry, ts CS is definitely connected to Teensy 8, not 2! It’s always possible pin 8 has a fault. I could just switch that one as you say. I suppose the way I may be headed if no progress is building a whole new board given your comments that the code is good. I have a second ILI9341, would need another Teensy, guess I need to strongly consider the touch screen fault possibility the more I think on this,

Steve
 
I'm having the same problem with a T3.2 implementation, except it started after some period of working correctly. Now I'm getting continuous touch detections even without anything touching the display. I'll start a new topic with my hardware & software setup.
 
I'm having the same problem with a T3.2 implementation, except it started after some period of working correctly. Now I'm getting continuous touch detections even without anything touching the display. I'll start a new topic with my hardware & software setup.

Thanks I will follow that. I’m waiting on another touch screen to try here,

Steve
 
May have been a false alarm. Went back very carefully through wiring setup, and reseated everything. Seems to be working much better now. I've had it running now for 2 days straight with no problems. YMMV ;-)

Frank
 
Good news. Such is the nature of electronics :). I've checked my board a zillion times. Decided to just build another and see what gives. First supplier sent me a cracked ILI9341, waiting on a bunch from China now, at least I can pick from a selection if needed.

Steve
 
I got a cracked one also, and one of the two I got from PJRC was DOA, so I suspect that what we are getting when we buy these cheap displays is the rejects from the quality control process. I remember as a young engineer discovering that +/- 5% resistors were never closer than +/- 2% to the nominal value. Then I realized that the reason was - if a resistor measured within +/- 2% it got marketed as +/- 2% for twice the price! ;-).

Still, it's real hard for me to complain when I can get these wonderful displays so cheaply, even if I have to put up with a high defect rate.

Frank
 
Here's a shot showing my touch-screen endeavor. Seems to be working consistently now, so earlier issues were probably operator error.

IMG_4068.jpg
 

Attachments

  • IMG_4068.jpg
    IMG_4068.jpg
    113 KB · Views: 59
Here's a shot showing my touch-screen endeavor. Seems to be working consistently now, so earlier issues were probably operator error.

View attachment 23958

Looks good. Yea at about 5ukp for a TFT I could accept the odd dud amongst the Chinese ones . I think your comment re the rejects is correct, particularly from some ebay vendors. Are you using the t3n version of the 9341 library and is the touchscreen on the same SPI interface?

Spent my enforced idle time getting some much older tech working with the Teensy after I found it in a box unused here. At the price of these I didn’t expect QC issues 😀.

96444A7B-A77A-400D-A844-79CBC5C1F9CB.jpg
 
oohhh, an old 2x20 vacuum flourescent dot-matrix display. I would have killed for one of these as a design engineer in the 70's ;-)

Yes, I'm using the t3n version of the library. Don't know what you are asking about the SPI interface; I used the pinouts specified in the documents Paul provided here. I'm using the non-proportional fonts because I couldn't get the proportional fonts to work. After a few hours of operation the text started moving around on the screen, and even flipped upside down. Has to be something going on in the font library, but I didn't need anything but basic text.

Frank
 
That size of VFD now seems to retail for about 100 ukp here, the ILI is by far the more cost effective option. Just meant was touch sharing the MOSI,MISO and CLK with the display, it’s now clear it is and that is good as it’s the same configuration I’m trying to get working!

Steve
 
The code works. The touchscreen works. However, no graphics on display. I tried removing "TS_Point p = ts.getPoint();" and I am able to print graphics to screen. This maybe the problem with library
 
The code works. The touchscreen works. However, no graphics on display. I tried removing "TS_Point p = ts.getPoint();" and I am able to print graphics to screen. This maybe the problem with library

No code posted for review ... what library and update method is in use?
Normal blocking write updates can't be interrupted by touch reading?
With DMA updates any touch polling will break the background DMA transfer.

Using the INTERRUPT interface and pin allows notice of touch without polling. During reading of touch data the screen writes around the Touch reads should work without issue, but DMA would need to be disabled.
 
Back
Top