XPT2046 Touch Screen Controller example.

Post your cool example code here.
Post Reply
User avatar
Vassilis
Posts: 320
Joined: Thu May 21, 2015 6:42 am
Location: Thessaloniki, Greece
Contact:

XPT2046 Touch Screen Controller example.

Post by Vassilis » Wed Dec 02, 2015 4:40 pm

In case of someone wants to play with XPT2046 touch screen controller that can be found on the new ILI9341 SPI LCDs, I wrote a simple sketch for that.
The sketch prints the X,Y coordinates on Serial port 1 (values 0-4095).

Code: Select all

/**
 * XPT2046 Touch Screen Controller example.
 * 
 * Copyright (c) 02 Dec 2015 by Vassilis Serasidis
 * Home: http://www.serasidis.gr
 * email: avrsite@yahoo.gr
 * 
 * The sketch example has been created for using it with STM32Duino (http://www.stm32duino.com)
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <SPI.h>

#define csPin PA3
#define Z_THRESHOLD   3500

int xy[2];

void setup() {
  Serial1.begin(9600);
  Serial1.println("-------------------------------------------------");
  Serial1.println("XPT2046 example sketch");
  Serial1.println("Copyright (c) 02 Dec 2015 by Vassilis Serasidis");
  Serial1.println("Home: http://www.serasidis.gr");
  Serial1.println("-------------------------------------------------");
  
  SPI.begin(); //Initiallize the SPI1 port.
  pinMode(csPin, OUTPUT);
  digitalWrite(csPin, HIGH);
}

//-----------------------------------------------------
//
//-----------------------------------------------------
void loop() {
  if(read_XY(xy)){     //Read the X,Y coordinates (12 bits, values 0-4095)
    Serial1.print("X: ");
    Serial1.println(xy[0]); //Print X value
    Serial1.print("Y: ");
    Serial1.println(xy[1]); //Print Y value
    Serial1.println();
    delay(1000);
  }
}

//-----------------------------------------------------
//
//-----------------------------------------------------
boolean read_XY(int *xy){
  int z1, z2, tmpH, tmpL;
  digitalWrite(csPin, LOW);

  //Check if touch screen is pressed.
  SPI.transfer(B10110001); // Z1
  tmpH = (SPI.transfer(0) << 5);
  tmpL = (SPI.transfer(0) >> 3);
  z1 = tmpH | tmpL;

  SPI.transfer(B11000001); // Z2
  tmpH = (SPI.transfer(0) << 5);
  tmpL = (SPI.transfer(0) >> 3);
  z2 = tmpH | tmpL;
  
  if((z2 - z1) < Z_THRESHOLD){ //If the touch screen is pressed, read the X,Y  coordinates from XPT2046.
    SPI.transfer(B11010001); // X
    tmpH = (SPI.transfer(0) << 5);
    tmpL = (SPI.transfer(0) >> 3);
    xy[0] =  tmpH | tmpL;
    
    SPI.transfer(B10010001); // Y
    tmpH = (SPI.transfer(0) << 5);
    tmpL = (SPI.transfer(0) >> 3);
    xy[1] =  tmpH | tmpL;
    digitalWrite(csPin, HIGH);
    return true;
  }
  digitalWrite(csPin, HIGH);
  return false;
}
Enjoy!

User avatar
RogerClark
Posts: 7160
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: XPT2046 Touch Screen Controller example.

Post by RogerClark » Wed Dec 02, 2015 9:06 pm

Thanks for sharing

User avatar
Vassilis
Posts: 320
Joined: Thu May 21, 2015 6:42 am
Location: Thessaloniki, Greece
Contact:

Re: XPT2046 Touch Screen Controller example.

Post by Vassilis » Thu Dec 03, 2015 12:56 am

RogerClark wrote:Thanks for sharing
You are welcome !

User avatar
Vassilis
Posts: 320
Joined: Thu May 21, 2015 6:42 am
Location: Thessaloniki, Greece
Contact:

Re: XPT2046 Touch Screen Controller example.

Post by Vassilis » Thu Dec 03, 2015 6:55 pm

I convert the example sketch into a library XPT2046_touch and I added two example sketches also.
The first sketch checks the touch screen to see if it is pressed. Then, it prints the X,Y coordinates to the Serial port 1. That example is the same with the sketch I published on the first post.

The second sketch creates some virtual buttons on the touch screen area. The number of the buttons is set in to the sketch

Code: Select all

//Create 4*2=8 virtual buttons
#define LINES     2
#define COLUMNS   4
The SPI port can be changed according to your needs

Code: Select all

SPIClass mySPI(1); //Create an SPI instance on SPI1 port.
XPT2046_touch ts(CS_PIN, mySPI); // Chip Select pin, SPI port
The reason I wrote this library is to use it to my new homemade 3D printer on the LCD + Touch screen + SD section.
I already sent a Pull Request to Roger.

exider
Posts: 9
Joined: Wed Oct 04, 2017 12:58 pm

Re: XPT2046 Touch Screen Controller example.

Post by exider » Sat Oct 14, 2017 7:14 pm

Hi Vassilis,

Where can I find the library?

Thanks

Post Reply