LCD suddenly uncalibrated

Hi
I have touch TFT LCD 2.4 see attached images and I use code below to draw 6 rectangles as buttons. Everything seems to be fine when I used arduino UNO (even when I had the very same problem also, it was no longer present for several days of testing mainly when I change the wiring from breadboard to actual circuit… So I thought it was because of the breadboard) but now I change the UNO for Mega because I didnt have enough memory for all other thing and the problems is back again.

The LCD was calibrated just fine and every time that I pressed some button it was perfect match with the button I touched. But now it no longer works and for example the X coordinates on the left side show -14 on quite a big area insted of 4 to 104 as before. Y are also different now. Iam not sure whats wrong because it worked so well past days and suddenly its like uncalibrated

#include <Adafruit_GFX.h>
#include "SPI.h"
#include "Adafruit_ILI9340.h"
#include "XPT2046_Touchscreen.h"

#define SHIELD_ETHERNET 10
#define LCD_cs 1
#define LCD_dc 6
#define LCD_rst 8
#define DOTYK_cs 7
#define LCD_MOSI 51 // (11 on UNO)
#define LCD_SCLK 52 // (13 on UNO)
#define LCD_MISO 50 // (12 on UNO)

#define TS_MINX 176
#define TS_MINY 218
#define TS_MAXX 3920
#define TS_MAXY 3900

String Title_SDS;   //Button "number"
int ReturnV;

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

void setup() {
  displej.begin();
  dotyk.begin();
  displej.setRotation(1);
  displej.fillScreen(ILI9340_BLACK);
  DrawButtons();
}

void loop() {

  if (dotyk.touched()) {

    TS_Point bod = dotyk.getPoint();
    bod.x = map(bod.x, TS_MAXX, TS_MINX, 0, 320);
    bod.y = map(bod.y, TS_MAXY, TS_MINY, 240, 0);

    displej.fillRect(0, 0, 220, 27, ILI9340_BLACK);
    displej.setCursor(0, 5);
    displej.setTextColor(ILI9340_WHITE);
    displej.setTextSize(1);
    displej.print(" Pressure = ");
    displej.print(bod.z);
    displej.println(",");
    displej.print(" x/y = ");
    displej.print(bod.x);
    displej.print(" / ");
    displej.print(bod.y);
    displej.println();

    if ((bod.x >= 4) && (bod.x <= 104) && (bod.y >= 32) && (bod.y <= 132)) {
      Title_SDS = "One";
      SubDisplayScreen();
    } else {
    }

    if ((bod.x >= 109) && (bod.x <= 209) && (bod.y >= 32) && (bod.y <= 132)) {
      Title_SDS = "Two";
      SubDisplayScreen();
    } else {
    }

    if ((bod.x >= 214) && (bod.x <= 314) && (bod.y >= 32) && (bod.y <= 132)) {
      Title_SDS = "Three";
      SubDisplayScreen();
    } else {
    }

    if ((bod.x >= 4) && (bod.x <= 104) && (bod.y >= 137) && (bod.y <= 237)) {
      Title_SDS = "Four";
      SubDisplayScreen();
    } else {
    }

    if ((bod.x >= 109) && (bod.x <= 209) && (bod.y >= 137) && (bod.y <= 237)) {
      Title_SDS = "Five";
      SubDisplayScreen();
    } else {
    }

    if ((bod.x >= 214) && (bod.x <= 314) && (bod.y >= 137) && (bod.y <= 237)) {
      Title_SDS = "Six";
      SubDisplayScreen();
    } else {
    }

    bod.x = 0;
    bod.y = 0;
    bod.z = 0;

  } else {
    //NO TOUCH...
  }

  if (ReturnV == 1) {
    ReturnV = 0;
    DrawButtons();
  }


  delay(500);
}

void SubDisplayScreen() {
  displej.fillScreen(ILI9340_WHITE);
  displej.setTextColor(ILI9340_BLACK);
  displej.setCursor(88, 90);
  displej.setTextSize(2);
  displej.print(" Button - ");
  displej.print(Title_SDS);
  delay(1000);
  ReturnV = 1; //Back to menu buttons
}

