[Diao Ye learns programming] Arduino hands-on (153) --- 2.4-inch TFT LCD touch screen module 7

The reference to 37 sensors and actuators has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, according to the concept of practice to get true knowledge (must be done), for the purpose of learning and communication, I am going to try a series of experiments one by one, regardless of success (the program goes through) or not, They will be recorded - small progress or unsolvable problems, hoping to inspire others.

[Arduino] 168 kinds of sensor module series experiments (data code + simulation programming + graphics programming)
Experiment 156: 2.4-inch TFT LCD touch screen color screen module TFT-LCD high-definition true color display

insert image description here

Randomly found:

2.4 inch TFT LCD touch screen color screen module TFT-LCD HD true color display

Reference Electrical Schematic

insert image description here
insert image description here

2.4-inch IPS resistive touch screen built-in ILI9341 control chip, XPT2046 touch panel control chip compatible with Arduino interface

IPS screen

It is a technology based on TFT, and its essence is a TFT screen. IPS screen (In-Plane Switching, in-plane switching) technology is a liquid crystal panel technology introduced by Hitachi in 2001, commonly known as "Super TFT". The biggest feature of the IPS panel is that the two poles are on the same surface. Unlike other liquid crystal mode electrodes are arranged three-dimensionally on the upper and lower sides. This technology optimizes the arrangement of liquid crystal molecules and adopts a horizontal arrangement. When encountering external pressure, the molecular structure sinks slightly downward, and the overall molecule is horizontal. When encountering external force, the solidity and stability of the liquid crystal molecular structure of the hard screen is better than that of the soft screen. It will not distort the picture, affect the color of the picture, and protect the effect of the picture to the greatest extent. There is also a S-IPS panel, which is an improved type of IPS.

insert image description here
The touch screen (Touch Panel), also known as "touch screen" or "touch panel", is an inductive liquid crystal display device that can receive input signals such as contacts. The tactile feedback system can drive various connecting devices according to the pre-programmed program, which can be used to replace the mechanical button panel, and create vivid audio-visual effects through the liquid crystal display screen.

Touch screen technology was produced in the 1970s and was first used in the military of the United States. Since then, this technology has gradually transferred to civilian use. With the development of electronic technology and network technology and the popularization of Internet applications, a new generation of touch screen technology and products have emerged one after another. , Its strong and durable, fast response, space-saving, easy to communicate and many other advantages have been recognized by the public. As the latest computer input device, the touch screen is a simple, convenient and natural way of human-computer interaction. It endows multimedia with a new look and is a very attractive new multimedia interactive device. Mainly used in public information query, industrial control, military command, electronic games, multimedia teaching, etc.

The essence of a touch screen is a sensor, which consists of a touch detection component and a touch screen controller. The touch detection component is installed in front of the display screen to detect the user's touch position and send it to the touch screen controller after receiving it; the main function of the touch screen controller is to receive touch information from the touch point detection device and convert it into contact coordinates and send it to the CPU , At the same time, it can receive the commands sent by the CPU and execute them.

insert image description here
resistive touch screen

The outermost layer of the resistive screen generally uses a soft screen, and the inner contacts are connected up and down by pressing. The inner layer is equipped with physical material oxide metal, that is, N-type oxide semiconductor - indium tin oxide (Indium Tin Oxides, ITO), also known as indium oxide, with a light transmittance of 80%, and the upper and lower layers are separated in the middle. ITO is the main material used in resistive touch screens and capacitive touch screens. Their working surfaces are ITO coatings. Press the outer layer with fingertips or any object to make the surface film concave and deform, so that the inner two layers of ITO touch and conduct electricity for positioning. To the coordinates of the pressed point to achieve manipulation. According to the number of outgoing lines of the screen, there are 4 lines, 5 lines and multi-lines. The threshold is low, the cost is relatively cheap, and the advantage is that it is not affected by dust, temperature, and humidity. The disadvantages are also obvious. The outer screen film is easy to scratch, and you cannot touch the screen with sharp objects. Generally, it cannot be multi-touch, that is, it can only support a single point. If two or more touch points are pressed at the same time, it cannot be recognized and the precise coordinates cannot be found. To enlarge a picture on the resistive screen, you can only click "+" multiple times to enlarge the picture step by step. This is the basic technical principle of the resistive screen.

Use pressure sensing for control. When a finger touches the screen, the two conductive layers come into contact at the touch point, and the resistance changes. Signals are generated in both X and Y directions and sent to the touch screen controller. The controller detects this contact and calculates the (X,Y) position, which behaves like a mouse. The resistive touch screen is not afraid of dust, water and dirt, and can work in harsh environments. However, since the outer layer of the composite film is made of plastic material, the anti-explosion property is poor, and the service life is affected to a certain extent.

The resistive touch screen is controlled by pressure sensing. Its surface layer is a layer of plastic and the bottom layer is a layer of glass. It can withstand the interference of harsh environmental factors, but the feel and light transmission are poor. It is suitable for wearing gloves and cannot be directly touched by hands. occasion.

insert image description here
insert image description here

Touch screen hardware structure and working principle

According to different working principles and transmission media, touch screens are mainly divided into resistive, capacitive, infrared and surface acoustic wave types. The resistive touch screen is a 4-layer transparent composite film screen, as shown in the figure. The bottom is a base layer made of glass or plexiglass; the top is a plastic layer whose outer surface has been hardened to make it smooth and scratch-resistant; the middle is two layers of metal conductive layers, and there are many small transparent isolation points between the conductive layers to separate the two layers. separated. The two metal conductive layers are the working surface of the touch screen, and each of its two ends is coated with a strip of silver glue, which is called a pair of electrodes on the working surface of the touch screen. The X working surface and Y working surface of the four-wire touch screen are respectively added to two conductive layers, and there are 4 lead wires in total, which are respectively connected to the X electrode pair and Y electrode pair of the touch screen. When the stylus touches the screen, the two conductive layers come into contact at the contact point. When a resistive touch screen is used as an input device in conjunction with a display screen, the essence of its work is to determine the contact coordinates of the touch screen by measuring the divided voltage of the resistance in the X and Y directions, and map the coordinates to the coordinates of the display screen, so that Realize human-computer interaction. Since the working surface of the resistive touch screen is completely isolated from the outside world and is less affected by the environment, it has the advantages of not being afraid of dust and water vapor, high stability, and no drifting, and is especially suitable for industrial field use.

insert image description here
XPT2046 chip

It is a 4-wire resistive touch screen controller with 12-bit resolution 125KHz conversion rate step-approximation A/D converter. The XPT2046 supports low voltage I/O interfaces from 1.5V to 5.25V. XPT2046 can detect the pressed screen position by performing two A/D conversions, besides, it can also measure the pressure applied on the touch screen. The built-in 2.5V reference voltage can be used as auxiliary input, temperature measurement and battery monitoring. The voltage range of battery monitoring can be from 0V to 6V. The XPT2046 has a temperature sensor integrated on-chip. In a typical working state of 2.7V, with the reference voltage turned off, the power consumption can be less than 0.75mW. The XPT2046 is available in tiny packages: TSSOP-16, QFN-16 and VFBGA-48. The working temperature range is -40℃~+85℃. Fully compatible with ADS7846, TSC2046, AK4182A.

insert image description here
XPT2046 basic principle description

XPT2046 is a typical successive approximation analog-to-digital converter (SAR ADC), which includes functions such as sample/hold, analog-to-digital conversion, and serial port data output. At the same time, the chip integrates a 2.5V internal reference voltage source and temperature detection circuit, and uses an external clock when working. XPT2046 can be powered by a single power supply, and the power supply voltage range is 2.7V ~ 5.5V. The reference voltage value directly determines the input range of the ADC. The reference voltage can use an internal reference voltage, or directly input a reference voltage within the range of 1V to VCC from the outside (the output impedance of the external reference voltage source is required to be low). X, Y, Z, VBAT, Temp and AUX analog signals enter the ADC after being selected by the on-chip control register, and the ADC can be configured as single-ended or differential mode. When selecting VBAT, Temp and AUX, it should be configured as single-ended mode; when used as a touch screen application, it should be configured as differential mode, which can effectively eliminate the measurement error caused by the parasitic resistance of the drive switch and external interference, and improve conversion accuracy.

insert image description here
insert image description here
insert image description here
insert image description here
Calibration of the touch screen:

Touch the 4 corners of the LCD screen to collect the 12-bit values ​​of the X and Y coordinates. At this time, the minimum and maximum values ​​of the X coordinate and Y coordinate are obtained respectively. According to the resolution of the LCD screen, and then through the mathematical equivalence formula, the 12-bit ADC value obtained through collection can be displayed on which part of the touch screen. Clicked.

In simple terms, a resistive touch screen is a sensor that uses pressure sensing for control, converting the physical position of a touch point (X, Y) in a rectangular area into a voltage representing the X and Y coordinates. The main part of a resistive touch screen is A resistive film screen that fits well with the display surface. This is a multi-layer composite film. It uses a layer of glass or hard plastic plate as the base layer, and the surface is coated with a conductive layer (transparent conductive resistor). The inner surface is also coated with a layer of hardened smooth anti-scratch plastic layer, and there are many small (less than 1/1000 inch) transparent isolation points between them to separate the two conductive layers. When a finger touches the screen, the two conductive layers are in contact at the touch point, and the resistance changes. One of the conductive layers is connected to a 5V uniform voltage field in the Y-axis direction, so that the voltage of the detection layer changes from zero to non-zero. , after the controller detects this connection, it performs A/D conversion, and compares the obtained voltage value with 5V to obtain the Y-axis coordinate of the touch point, and similarly obtains the X-axis coordinate, and then Works like a simulated mouse. This is the most basic principle common to all resistive technology touch screens. The coordinate value obtained by pressure induction cannot achieve 100% accuracy, and there are errors in it. Due to the existence of errors, the graphics drawn on the touch screen and the graphics on the LCD screen will have deviations in the set of corresponding points. When a button is clicked or a function is selected on the touch screen, the built-in software cannot correct Click to make a correct response, and the touch screen is discrete, and the density of any two touch points cannot be exactly the same, so almost all devices with resistive touch screens must undergo certain calibration before leaving the factory. Calibration is a process of graphic reconstruction. That is to say, after the graphics are transformed, the point set consistent with the LCD screen is converted. The existing calibration algorithm is mainly used to improve the above-mentioned inherent errors.

insert image description here
Reference Electrical Schematic

insert image description here
Arduino Uno Numeric Keypad

A small program that emulates a numeric keypad using a 2.4" TFT touchscreen program does not act as a USB HID device, FYI only tested on an Arduino UNO and using the TFT touchscreen driver 0x9341

The "Numpad" program is fully functional and styled:

The layout of the "numpad-layout" program has no styles but does work (takes up less space), to see the program in action, open the Serial Monitor and get started.

Screens used:

2.4" TFT touch screen 240x320 (MCUFriend)

driver 0x9341

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百五十六:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

 项目十八:测试并使用 TFT 触摸屏驱动程序 0x9341

 模块直插,引脚用法如下:

 LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK

 Arduino Uno A3 A2 A1 A0 A4 10 11 12 13

 LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7

 Arduino Uno 8 9 2 3 4 5 6 7

*/

#include <Adafruit_GFX.h>

#include <TouchScreen.h>

#include <Adafruit_TFTLCD.h>

#define YP A2 // must be an analog pin, use "An" notation!

#define XM A1 // must be an analog pin, use "An" notation!

#define YM 6  // can be a digital pin

#define XP 7  // can be a digital pin

#define TS_MINX 150

#define TS_MINY 120

#define TS_MAXX 850

#define TS_MAXY 891

//SPI Communication

#define LCD_CS A3

#define LCD_CD A2

#define LCD_WR A1

#define LCD_RD A0 

// optional

#define LCD_RESET A4 

//Color Definitons

#define BLACK  0x0000

#define WHITE  0xFFFF

#define RED   0xF800

#define GREEN  0x07E0

#define CYAN  0x07FF

#define MINPRESSURE 1

#define MAXPRESSURE 1000

// For better pressure precision, we need to know the resistance

// between X+ and X- Use any multimeter to read it

// For the one we're using, its 300 ohms across the X plate

// Pins A2-A6

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

//2.4 = 240 x 320

//Height 319 to fit on screen

//Size of key containers 70px

#define BOXSIZE 70

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

 //Container variables for touch coordinates

 int X, Y, Z;

  

 //Screen height without hidden pixel

 double tHeight = tft.height()-1;

 //Centering the mid square

 double center = (tft.width()/2)-(BOXSIZE/2);

 //Space between squares

 double padding = 10;

 //Position of squares to the left and right of center

 double fromCenter = BOXSIZE + padding;

 //Second row Y-Axis position

 double secondRow = BOXSIZE + padding;

 //Third row Y-Axis position

 double thirdRow = secondRow + BOXSIZE + padding;

 //Fourth row Y-Axis position

 double fourthRow = thirdRow + BOXSIZE + padding;

 //Y-Axis align for all squares

 double verticalAlign = (tHeight-((BOXSIZE * 4)+(padding * 3)))/2;

 //Left column starting x posision

 double leftColPositionX = center - fromCenter;

 //Mid column starting x posision

 double midColPositionX = center;

 //Right column starting x posision

 double rightColPositionX = center + fromCenter;

void setup() {
    
      

 Serial.begin(9600);

 tft.reset();  

 uint16_t identifier = tft.readID();

 tft.begin(identifier);

 //Background color

 tft.fillScreen(BLACK);

 createButtons();

 Serial.println(F("按 TFT 屏幕上的任意按钮:"));

}

void loop() {
    
    

 retrieveTouch();

 int boxHeightRow1 = verticalAlign + BOXSIZE;

 int boxHeightRow2 = secondRow + BOXSIZE;

 int boxHeightRow3 = thirdRow + BOXSIZE;

 int boxHeightRow4 = fourthRow + BOXSIZE;

 if(Z > MINPRESSURE && Z < MAXPRESSURE){
    
    

  //Check if element clicked is in left column

  if(X > leftColPositionX && X < (leftColPositionX+BOXSIZE)){
    
    

   //Check if element clicked is in row 1

   if(Y > verticalAlign){
    
    

    if(Y < boxHeightRow1){
    
    

       Serial.println("1");

       delay(150); 

    }

    //Check if element clicked is in row 2

    else if(Y < boxHeightRow2){
    
    

       Serial.println("4");

       delay(150); 

    }

    //Check if element clicked is in row 3

    else if(Y < boxHeightRow3){
    
    

       Serial.println("7");

       delay(150); 

    }

    //Check if element clicked is in row 4

    else if(Y < boxHeightRow4){
    
    

       Serial.println("0");

       delay(150); 

    }     

   }

    //Check if element clicked is in mid column

  } else if (X > midColPositionX && X < (midColPositionX+BOXSIZE)){
    
    

   //Check if element clicked is in row 1

    if(Y > verticalAlign){
    
    

     if(Y < boxHeightRow1){
    
    

        Serial.println("2");

        delay(150); 

     }

     //Check if element clicked is in row 2

     else if(Y < boxHeightRow2){
    
    

        Serial.println("5");

        delay(150); 

     }

     //Check if element clicked is in row 3

     else if(Y < boxHeightRow3){
    
    

        Serial.println("8");

        delay(150); 

     }

     //Check if element clicked is in row 4

     else if(Y < boxHeightRow4){
    
    

        Serial.println("0");

        delay(150); 

     }    

    }

   //Check if element clicked is in third column

  } else if (X > rightColPositionX && X < (rightColPositionX+BOXSIZE)){
    
    

    if(Y > verticalAlign){
    
    

     //Check if element clicked is in row 1

     if(Y < boxHeightRow1){
    
    

        Serial.println("3");

        delay(150); 

     }

     //Check if element clicked is in row 2

     else if(Y < boxHeightRow2){
    
    

        Serial.println("6");

        delay(150); 

     }

     //Check if element clicked is in row 3

     else if(Y < boxHeightRow3){
    
    

        Serial.println("9");

        delay(150); 

     }

     //Check if element clicked is in row 3

     else if(Y < boxHeightRow4){
    
    

        Serial.println(".");

        delay(150); 

     }     

    }

  }

 }

}

void retrieveTouch()

{
    
    

  digitalWrite(13, HIGH); 

  TSPoint p = ts.getPoint();

  digitalWrite(13, LOW);

  //If sharing pins, you'll need to fix the directions of the touchscreen pins

  pinMode(XM, OUTPUT); 

  pinMode(YP, OUTPUT); 

  

  //Scale from 0->1023 to tft.width

  X = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());

  Y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());

  Z = p.z;

}

void createButtons(){
    
    

 //(initial x,initial y,width,height,color)

 double secondRowVertialAlign = secondRow + verticalAlign;

 double thirdRowVertialAlign = thirdRow + verticalAlign;

 double fourthRowVertialAlign = fourthRow + verticalAlign;

 /***Draw squares with specified dimensions and position (No Fill)***/

 //(initial x,initial y,width,height,color)

 //First Row

 tft.drawRect(leftColPositionX, verticalAlign, BOXSIZE, BOXSIZE, WHITE);

 tft.drawRect(midColPositionX, verticalAlign, BOXSIZE, BOXSIZE, WHITE);

 tft.drawRect(rightColPositionX, verticalAlign, BOXSIZE, BOXSIZE, WHITE);

 //Second Row

 tft.drawRect(leftColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, CYAN);

 tft.drawRect(midColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, CYAN);

 tft.drawRect(rightColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, CYAN);

 //Third Row

 tft.drawRect(leftColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREEN);

 tft.drawRect(midColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREEN);

 tft.drawRect(rightColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREEN);

 //Fourth Row

 tft.drawRect(leftColPositionX, fourthRowVertialAlign, (BOXSIZE*2)+padding, BOXSIZE, RED);

 tft.drawRect(rightColPositionX, fourthRowVertialAlign, BOXSIZE, BOXSIZE, RED);

}


Experimental record:

1. White screen with no display

2. Open the serial port to view, the program is running, press any area, the number can be printed

3. The touch screen can be used, but the LCD screen cannot work. I don’t know where the problem lies.

insert image description here

This program mainly uses three libraries −

#include <Adafruit_GFX.h>  // 核心图形库
#include <Adafruit_TFTLCD.h> // 特定硬件的库
#include <TouchScreen.h> // 触摸屏驱动库

The specific hardware library used in the previous running program is "MCUFRIEND_kbv.h", which can be adapted to a 2.4-inch screen.

It seems that the white screen problem lies in the hardware driver library, Adafruit_TFTLD.h related settings or the use of functions

Later try modifying:

void setup() {
    
      
  Serial.begin(9600);
  tft.reset();  
  uint16_t identifier = tft.readID();
  tft.begin(identifier);

for

void setup() {
    
    
  Serial.begin(9600);
  tft.reset();
  tft.begin(0x9341);

After directly pointing out that it is the ILI9341 chip, there is finally a simple display on the screen

Experimental project 18 scene graph

insert image description here
Revised Item 18, Experimental Procedures

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百五十六:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

 项目十八:测试并使用 TFT 触摸屏驱动程序 0x9341

 模块直插,引脚用法如下:

 LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK

 Arduino Uno A3 A2 A1 A0 A4 10 11 12 13

 LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7

 Arduino Uno 8 9 2 3 4 5 6 7

*/

#include <Adafruit_GFX.h>

#include <TouchScreen.h>

#include <Adafruit_TFTLCD.h>

#define YP A2 // must be an analog pin, use "An" notation!

#define XM A1 // must be an analog pin, use "An" notation!

#define YM 6  // can be a digital pin

#define XP 7  // can be a digital pin

#define TS_MINX 150

#define TS_MINY 120

#define TS_MAXX 850

#define TS_MAXY 891

//SPI Communication

#define LCD_CS A3

#define LCD_CD A2

#define LCD_WR A1

#define LCD_RD A0

// optional

#define LCD_RESET A4

//Color Definitons

#define BLACK  0x0000

#define WHITE  0xFFFF

#define RED   0xF800

#define GREEN  0x07E0

#define CYAN  0x07FF

#define MINPRESSURE 1

#define MAXPRESSURE 1000

// For better pressure precision, we need to know the resistance

// between X+ and X- Use any multimeter to read it

// For the one we're using, its 300 ohms across the X plate

// Pins A2-A6

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

//2.4 = 240 x 320

//Height 319 to fit on screen

//Size of key containers 70px

#define BOXSIZE 70

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

//Container variables for touch coordinates

int X, Y, Z;

//Screen height without hidden pixel

double tHeight = tft.height() - 1;

//Centering the mid square

double center = (tft.width() / 2) - (BOXSIZE / 2);

//Space between squares

double padding = 10;

//Position of squares to the left and right of center

double fromCenter = BOXSIZE + padding;

//Second row Y-Axis position

double secondRow = BOXSIZE + padding;

//Third row Y-Axis position

double thirdRow = secondRow + BOXSIZE + padding;

//Fourth row Y-Axis position

double fourthRow = thirdRow + BOXSIZE + padding;

//Y-Axis align for all squares

double verticalAlign = (tHeight - ((BOXSIZE * 4) + (padding * 3))) / 2;

//Left column starting x posision

double leftColPositionX = center - fromCenter;

//Mid column starting x posision

double midColPositionX = center;

//Right column starting x posision

double rightColPositionX = center + fromCenter;

void setup() {
    
    

 Serial.begin(9600);

 tft.reset();

 tft.begin(0x9341);

 //Background color

 tft.fillScreen(BLACK);

 Serial.println(F("准备就绪OK!"));

 createButtons();

 Serial.println(F("请按 TFT 屏幕上的任意按钮:"));

}

void loop() {
    
    

 retrieveTouch();

 int boxHeightRow1 = verticalAlign + BOXSIZE;

 int boxHeightRow2 = secondRow + BOXSIZE;

 int boxHeightRow3 = thirdRow + BOXSIZE;

 int boxHeightRow4 = fourthRow + BOXSIZE;

 if (Z > MINPRESSURE && Z < MAXPRESSURE) {
    
    

  //Check if element clicked is in left column

  if (X > leftColPositionX && X < (leftColPositionX + BOXSIZE)) {
    
    

   //Check if element clicked is in row 1

   if (Y > verticalAlign) {
    
    

    if (Y < boxHeightRow1) {
    
    

     Serial.println("1");

     delay(150);

    }

    //Check if element clicked is in row 2

    else if (Y < boxHeightRow2) {
    
    

     Serial.println("4");

     delay(150);

    }

    //Check if element clicked is in row 3

    else if (Y < boxHeightRow3) {
    
    

     Serial.println("7");

     delay(150);

    }

    //Check if element clicked is in row 4

    else if (Y < boxHeightRow4) {
    
    

     Serial.println("0");

     delay(150);

    }

   }

   //Check if element clicked is in mid column

  } else if (X > midColPositionX && X < (midColPositionX + BOXSIZE)) {
    
    

   //Check if element clicked is in row 1

   if (Y > verticalAlign) {
    
    

    if (Y < boxHeightRow1) {
    
    

     Serial.println("2");

     delay(150);

    }

    //Check if element clicked is in row 2

    else if (Y < boxHeightRow2) {
    
    

     Serial.println("5");

     delay(150);

    }

    //Check if element clicked is in row 3

    else if (Y < boxHeightRow3) {
    
    

     Serial.println("8");

     delay(150);

    }

    //Check if element clicked is in row 4

    else if (Y < boxHeightRow4) {
    
    

     Serial.println("0");

     delay(150);

    }

   }

   //Check if element clicked is in third column

  } else if (X > rightColPositionX && X < (rightColPositionX + BOXSIZE)) {
    
    

   if (Y > verticalAlign) {
    
    

    //Check if element clicked is in row 1

    if (Y < boxHeightRow1) {
    
    

     Serial.println("3");

     delay(150);

    }

    //Check if element clicked is in row 2

    else if (Y < boxHeightRow2) {
    
    

     Serial.println("6");

     delay(150);

    }

    //Check if element clicked is in row 3

    else if (Y < boxHeightRow3) {
    
    

     Serial.println("9");

     delay(150);

    }

    //Check if element clicked is in row 3

    else if (Y < boxHeightRow4) {
    
    

     Serial.println(".");

     delay(150);

    }

   }

  }

 }

}

void retrieveTouch()

{
    
    

 digitalWrite(13, HIGH);

 TSPoint p = ts.getPoint();

 digitalWrite(13, LOW);

 //If sharing pins, you'll need to fix the directions of the touchscreen pins

 pinMode(XM, OUTPUT);

 pinMode(YP, OUTPUT);

 //Scale from 0->1023 to tft.width

 X = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());

 Y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());

 Z = p.z;

}

