Arduino NANO with 2.8inch TFT SPI with touch

Dear community,

I have a problem with the touchscreen. Maybe somebody will know how to fix it.

I have 2.8inch TFT SPI display (ILI9341 which work without problem). Problem is with touch panel (XPT2046) connected via SPI too. I attached connection (which I checked several times). I see correct value of push force, but X and Y coordinates are not real (most of time are 31 and sometimes can reach thousands). Why? Did anyone think of how to fix it?

Here is a test code:

#include "SPI.h"
#include "Adafruit_ILI9341.h"
#include "XPT2046_Touchscreen.h"

#define LCD_cs 10
#define LCD_dc 9
#define LCD_rst 8
#define DOTYK_cs  7

Adafruit_ILI9341 displej = Adafruit_ILI9341(LCD_cs, LCD_dc, LCD_rst);
XPT2046_Touchscreen dotyk(DOTYK_cs);

void setup() {
  displej.begin();
  dotyk.begin();

  displej.setRotation(0);
  displej.fillScreen(ILI9341_BLACK);
  displej.setCursor(0, 0);
  displej.setTextColor(ILI9341_WHITE);
  displej.setTextSize(1);
  displej.println("Dotykovy displej");
  displej.setTextSize(3);
  displej.println("2.4 TFT LCD");
  displej.setTextSize(2);
  displej.println("navody.");
  displej.println("arduino-shop.cz");
}

void loop() {
  if (dotyk.touched()) {
    TS_Point bod = dotyk.getPoint();
    displej.fillRect(115, 100, 100, 50, ILI9341_BLACK);
    displej.drawRect(20, 200, 100, 100, ILI9341_RED);
    displej.drawFastHLine(10, 90, 220, ILI9341_BLUE);
    displej.drawFastVLine(110, 90, 100, ILI9341_GREEN);
    displej.setCursor(0, 100);
    displej.setTextColor(ILI9341_WHITE);
    displej.setTextSize(2);
    displej.print("   Tlak = ");
    displej.print(bod.z);
    displej.println(",");
    displej.print("Sour. x = ");
    displej.print(bod.x);
    displej.println(",");
    displej.print("Sour. y = ");
    displej.print(bod.y);
    displej.println();
    if (bod.x > 580 & bod.x < 1660 & bod.y > 2100 & bod.y < 3500 ) {
      displej.setCursor(35, 240);
      displej.print("Stisk!");
    } else {
      displej.fillRect(35, 210, 80, 80, ILI9341_BLACK);
    }
  }
  delay(1);

}

Thanks!
UK

Your voltage-dividers are a high load but should work ok. I would choose 4k7, 10k instead of 220R, 470R.
You should not have a voltage-divider on the MISO line. Just remove the 470R. Leave the 220R series resistor. It will do no harm.

Quite honestly, life is simpler with a 3.3V MCU. Then you do not need any level shifters.

David.

So, do you think the wire connection is wrong? If not, I should not get wrong XY coordinates from touch right?

I saw connections where is +3.3 to Reset pin and T_IRQ pin to Arduino. But I read this touch interruption is not working well.

Any ideas?

No, I said that your wiring was correct except for MISO.

So I changed possition of TFT display on non-soldering field and it start working (possibly short circuit in field). All work well but only Y coordinate have minimal value on left side but example code count it from right. It is possible to change this direction of X / Y axis of touch panel?

Thank you.
UK

I have two 2.4 inch XPT2046 displays.
The XPT2046 is wired differently on each screen.

Note that any Touch Panel is glued and hard-wired in one position.
e.g. X, Y might be wired for PORTRAIT on one display and LANDSCAPE on another.

If your Application is in LANDSCAPE but your XPT2046 was wired for Portrait, you need to swap X, Y.
Likewise, you probably want to calibrate your panel so the library gives you screen pixel coordinates.

Some libraries return raw XPT2046 values.
Some libraries return pixel coordinates.

You are using XPT2046_TouchScreen.h library. It looks as if you always get the raw values.
You can use the Arduino map() function to transform the raw value to a pixel coordinate.

You can use a Calibration sketch to get the TS_LEFT, TS_RT, TS_TOP, TS_BOT values for your panel.
But you can get a rough guess by just assuming TS_LEFT is about 100 and TS_RT is about 3900.

David.

Thank you for explanation. Looks like touch panel return pixel value * 10.

UK