//Draw Button
void DrawButtons()
{
  displej.fillScreen(ILI9340_BLACK);
  int DwBcoord_Xo = 4;
  int DwBcoord_Yo = 32;
  int DwBcoord_X = DwBcoord_Xo;
  int DwBcoord_Y = DwBcoord_Yo;
  int DwBcoord_W = 100;
  int DwBcoord_H = 100;
  int DwBcoord_RE = 10;
  int DwBcoord_SP = 5;
  for (int j = 1; j <= 6 ; j++) {
    displej.fillRoundRect (DwBcoord_X, DwBcoord_Y, DwBcoord_W, DwBcoord_H, DwBcoord_RE, ILI9340_WHITE);
    displej.drawRoundRect (DwBcoord_X, DwBcoord_Y, DwBcoord_W, DwBcoord_H, DwBcoord_RE, ILI9340_BLACK);

    if (j == 3) {
      DwBcoord_X = DwBcoord_Xo;
      DwBcoord_Y = DwBcoord_Y + DwBcoord_H + DwBcoord_SP;
    } else {
      DwBcoord_X = DwBcoord_X + DwBcoord_W + DwBcoord_SP;
    }
  }
}

What happens when you recalibrate it?

And where are the calibration settings stored? On the Uno's EEPROM ? If so, that may explain your difficulties.

jremington:
What happens when you recalibrate it?

Iam not sure how you mean this, I thought the only "calibration" is in my code here:

#define TS_MINX 176
#define TS_MINY 218
#define TS_MAXX 3920
#define TS_MAXY 3900
.
.
.
.
TS_Point bod = dotyk.getPoint();
bod.x = map(bod.x, TS_MAXX, TS_MINX, 0, 320);
bod.y = map(bod.y, TS_MAXY, TS_MINY, 240, 0);

if not, then I probably dont know how to calibrate it. Iam sorry Iam new to this and try to understand it. I thought this will be it, because it worked... but its not. If I touch vith stylus from left side anywhete to half of the display the X is still -14 ... in that half of screen it starts toc hange to 4.. 8... but really huge area is just -14

The numbers below must be correct for the touch screen to be usable. Where did they come from?

#define TS_MINX 176
#define TS_MINY 218
#define TS_MAXX 3920
#define TS_MAXY 3900

Here is a tutorial on calibrating touch screens.

jremington:
The numbers below must be correct for the touch screen to be usable. Where did they come from?

#define TS_MINX 176

#define TS_MINY 218
#define TS_MAXX 3920
#define TS_MAXY 3900