void createButtons() {
    
    

 //(initial x,initial y,width,height,color)

 double secondRowVertialAlign = secondRow + verticalAlign;

 double thirdRowVertialAlign = thirdRow + verticalAlign;

 double fourthRowVertialAlign = fourthRow + verticalAlign;

 /***Draw squares with specified dimensions and position (No Fill)***/

 //(initial x,initial y,width,height,color)

 //First Row

 tft.drawRect(leftColPositionX, verticalAlign, BOXSIZE, BOXSIZE, WHITE);

 tft.drawRect(midColPositionX, verticalAlign, BOXSIZE, BOXSIZE, WHITE);

 tft.drawRect(rightColPositionX, verticalAlign, BOXSIZE, BOXSIZE, WHITE);

 //Second Row

 tft.drawRect(leftColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, CYAN);

 tft.drawRect(midColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, CYAN);

 tft.drawRect(rightColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, CYAN);

 //Third Row

 tft.drawRect(leftColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREEN);

 tft.drawRect(midColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREEN);

 tft.drawRect(rightColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREEN);

 //Fourth Row

 tft.drawRect(leftColPositionX, fourthRowVertialAlign, (BOXSIZE * 2) + padding, BOXSIZE, RED);

 tft.drawRect(rightColPositionX, fourthRowVertialAlign, BOXSIZE, BOXSIZE, RED);

}


After touching the square on the screen, the experimental serial port returns

insert image description here

[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)

Experiment 156: 2.4-inch TFT LCD touch screen color screen module TFT-LCD high-definition true color display

Project Nineteen: Arduino Uno Numeric Keypad (Light Gray)

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百五十六:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

 项目十九:Arduino Uno 数字键盘(浅灰色)

 模块直插,引脚用法如下:

 LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK

 Arduino Uno A3 A2 A1 A0 A4 10 11 12 13

 LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7

 Arduino Uno 8 9 2 3 4 5 6 7

*/

#include <Adafruit_GFX.h>

#include <TouchScreen.h>

#include <Adafruit_TFTLCD.h>

#define YP A2 // must be an analog pin, use "An" notation!

#define XM A1 // must be an analog pin, use "An" notation!

#define YM 6  // can be a digital pin

#define XP 7 // can be a digital pin

#define TS_MINX 150

#define TS_MINY 120

#define TS_MAXX 850

#define TS_MAXY 891

//SPI Communication

#define LCD_CS A3

#define LCD_CD A2

#define LCD_WR A1

#define LCD_RD A0

// optional

#define LCD_RESET A4

//Color Definitons

#define BLACK   0x0000

#define BLUE   0x001F

#define GREY   0xCE79

#define LIGHTGREY 0xDEDB

#define MINPRESSURE 1

#define MAXPRESSURE 1000

// For better pressure precision, we need to know the resistance

// between X+ and X- Use any multimeter to read it

// For the one we're using, its 300 ohms across the X plate

// Pins A2-A6

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

//Size of key containers 70px

#define BOXSIZE 70

//2.4 = 240 x 320

//Height 319 to fit on screen

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

//Container variables for touch coordinates

int X, Y, Z;

//Screen height without hidden pixel

double tHeight = tft.height() - 1;

//Centering the mid square

double center = (tft.width() / 2) - (BOXSIZE / 2);

//Space between squares

double padding = 10;

