Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: XPT2046_Touchscreen problems ...

  1. #1
    Junior Member
    Join Date
    Aug 2018

    XPT2046_Touchscreen problems ...

    Hi to all, I'm a newbie so I beg apologize if I put some nonsense. I'm testing the example "TouchTest" of the XPT2046_Touchscreen 1.2.0 library with Arduino IDE 1.8.5:
    #include <XPT2046_Touchscreen.h>
    #include <SPI.h>
    //XPT2046_Touchscreen ts(CS_PIN);
    #define CS_PIN 14
    // MOSI=11, MISO=12, SCK=13 UNO
    // MOSI=51, MISO=50, SCK=52 MEGA
    //XPT2046_Touchscreen ts(CS_PIN);  // Param 2 - NULL - No interrupts
    //XPT2046_Touchscreen ts(CS_PIN, 255);  // Param 2 - 255 - No interrupts
    #define TIRQ_PIN  15
    XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling
    void setup() {
      while (!Serial && (millis() <= 1000));
    void loop() {
      if (ts.touched()) {
        TS_Point p = ts.getPoint();
        Serial.print("P= ");Serial.print(p.z);Serial.print(", x= ");Serial.print(p.x);
        Serial.print(", y= ");Serial.print(p.y);Serial.println();
    The results and problems are:
    Click image for larger version. 

Name:	xpt2046.jpg 
Views:	113 
Size:	166.7 KB 
ID:	14554
    A) The function if (ts.touched()) doesn't work correctly because it doesn't wait for the interruption to arrive.
    The initial value for Pressuree, x and y are, 4095, 4080, 0, respectively. I think that x should be 0, too, as for y.
    B) By touching, approximately, in the center of the screen, x and y acquire the values 1984 and 1952. The screen is 320x240 but I have not found in XPT2046_Touchscreen where the width and height of the screen can be fixed.
    C) Two Warnings related to the SPI clock frequency appear in my simulator.
    D) Using the debugging capabilities of my simulator I get the following:
    Click image for larger version. 

Name:	xpt2046log.jpg 
Views:	61 
Size:	95.3 KB 
ID:	14555
    It may be that everything is due to some SPI control problem of the XPT2046_Touchscreen 1.2.0 library but I think there must be some problem in the initial value of x and how to set the resolution of the screen, in this case 320x240.

    Paul or any of the forum gurus can help me?

    Thank you for your time and attention.

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Are you using a Teensy here? Or an Arduino Mega?

    I am assuming you are using Paul's library...

    If I am reading this right you are trying to use pin 15 as an interrupt pin?
    Looking at Arduino reference:
    I don't believe pin 15 is a valid interrupt pin for the mega. You might try using a different one?

    As for values returned from Touch screen. They are raw values, which need to be mapped into the valid range for your display...

    With one of my programs that used it, I found a range of about:
    #define TS_MINX 260
    #define TS_MINY 350
    #define TS_MAXX 3825
    #define TS_MAXY 3825
    I then have a helper function that looks like:
    uint16_t g_last_touch_raw_x;
    uint16_t g_last_touch_raw_y;
    boolean OLEDSGC::GetTouchPoint(word *pwX, word *pwY)
      TS_Point pt;
      pt = ts.getPoint();
      g_last_touch_raw_x = pt.x;
      g_last_touch_raw_y = pt.y;
      // Scale from ~0->4000 to tft.width using the calibration #'s
      *pwX = map(pt.y, TS_MINX, TS_MAXX, tft.width(), 0);
      *pwY = map(pt.x, TS_MINY, TS_MAXY, 0, tft.height());
      *pwX = map(pt.x, TS_MINX, TS_MAXX, tft.width(), 0);
      *pwY = map(pt.y, TS_MAXY, TS_MINY, 0, tft.height());
      return true;
    As again the ranges you use depends on orientation of display. I know that others have like @defragster have versions that have refined these values some.


  3. #3
    Junior Member
    Join Date
    Aug 2018
    KurtE, thank you very much for your quick response and sorry for my delay in communicating with you again but I spent four days doing a thousand tests, both with the ILI9325 and the SSD1289. In fact, I use Arduino MEGA and Paul's library.
    Your successful comment about using a valid interruption pin in the MEGA, instead of the wrong pin 15, made me think that he was the origin of all the problems but it has not been like that. On the other hand, I have not known how to use your "helper function".

    a) Definitely, the Proteus model of the ILI9325 works perfectly with the UTFT library. On the other hand, the Proteus model of the SSD1289 visualizes at 180, inverts, more or less, the colors and, in addition, the images appear with multiple faults.

    b) Also definitely, I have come to the conclusion that both the ILI9325 and the SSD1289 do not work with any "touchscreen" library and Arduino IDE (Yes, they work and very well with Bascom-AVR.) I have tested with UTouch, URTouch, XPT2046_touchscreen and half a dozen more, including the Defragster tests.

    I do not know if you can help me in something else, especially in how to use your "helper function", before finally leaving and "throwing in the towel".

    Thanks for your time and patience.

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Again hard to know what your actual issue is with the XPT2046.

    As I mentioned the values you receive are raw values, that are not in the same scale/origins as the displays...

    But is it working? That is, when you press on the touch screen, does it show you an X and Y value. I mean you can maybe run a simple sketch where the loop() does something like:

    void loop()
      // See if there's any  touch data for us
      if (ts.bufferEmpty()) {
      // You can also wait for a touch
      if (! ts.touched()) {
      // Retrieve a point
      TS_Point p = ts.getPoint();
      Serial.print("X = "); Serial.print(p.x);
      Serial.print("\tY = "); Serial.print(p.y);
      Serial.print("\tPressure = "); Serial.print(p.z);
    And then hopefully as you touch the screen you should see values.
    I simply moved my finger around from far left to far right. And looked at which value (X or Y) was changing and which way and found what looks like a MIN and MAX value,
    Likewise UP and Down.

    So now you have a set of ranges setup. Now you simply need to convert those values into Screen values...
    Note: The values returned from touch screen will always be the same regardless of what logical orientation you set your displays Screen Orientation... No hardware connection between the two.

    So Arduino has a map function:

    Which allows you to a value in one range of values to it's corresponding value in a different range

    So Suppose Our touch panel showed a range for X's of 337-3729 and Y's of 529-3711

    And suppose your screen has a width of 320 and suppose the touch panel uses X for the horizontal and goes from low X on left hand edge to high value on right hand edge,
    You should then be able to map some randon TS point X the logical Screen X by using map like:
    screen_x = map(touch_x, 337, 3729, 0, 319)

    And likewise for screen Y.

    But again not sure if that is your issue now?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts