TFT 3.5" ILI9486 8bit - Touch Screen - issue with initialization

Hi,
My problem is that I can’t get TOUCH FUNCTION running - returning coordinates of place I touch.

my hardware:
Arduino Mega 2560
TFT 3.5" ILI9486 8bit with Touch Screen

Quite sure that I’m using wrong library based on which I glued my code together (based on examples I found on internet like :https://howtomechatronics.com/tutorials/arduino/arduino-tft-lcd-touch-screen-tutorial/).

calibration test :

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 : (A2, D8) = 22
Testing : (A3, D9) = 32
Diagnosing as:-
XM,XP:  (A2, D8) = 22
YP,YM:  (A3, D9) = 32
ID = 0x9486

cx=165 cy=925 cz=200 LEFT, TOP, Pressure
cx=142 cy=528 cz=406 LEFT, MIDH, Pressure
cx=146 cy=112 cz=607 LEFT, BOT, Pressure
cx=523 cy=943 cz=279 MIDW, TOP, Pressure
cx=511 cy=113 cz=591 MIDW, BOT, Pressure
cx=894 cy=943 cz=414 RT, TOP, Pressure
cx=889 cy=529 cz=512 RT, MIDH, Pressure
cx=892 cy=115 cz=662 RT, BOT, Pressure
MCUFRIEND_kbv ID=0x9486  320 x 480

const int XP=8,XM=A2,YP=A3,YM=9; //320x480 ID=0x9486
const int TS_LEFT=126,TS_RT=915,TS_TOP=954,TS_BOT=95;
PORTRAIT CALIBRATION     320 x 480
x = map(p.x, LEFT=126, RT=915, 0, 320)
y = map(p.y, TOP=954, BOT=95, 0, 480)
Touch Pin Wiring XP=8 XM=A2 YP=A3 YM=9
LANDSCAPE CALIBRATION    480 x 320
x = map(p.y, LEFT=954, RT=95, 0, 480)
y = map(p.x, TOP=915, BOT=126, 0, 320)

I stopped at stage when I want to touch screen and get coordinates on COM to make sure that once I write rest of code defining areas to touch, it will actually go somewhere. I bet there are parts of code that can go to trash as they don’t do anything…
My code is below

#include <Adafruit_GFX.h>    // Core graphics library
#include <MCUFRIEND_kbv.h>   // Hardware-specific library
MCUFRIEND_kbv tft;
#include <URTouch.h>
URTouch  myTouch( 6, 5, 4, 3, 2);
  
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>

#include <FreeDefaultFonts.h>

#define BLACK   0x0000
#define RED     0xF800
#define GREEN   0x07E0
#define WHITE   0xFFFF
#define GREY    0x8410
#define YELLOW  0xFFE0


int page = 0;
int XX,YY;

void setup(void)
{
    Serial.begin(9600);
    uint16_t ID = tft.readID();
    if (ID == 0xD3) ID = 0x9481;
    tft.width();
    tft.height();
    tft.begin(ID);
    tft.setRotation(1);
    tft.fillScreen(BLACK);
    myTouch.InitTouch();
    myTouch.setPrecision(PREC_MEDIUM);
}

void loop()
{
  if (page == 0)
  {
  HomeScreen();
    if (myTouch.dataAvailable() > 0) {
    myTouch.read();
    XX=myTouch.getX(); // X coordinate where the screen has been pressed
    YY=myTouch.getY(); // Y coordinates where the screen has been pressed
      Serial.println("X = ");
      Serial.println(XX);
      Serial.println("Y = ");
      Serial.println(YY);
     }
  }

  if (page == 1)
  {
    FlapsScreen();
  }

  if (page == 2)
  {
  StepperScreen();
  }
  else {
    page=0;
  }
}

void HomeScreen()
{
    //tft.fillScreen(BLACK);
    showmsgXY(20, 10, 2, NULL, "Reinsch Engineering - HVAC v1");
    showmsgXY(20, 30, 2, NULL, "Delorean DMC-12 Heaterbox controller");
    tft.drawRoundRect(60,80,360,70, 10, YELLOW);
    tft.fillRoundRect(61,81,358,68,10, GREY);
    showmsgXY(180, 100, 4, NULL, "FLAPS");
    tft.drawRoundRect(60, 170, 360, 70, 10, YELLOW);
    tft.fillRoundRect(61, 171, 358, 68, 10, GREY);
    showmsgXY(160, 190, 4, NULL, "STEPPER");
    delay(300);
}

void FlapsScreen()
{
  
}

void StepperScreen()
{
  
}

void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg)
{
    int16_t x1, y1;
    uint16_t wid, ht;
//    tft.drawFastHLine(0, y, tft.width(), WHITE);
    tft.setFont(f);
    tft.setCursor(x, y);
    tft.setTextColor(YELLOW);
    tft.setTextSize(sz);
    tft.print(msg);
    delay(2000);
}