//Position of squares to the left and right of center

double fromCenter = BOXSIZE + padding;

//Second row Y-Axis position

double secondRow = BOXSIZE + padding;

//Third row Y-Axis position

double thirdRow = secondRow + BOXSIZE + padding;

//Fourth row Y-Axis position

double fourthRow = thirdRow + BOXSIZE + padding;

//Y-Axis align for all squares

double verticalAlign = (tHeight - ((BOXSIZE * 4) + (padding * 3))) / 2;

//Left column starting x posision

double leftColPositionX = center - fromCenter;

//Mid column starting x posision

double midColPositionX = center;

//Right column starting x posision

double rightColPositionX = center + fromCenter;

void setup() {
    
    

 Serial.begin(9600);

 tft.reset();

 tft.begin(0x9341);

 //Background color

 tft.fillScreen(LIGHTGREY);

 createButtons();

 insertNumbers();

 Serial.println(F("Press any button on the TFT screen: "));

}

void loop() {
    
    

 retrieveTouch();

 int boxHeightRow1 = verticalAlign + BOXSIZE;

 int boxHeightRow2 = secondRow + BOXSIZE;

 int boxHeightRow3 = thirdRow + BOXSIZE;

 int boxHeightRow4 = fourthRow + BOXSIZE;

 if (Z > MINPRESSURE && Z < MAXPRESSURE) {
    
    

  //Check if element clicked is in left column

  if (X > leftColPositionX && X < (leftColPositionX + BOXSIZE)) {
    
    

   //Check if element clicked is in row 1

   if (Y > verticalAlign) {
    
    

    if (Y < boxHeightRow1) {
    
    

     Serial.println("1");

     delay(150);

    }

    //Check if element clicked is in row 2

    else if (Y < boxHeightRow2) {
    
    

     Serial.println("4");

     delay(150);

    }

    //Check if element clicked is in row 3

    else if (Y < boxHeightRow3) {
    
    

     Serial.println("7");

     delay(150);

    }

    //Check if element clicked is in row 4

    else if (Y < boxHeightRow4) {
    
    

     Serial.println("0");

     delay(150);

    }

   }

   //Check if element clicked is in mid column

  } else if (X > midColPositionX && X < (midColPositionX + BOXSIZE)) {
    
    

   //Check if element clicked is in row 1

   if (Y > verticalAlign) {
    
    

    if (Y < boxHeightRow1) {
    
    

     Serial.println("2");

     delay(150);

    }

    //Check if element clicked is in row 2

    else if (Y < boxHeightRow2) {
    
    

     Serial.println("5");

     delay(150);

    }

    //Check if element clicked is in row 3

    else if (Y < boxHeightRow3) {
    
    

     Serial.println("8");

     delay(150);

    }

    //Check if element clicked is in row 4

    else if (Y < boxHeightRow4) {
    
    

     Serial.println("0");

     delay(150);

    }

   }

   //Check if element clicked is in third column

  } else if (X > rightColPositionX && X < (rightColPositionX + BOXSIZE)) {
    
    

   if (Y > verticalAlign) {
    
    

    //Check if element clicked is in row 1

    if (Y < boxHeightRow1) {
    
    

     Serial.println("3");

     delay(150);

    }

    //Check if element clicked is in row 2

    else if (Y < boxHeightRow2) {
    
    

     Serial.println("6");

     delay(150);

    }

    //Check if element clicked is in row 3

    else if (Y < boxHeightRow3) {
    
    

     Serial.println("9");

     delay(150);

    }

    //Check if element clicked is in row 3

    else if (Y < boxHeightRow4) {
    
    

     Serial.println(".");

     delay(150);

    }

   }

  }

 }

}

void retrieveTouch()

{
    
    

 digitalWrite(13, HIGH);

 TSPoint p = ts.getPoint();

 digitalWrite(13, LOW);

 //If sharing pins, you'll need to fix the directions of the touchscreen pins

 pinMode(XM, OUTPUT);

 pinMode(YP, OUTPUT);

 //Scale from 0->1023 to tft.width

 X = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());

 Y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());

 Z = p.z;

}