Here is a [tutorial on calibrating touch screens](https://www.embedded.com/design/system-integration/4023968/How-To-Calibrate-Touch-Screens).

I get those from the LCD, I just touched the corners and frite down after several attempts what was the highest and lowest number for x and y... problem is even when i want to try it again (I commented those lines ) the displey just shows 4094 for x everywhere from left to half of screen just like taht -14

(I commented those lines )

That won't work. You need the raw coordinates for calibration. Start over from the beginning.

Post the code you are using for calibration, using code tags.

jremington:
That won’t work. You need the raw coordinates for calibration. Start over from the beginning.

Post the code you are using for calibration, using code tags.

unfortunately I dont h ave any, I used only that one I aleready posted on my first post (when I first started playing with that LCD the code just missed the TS_MINX, TS_MINY, TS_MAXX, TS_MAXY (with those numbers I obtained by trying to get the lowes and highest values) and those lines:

bod.x = map(bod.x, TS_MAXX, TS_MINX, 0, 320);
bod.y = map(bod.y, TS_MAXY, TS_MINY, 240, 0);

After that it worked but suddently on Mega it doesnt… I tried even some calibratin code below, but after each attemt to touch with the stylus the three points it gets me everytime some strange and way different values… like firt attempt is:

for X:
0.667
0.252
185

for Y
0.926
0.207
103

and then second attempt I get:

2.96
2.05
152

4.83
1.73
-15215

Ok code exceeds maxumim so Iam attaching it as file

calibration_LCD.ino (9.98 KB)

Sounds like a loose connection or wiring problem. Check every connection -- maybe even take the project apart completely and rewire it.

Cleanly soldered joints are best.

I used multimeter and everything looks ok Iam able to get sound signal on all paths to the display pins. I even tried to wire it back to UNO and the same problem was there for a while, then it suddenly start to work but not completly. The buttons reacted only sometimes, sometiems they were off (the x and y coordinates go off) and then again they are ok… its very weird. On the Mega it just doesnt work

I even tried different library and code see below… but its strange the x and y here are always both -1 and it doesnt seems it registering touch at all…

//------------------------------------------------------------------
#include <SPI.h> 
#include <TFT_ILI9341.h> 
#include <UTouch.h> 
//------------------------------------------------------------------

#define TFT_CS    10   // Chip select for TFT display, don't change when using F_AS_T
#define TFT_DC     9   // Data/command line, don't change when using F_AS_T
#define TFT_RST    8   // Reset, you could connect this to the Arduino reset pin
#define TOUCH_CS   3   // Touch chip select
#define TOUCH_IRQ  2   // Touch IRQ
#define TOUCH_DIN  51  // Touch MOSI
#define TOUCH_DOUT 50   // Touch MISO
#define TOUCH_CLK  52   // Touch SCLK

//------------------------------------------------------------------
TFT_ILI9341 tft = TFT_ILI9341(TFT_CS, TFT_DC);
UTouch  touch(TOUCH_CLK, TOUCH_CS, TOUCH_DIN, TOUCH_DOUT, TOUCH_IRQ);
//------------------------------------------------------------------
const byte TFT_rotation = 1; 
byte TOUCH_rotation;
int x; 
int y; 

void setup() {
  Serial.begin(9600);
  //------------------------------------------------------------------
  tft.init(); // inicializace TFT displeje
  tft.setRotation(TFT_rotation);
  tft.setTextColor(TFT_BLACK);
  //------------------------------------------------------------------
  if (TFT_rotation == 0 || TFT_rotation == 2) TOUCH_rotation = PORTRAIT; 
  else if (TFT_rotation == 1 || TFT_rotation == 3) TOUCH_rotation = LANDSCAPE;
  touch.InitTouch(TOUCH_rotation);
  touch.setPrecision(PREC_MEDIUM);
  tft.fillScreen(TFT_WHITE); // vycisti pole pro novy zapis
  tft.drawCentreString("TEST TOUCH", 120, 120, 4);
  tft.drawCentreString("LAYER", 120, 150, 4);
  delay(1000);
}

void loop() {

  if (touch.dataAvailable())
  {

    touch.read();  
    x = touch.getX();
    y = touch.getY();
    
    if (TFT_rotation == 0) { 
      x = map(x, 0, 239, 239, 0);
      y = map(y, 0, 319, 319, 0);
    }
    else if (TFT_rotation == 3) { 
      x = map(x, 0, 319, 319, 0);
      y = map(y, 0, 239, 239, 0);
    }
    

    if (x != -1 && y != -1 && x != 319)
    {
      tft.fillScreen(TFT_WHITE);
      Serial.print(" X= ");
      Serial.print(x);
      Serial.print(" Y= ");
      Serial.println(y);
      tft.fillCircle(x, y, 10, TFT_BLACK); // draw circle in place of touch
    }
    delay(600);
    
  }



}

Ok seems noone have some ideas, I tried it again... tried buch of different libraries and calibration programs seems nothing works. Last attempt was even worse the display didnt show any coortinates at all and each touch was just reseting the display.

I decided to try another approach. I found a wiring diagram that connecting the screen through level converters see HERE and we'll see what happens. Strange is that in this diagram all the display pins are connected separately. In the diagram I had before they connected some together like T_DO display pin was conected to MISO display pin and both were leading to UNO D12 or T_DIN display pin was connected with MOSI display pin both leading to D11 and T_CLK display pin was connected to SCKdisplay pin both leading to D13