When programming lcd, the display x and y co-ordinates are different to touch

Simply put, on the display the co-ordinates are as follows.
top left - (450,330)
top right - (0,330)
bottom left - (450,0)
bottom right - (0,0)

however when reading a touch they are as follows:

top left - (0,0)
top right - (330,0)
bottom left - (0,450)
bottom right - (330,450)

my code is below and it is based of of the example sketch.
I would appreciate any help

#if 1

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000

// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP = 6, XM = A2, YP = A1, YM = 7; //ID=0x9341
const int TS_LEFT = 208, TS_RT = 912, TS_TOP = 130, TS_BOT = 928;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

Adafruit_GFX_Button on_btn, off_btn;

int pixel_x, pixel_y;     //Touch_getXY() updates global vars
bool Touch_getXY(void)
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because TFT control pins
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
        pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
        pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
    return pressed;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

void setup(void)
    uint16_t ID = tft.readID();
    Serial.print("TFT ID = 0x");
    Serial.println(ID, HEX);
    Serial.println("Calibrate for your Touch Panel");
    if (ID == 0xD3D3) ID = 0x9486; // write-only shield
    tft.setRotation(0);            //PORTRAIT
    // x,y ,width,hight
    on_btn.initButton(&tft,  50, 200, 100, 40, WHITE, CYAN, BLACK, "ON", 2);
    off_btn.initButton(&tft, 200, 200, 100, 40, WHITE, CYAN, BLACK, "OFF", 2);
    tft.fillRect(40, 80, 160, 80, RED);

/* two buttons are quite simple
void loop(void)
    bool down = Touch_getXY();
    on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
    off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
    if (on_btn.justReleased())
    if (off_btn.justReleased())
    if (on_btn.justPressed()) {
        tft.fillRect(40, 80, 160, 80, GREEN);
    if (off_btn.justPressed()) {
        tft.fillRect(40, 80, 160, 80, RED);

I would appreciate any help

A couple of seconds thought, and some very simple arithmetic.

Please expand as you have not given a very helpful reply

Run TouchScreen_Calibr_native.ino example.

Please copy-paste the output from the Serial Terminal to your message. This will tell me the size of your screen. And if the Calibration result looks normal.

I can only speak English. It must be difficult to understand my examples if you are not a native English speaker. Please tell me what you do not understand. Advise me how to improve examples.


This is the output: -

TouchScreen.h GFX Calibration Making all control and bus pins INPUT_PULLUP Typical 30k Analog pullup with corresponding pin would read low when digital is written LOW e.g. reads ~25 for 300R X direction e.g. reads ~30 for 500R Y direction

Testing : (A1, D7) = 22 Testing : (A2, D6) = 31 Diagnosing as:- XM,XP: (A1, D7) = 22 YP,YM: (A2, D6) = 31 ID = 0x6814

cx=428 cy=678 cz=135 LEFT, TOP, Pressure cx=407 cy=668 cz=126 LEFT, MIDH, Pressure cx=221 cy=859 cz=132 LEFT, BOT, Pressure cx=451 cy=557 cz=135 MIDW, TOP, Pressure cx=474 cy=801 cz=135 MIDW, BOT, Pressure cx=543 cy=544 cz=135 RT, TOP, Pressure cx=466 cy=666 cz=133 RT, MIDH, Pressure cx=489 cy=712 cz=129 RT, BOT, Pressure MCUFRIEND_kbv ID=0x6814 320 x 480

const int XP=7,XM=A1,YP=A2,YM=6; //320x480 ID=0x6814 const int TS_LEFT=347,TS_RT=503,TS_TOP=588,TS_BOT=794; PORTRAIT CALIBRATION 320 x 480 x = map(p.x, LEFT=347, RT=503, 0, 320) y = map(p.y, TOP=588, BOT=794, 0, 480) Touch Pin Wiring XP=7 XM=A1 YP=A2 YM=6 LANDSCAPE CALIBRATION 480 x 320 x = map(p.y, LEFT=588, RT=794, 0, 480) y = map(p.x, TOP=503, BOT=347, 0, 320)

Thank you Adam

Your calibration does not look normal. I would expect to see 800 difference between TS_LEFT and TS_RT. You only have 150 difference. Is your screen inside an enclosure? i.e. external pressure on the edges of the screen.

const int XP=7,XM=A1,YP=A2,YM=6; //320x480 ID=0x6814
const int TS_LEFT=347,TS_RT=503,TS_TOP=588,TS_BOT=794;

I have a RM68140 shield. My calibration looks like this:

const int XP=7,XM=A1,YP=A2,YM=6; //320x480 ID=0x6814
const int TS_LEFT=134,TS_RT=906,TS_TOP=114,TS_BOT=924;


it is not in an enclosure, i bout it cheap off of ebay. here is the link: Here is the link