when I delete parts that are TOUCH related I have a good image, but if it added, it is causing issues and no coordinates returing on COM

I would appreciate your help. I’m trying to motorize my Heater box (HVAC) from my Delorean and this TFT TOUCH SCREEN is part of my test rig.
Standard unit is operated my electromechanical switches. They control vacuum delivery to actuators and position of hot/cold mixing flap. So far I added stepper motor from other car that is succesfully able to control mixing flap in whole range (some 3d printed parts were required). For delivering vacuum to correct actuator, I’ve used set of 6 small solenoids. Everything works separately. Now trying to build a closed testing loop with TFT TOUCH for manual operation of some functions.

I would appreciate your help!
regards,
Marek

Your Calibration looks quite normal.

const int XP=8,XM=A2,YP=A3,YM=9; //320x480 ID=0x9486
const int TS_LEFT=126,TS_RT=915,TS_TOP=954,TS_BOT=95;

"Touchscreen.h" suits your resistive panel e.g. your screen.
"URTouch.h" is for an XPT2046 intelligent Touch controller chip.

Run the Touch example(s) that come with the MCUFRIEND_kbv.h and Touchscreen.h libraries.

David.

Thanks for your support! I downloaded that library and run example. It worked and I could read out the position. Resistance of screen is 264[ohm]. My next step was to merge that code from example to my code. For some reason it was going WHITE screen all the time. I put some lines of my code to comment to disable them and I noticed that once program “tft.” is called, screen will go white.

Is it possible that both libraries are using same variables and overwriting values of them? I changed name of variables intruduced by me to other then x,y and z but that did not help. Do I need to open those libraries and somehow change variables so they not used by more then one library ?

here is my code:

#include <Adafruit_GFX.h>    // Core graphics library
#include <MCUFRIEND_kbv.h>   // Hardware-specific library
MCUFRIEND_kbv tft;

#include <stdint.h>
#include "TouchScreen.h"

#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>

#include <FreeDefaultFonts.h>

#define YP A3  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin

#define MINPRESSURE 10
#define MAXPRESSURE 1000

#define BLACK   0x0000
#define RED     0xF800
#define GREEN   0x07E0
#define WHITE   0xFFFF
#define GREY    0x8410
#define YELLOW  0xFFE0

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

int pageer = 0;

void setup(void)
{
    Serial.begin(9600);
    uint16_t ID = tft.readID();
    if (ID == 0xD3) ID = 0x9481;
    tft.width();
    tft.height();
    tft.begin(ID);
    tft.setRotation(1);
    tft.fillScreen(YELLOW);

}

void loop()
{
  TSPoint pep = ts.getPoint();
  
  if (pep.z > MINPRESSURE && pep.z < MAXPRESSURE) {
     Serial.print("X = "); Serial.print(pep.x);
     Serial.print("\tY = "); Serial.print(pep.y);
     Serial.print("\tPressure = "); Serial.println(pep.z);

}

  
  if (pageer == 0)
  {
HomeScreen();
  }


  if (pageer == 1)
  {
    FlapsScreen();
  }

  if (pageer == 2)
  {
  StepperScreen();
  }
 
}
void HomeScreen()
{
    tft.fillScreen(BLACK);
    showmsgXY(20, 10, 2, NULL, "Reinsch Engineering - HVAC v1");
    showmsgXY(20, 30, 2, NULL, "Delorean DMC-12 Heaterbox controller");
    tft.drawRoundRect(60,80,360,70, 10, YELLOW);
    tft.fillRoundRect(61,81,358,68,10, GREY);
    showmsgXY(180, 100, 4, NULL, "FLAPS");
    tft.drawRoundRect(60, 170, 360, 70, 10, YELLOW);
    tft.fillRoundRect(61, 171, 358, 68, 10, GREY);
    showmsgXY(160, 190, 4, NULL, "STEPPER");
    delay(3000);
}

void FlapsScreen()
{
  
}

void StepperScreen()
{
  
}

void showmsgXY(int xx3, int yy3, int sz, const GFXfont *f, const char *msg)
{
    int16_t x1, y1;
    uint16_t wid, ht;
//    tft.drawFastHLine(0, y, tft.width(), WHITE);
    tft.setFont(f);
    tft.setCursor(xx3, yy3);
    tft.setTextColor(YELLOW);
    tft.setTextSize(sz);
    tft.print(msg);
    delay(2000);
}

I would appreciate your support.
Thanks,
Marek

Please study the examples. Touch shares pins with the TFT.

You must set the XM, YP pins back to OUTPUT after every access of ts.getPoint()

Every example shows you where and how.

It is wise to set A3 HIGH as well (because it is LCD_CS pin)

Most shields use A1, A2 for XM, YP which means that they do not interfere with LCD_CS.

Seriously. I provide some examples. Run these first. You only need to replace the Calibration statements e.g.

const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x9320
const int TS_LEFT=897,TS_RT=122,TS_TOP=944,TS_BOT=141;

David.