Calibration of TFT 2.4 SPI 240x320 touch

Hello,
I have a touch screen problem. Normally, point 0.0 is located in one of the corners. I find myself around the middle of the display. In the upper half I have negative values and in the bottom positive to about 700.
How can I start the coordinates in one of the corners.

I use pro mini 328 3.3V

#include <ILI9341_due.h>
#include <ILI9341_due_config.h>
#include <SystemFont5x7.h>

#include "fonts/allFonts.h"

//#include <SPI.h>
//#include "Adafruit_GFX.h"
//#include "Adafruit_ILI9341.h"
#include "URTouch.h"

#define ILI9341_ALICEBLUE 0xF7DF
#define ILI9341_ANTIQUEWHITE 0xFF5A
#define ILI9341_AQUA 0x07FF
#define ILI9341_AQUAMARINE 0x7FFA
#define ILI9341_AZURE 0xF7FF
#define ILI9341_BEIGE 0xF7BB
#define ILI9341_BISQUE 0xFF38

//#define ILI9341_SPI_CLKDIVIDER SPI_CLOCK_DIV2

#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
//#define TFT_MISO 12
//#define TFT_MOSI 11
//#define TFT_CLK 13

ILI9341_due tft = ILI9341_due(TFT_CS, TFT_DC, TFT_RST);

#define t_SCK 3
#define t_CS 4
#define t_MOSI 5
#define t_MISO 6
#define t_IRQ 7

URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ);

int x, y,pos_x,pos_y;

void setup(){

tft.begin();
// tft.getDisplayStatus();

SPI_CLOCK_DIV8 ;
tft.setRotation(iliRotation90);
tft.fillScreen(ILI9341_BLACK);

ts.InitTouch();
ts.setPrecision(PREC_EXTREME);

Ecran(110,30,100,60,ILI9341_ALICEBLUE,ILI9341_RED);
Button1(20,120,ILI9341_BLUE,ILI9341_WHITE,'<');
Ecran(120,120,80,40,ILI9341_CYAN,ILI9341_BLUE);
Button1(220,120,ILI9341_BLUE,ILI9341_WHITE,' >');
Button2(20,180,ILI9341_GREEN,ILI9341_WHITE,"Start");
Button2(200,180,ILI9341_RED,ILI9341_WHITE," Stop");
}

void loop(){
// put your main code here, to run repeatedly:
waitfortouch(&pos_x,&pos_y);
tft.cursorToXY(0,0);tft.print("x=");tft.print(pos_x);
tft.cursorToXY(0,30);tft.print("y=");tft.print(pos_y);
// if (pos_x > 20 && pos_x < 100 && pos_y > 120 && pos_y < 160){tft.cursorToXY(135,145);tft.print('+');}

}

void Button1(int16_t x, int16_t y, uint16_t color_bn, uint16_t color_bd,char ime ){
tft.fillRoundRect(x,y,80,40,20,color_bn);
tft.drawRoundRect(x,y,80,40,20,color_bd);
tft.setFont(SystemFont5x7);
tft.setTextColor(color_bd, color_bn);
tft.setTextScale(2);
tft.cursorToXY(x+33,y+13);
tft.print(ime);
}

void Button2(int16_t x, int16_t y, uint16_t color_bn, uint16_t color_bd,char *ime ){
tft.fillRoundRect(x,y,100,40,20,color_bn);
tft.drawRoundRect(x,y,100,40,20,color_bd);
tft.setFont(SystemFont5x7);
tft.setTextColor(color_bd, color_bn);
tft.setTextScale(2);
tft.cursorToXY(x+13,y+13);
tft.print(ime);
}

void Ecran(int16_t x, int16_t y,int16_t w, int16_t h, uint16_t color_bn, uint16_t color_bd){
tft.fillRoundRect(x,y,w,h,10,color_bn);
tft.drawRoundRect(x,y,w,h,10,color_bd);
}

void waitfortouch(unsigned short int *pos_x,unsigned short int *pos_y){
do
{
delay(10);
if (ts.dataAvailable() == true)
{
ts.read();
*pos_x = ts.getX(); //Get touch point
*pos_y = ts.getY();
return;
}
}while(ts.dataAvailable()==false);
}

uTouchButtonTest of the library's examples is also behaving strangely. I press one button and another prints.

Have you tried running the URTouch_Calibration sketch on your particular display? The resulting output values are usually fed into URTouchCD.h. These parameters will define the touchscreen calibration for your getX() / getY() calls.

Yes, that's where I started ...
When I do the calibration everything works well. If I do my project the display turns. I tried the ILI9341_due_buttons_Demo_320x240 a little while ago, and I noticed that there was also a mirror-shifting display.

I think it's about rotating, but there is no mirror-turning command that would put things in place.

Your sketch is rotating the TFT display but the touch library is likely still using the default orientation (landscape).

Try replacing your InitTouch() call with the following and see if it improves the touch operation:

InitTouch(PORTRAIT, CAL_X, CAL_Y, CAL_S);

Thanks Impulsive,
This is a good guess. I have also looked for this option, but the URTouch library does not support it. I get a compiler error. Can you specify which library this option is?

URTouch is designed for bit-bashed SPI. Mr Karlsen does not ever use hardware SPI.

Since you have a real 3.3V Pro Mini, you can just put TFT, SD, Touch all on the same hardware SPI bus.

XPT2046_Touchscreen can be installed via the IDE Library Manager.

David.

David raises a better option provided there was no reason to use software / separate SPI interfaces (ie. the display is not a shield).

white_and_red: I see that I was referring to code from a fork with additional parameters. (It would be nice if the library were officially on GitHub :slight_smile: ) If you would like to test out your existing URTouch setup, please try the following initializer:

InitTouch(PORTRAIT);

Hello,
I read the posts and tried some things: InitTouch (PORTRAIT); did not give a good result. There is only one button but the others are missing.
I wanted to explore the XPT2046_Touchscreen, but for now I can not achieve anything. None of the examples go. Probably I still have to try. One question. XPT2046_Touchscreen will be used as a replacement for URTouch.h? I see that only 2 pins are used there.

Best regards!

Connect TFT, SD, Touch to the hardware SPI pins.

It is wise to put external 10k pullup resistors (to 3.3V) on each /CS line.
Only enable one device at a time.

XPT2046_Touchscreen should work out of the box.

If you have a problem:

  1. quote which library example
  2. show your wiring. schematic, photo or clear description.
  3. explain what result you got and what you expected.

David.

Are you saying to connect all the devices: TFT and TOUCH (SD I do not use so far) to 11,12 and 13 pin of PRO MINI and to switch them sequentially? CS will be different (10 and 6) and will have 10k resistors? Will I use the following libraries: ILI9341_due, ILI9341_due_Buttons and XPT2046_Touchscreen and SPI?

Regarding the symbol characters you’re observing, it is likely that the Arduino IDE Serial Monitor “baud rate” is not set correctly. Please try changing it to 38400, which should match the TouchTest sketch.

Yes Impulsive, I just discovered the cause and wiped out the issue as superfluous. I hope the question of post 10 will soon become superfluous and deleted. Thank you for your cooperation.

The TouchTest from XPT2046_Touchscreen.h works very well. I noticed that the touch screen has a great resolution. From 360 to 3700. The pressure is also reported in a large range.

I have encountered a problem with ILI9341_due_Buttons.h and
XPT2046_Touchscreen.h. The compiler gave an error:
ARG3_5: 43: 41: error: no matching function for 'ILI9341_due_Buttons' ILI9341_due_Buttons (ILI9341_due *, XPT2046_Touchscreen *)'
ILI9341_due_Buttons myButtons (& tft, & ts);
Can the two libraries work together? I see that ILI9341_due_Buttons.h uses URTouch.

Here is the entire program code I want to create:

#include <ILI9341_due.h>
#include <ILI9341_due_config.h>
//#include <SystemFont5x7.h>
//#include "fonts/allFonts.h"
#include <ILI9341_due_Buttons.h>
#include "BigFont.h"
#include <XPT2046_Touchscreen.h>
#include <SPI.h>
//#include <UTouch.h>

// comment out the SPI mode you want to use
//#define ILI9341_SPI_MODE_NORMAL
//#define ILI9341_SPI_MODE_EXTENDED // make sure you use pin 4, 10 or 52 for CS
//#define ILI9341_SPI_MODE_DMA

#define ILI9341_ALICEBLUE 0xF7DF 
#define ILI9341_ANTIQUEWHITE 0xFF5A 
#define ILI9341_AQUA 0x07FF 
#define ILI9341_AQUAMARINE 0x7FFA 
#define ILI9341_AZURE 0xF7FF 
#define ILI9341_BEIGE 0xF7BB 
#define ILI9341_BISQUE 0xFF38 

#define TFT_DC 9              
#define TFT_CS 10            
#define TFT_RST 8
//#define TFT_MISO 12     
//#define TFT_MOSI 11        
//#define TFT_CLK 13         
ILI9341_due tft = ILI9341_due(TFT_CS, TFT_DC, TFT_RST);

#define TIRQ_PIN 2              
#define CS_PIN 6 
//#define t_SCK 13                          
//#define t_MOSI 11              
//#define t_MISO 12             
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling

ILI9341_due_Buttons  myButtons(&tft, &ts);

int x, y,pos_x,pos_y, but1,but2,but_Start,but_Stop,pressed_button;

void setup(){
  
  tft.begin();       
  tft.setRotation(iliRotation90);           
  tft.fillScreen(ILI9341_BLACK);

  ts.begin();
  ts.setRotation(1);

  myButtons.setTextFont(BigFont);
 
Ecran(110,30,100,60,ILI9341_ALICEBLUE,ILI9341_RED);
but1 = myButtons.addButton( 40,  125, 60,  30, "<");
Ecran(120,120,80,40,ILI9341_CYAN,ILI9341_BLUE);
but2 = myButtons.addButton( 220,  125, 60,  30, ">");
but_Start = myButtons.addButton( 40,  180, 100,  40, "Start");
but_Stop = myButtons.addButton( 160,  180, 100,  40, "Stop");
myButtons.drawButtons();
}
 
void loop(){
  while (1)
  {
    if (ts.dataAvailable() == true)
    {
      pressed_button = myButtons.checkButtons();

      
      if (pressed_button == but1)
      {
        tft.printAt("Button 1", 130, 130);
        //Serial.println("Button 1");
      }

      if (pressed_button == but2)
      {
        tft.printAt("Button 2", 130, 130);
        //Serial.println("Button 2");
      }
      if (pressed_button == but_Start)
      {
        tft.printAt("Button 3", 130, 130);
        //Serial.println("Button 3");
      }
      if (pressed_button == but_Stop)
      {
        tft.printAt("Button 4", 130, 130);
        //Serial.println("Button 4");
      }
      if (pressed_button == -1)
      {
        tft.printAt("None    ", 130, 130);
      }
    }
  }
}

  void Ecran(int16_t x, int16_t y,int16_t w, int16_t h, uint16_t color_bn, uint16_t color_bd){
  tft.fillRoundRect(x,y,w,h,10,color_bn);  
  tft.drawRoundRect(x,y,w,h,10,color_bd);
  }

The second half may contain unclean places, but the problem is when compiling ILI9341_due_Buttons myButtons (& tft, & ts);