void createButtons() {
    
    

 //(initial x,initial y,width,height,color)

 double secondRowVertialAlign = secondRow + verticalAlign;

 double thirdRowVertialAlign = thirdRow + verticalAlign;

 double fourthRowVertialAlign = fourthRow + verticalAlign;

 /***Draw filled squares with specified dimensions and position***/

 //First Row

 tft.fillRect(leftColPositionX, verticalAlign, BOXSIZE, BOXSIZE, GREY);

 tft.fillRect(midColPositionX, verticalAlign, BOXSIZE, BOXSIZE, GREY);

 tft.fillRect(rightColPositionX, verticalAlign, BOXSIZE, BOXSIZE, GREY);

 //Second Row

 tft.fillRect(leftColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, GREY);

 tft.fillRect(midColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, GREY);

 tft.fillRect(rightColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, GREY);

 //Third Row

 tft.fillRect(leftColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREY);

 tft.fillRect(midColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREY);

 tft.fillRect(rightColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREY);

 //Fourth Row

 tft.fillRect(leftColPositionX, fourthRowVertialAlign, (BOXSIZE * 2) + padding, BOXSIZE, GREY);

 tft.fillRect(rightColPositionX, fourthRowVertialAlign, BOXSIZE, BOXSIZE, GREY);

 /***Draw Borders around squares***/

 //First Row

 tft.drawRect(leftColPositionX, verticalAlign, BOXSIZE, BOXSIZE, BLACK);

 tft.drawRect(midColPositionX, verticalAlign, BOXSIZE, BOXSIZE, BLACK);

 tft.drawRect(rightColPositionX, verticalAlign, BOXSIZE, BOXSIZE, BLACK);

 //Second Row

 tft.drawRect(leftColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);

 tft.drawRect(midColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);

 tft.drawRect(rightColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);

 //Third Row

 tft.drawRect(leftColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);

 tft.drawRect(midColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);

 tft.drawRect(rightColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);

 //Fourth Row

 tft.drawRect(leftColPositionX, fourthRowVertialAlign, (BOXSIZE * 2) + padding, BOXSIZE, BLACK);

 tft.drawRect(rightColPositionX, fourthRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);

}

void insertNumbers() {
    
    

 //Centers text horizontally on all three columns

 double leftColCursorX  = leftColPositionX + (BOXSIZE / 3);

 double midColCursorX  = midColPositionX + (BOXSIZE / 3);

 double rightColCursorX = rightColPositionX + (BOXSIZE / 3);

 //Centers text horizontally on all four rows

 double firstRowCursorY = verticalAlign + (BOXSIZE / 3);

 double secondRowCursorY = secondRow + firstRowCursorY;

 double thirdRowCursorY = thirdRow + firstRowCursorY;

 double fourthRowCursorY = fourthRow + firstRowCursorY;

 tft.setTextSize(4);

 tft.setTextColor(BLACK);

 //Insert Number 1

 tft.setCursor(leftColCursorX, firstRowCursorY);

 tft.println("1");

 //Insert Number 2

 tft.setCursor(midColCursorX, firstRowCursorY);

 tft.println("2");

 //Insert Number 3

 tft.setCursor(rightColCursorX, firstRowCursorY);

 tft.println("3");

 //Insert Number 4

 tft.setCursor(leftColCursorX, secondRowCursorY);

 tft.println("4");

 //Insert Number 5

 tft.setCursor(midColCursorX, secondRowCursorY);

 tft.println("5");

 //Insert Number 6

 tft.setCursor(rightColCursorX, secondRowCursorY);

 tft.println("6");

 //Insert Number 7

 tft.setCursor(leftColCursorX, thirdRowCursorY);

 tft.println("7");

 //Insert Number 8

 tft.setCursor(midColCursorX, thirdRowCursorY);

 tft.println("8");

 //Insert Number 9

 tft.setCursor(rightColCursorX, thirdRowCursorY);

 tft.println("9");

 //Insert Number 0

 tft.setCursor(leftColPositionX + BOXSIZE, fourthRowCursorY);

 tft.println("0");

 //Insert Period Character

 tft.setCursor(rightColCursorX, fourthRowCursorY);

 tft.println(".");

}

Arduino experiment scene diagram

insert image description here
[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)

Experiment 153: 2.4-inch TFT LCD touch screen color screen module TFT-LCD high-definition true color display

Project 20: Print the data detected by the touch screen through the serial port

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

 项目二十:串口打印触摸屏检测的数据

 模块直插,引脚用法如下:

 LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK

 Arduino Uno A3 A2 A1 A0 A4 10 11 12 13

 LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7

 Arduino Uno 8 9 2 3 4 5 6 7

*/

#include <stdint.h>

#include "TouchScreen.h" //导入触摸驱动库

#define YP A2

#define XM A1

#define YM 6

#define XP 7

// 为了更好的压力精度,我们需要知道阻力

// 在 X+ 和 X- 之间使用任何万用表读取它

// 对于我们使用的那个,它在 X 板上的 300 欧姆

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

void setup(void) {
    
    

 Serial.begin(9600);

}

void loop(void) {
    
    

 TSPoint p = ts.getPoint();

 if (p.z > ts.pressureThreshhold) {
    
    

  Serial.print("X = "); Serial.print(p.x);

  Serial.print("\tY = "); Serial.print(p.y);

  Serial.print("\tPressure = "); Serial.println(p.z);

 }

 delay(100);

}

Experimental serial port return

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/131625610