Skip to content

Screen touch coordinates calibration tutorial

Author: Vincent Time: 2023/12/9

Foreword

For devices with touch screens, we would want the coordinates displayed on the screen to match the coordinates of the touch, however, the same size display may have a different resolution, and the same size or driven touch screen has a different output, and because the display and touchscreen are mounted together later, so it maybe have some problems like their coordinates are mirror or opposite.

Furthermore, due to different production processes, the output of different batches of touchscreens is not guaranteed to be consistent, even though they have the same size and driver.And our example code and test code are not guaranteed to match all screens. Even some touch screens have different initialisation codes, which requires the user to do the matching on their own.

If you get a screen and the touch effect doesn't match what is expected, then you need to modify the software. Besides, if the software changed the rotation direction of the display, it also needs to be modified.

Note this is not a hardware issue.

Rationale

Both displays and touchscreens use a right-angle coordinate system, also called a Cartesian coordinate system.

Generally, the upper left corner is the coordinate origin, to the right is the positive direction of the x-axis, and like down is the positive direction of the y-axis.

image.png

That means, we need to make sure that the 0X and 0Y values of the screen coordinates are the same as the touch coordinates 0X' and 0Y'.

And in practice, we will encounter the following problems:

(1) The 0X' and 0X ranges are inconsistent.

Such as 0X = a0X'+b, the two coordinate systems are not the same size.

(2) 0X' and 0X directions are reversed.

Such as 0X = WIDTH - 0X', the two coordinate systems produce a mirror image.

(3) 0Y' output direction is consistent with 0X instead of 0Y.

Such as 0X = 0Y', the two coordinate systems produce a rotation.

All display and touch mismatch problems are caused by a combination of the above three problems.

Calibration preparation

First of all, please make sure the output of the screen and the output of the touch screen work normally.

Before calibration, please determine the resolution of the screen, which the screen WIDTH and HIGHTH. For example, seven-inch screen has two specifications 1024 * 600 and 800 * 480, we need to determine which one it is.

Please determine the output mode of the touch screen, different driver libraries will have different data output:

  • Some will output 0~4095 AD value.
  • Some will output the calculated X and Y values of coordinates according to the resolution you input.
  • Some libraries will directly default the screen to 320*240, and output the data according to this resolution

Anyway, there will always end up being a function, like get_touch(), it can output the X and Y values of the coordinates of the press point. Then the display driver will always provide a function, like draw_pixel(), it can draw a point.

In this way, we can take the location of our touch, draw it on the screen, and then we can detect which of the problems it is.

To illustrate this accurately, we set the four corners of the screen as a,b,c,d respectively. And the coordinates are:

a(0,0)

b(WIDTH,0)

c(0,HEIGHT)

d(WIDTH,HEIGHT)

image.png

Calibration method

The problem 1

The 0X' and 0X ranges are inconsistent: When the finger touches point a and slides to point b, the X value output by get_touch() is larger or less than the actual distance, but it does increment in the right direction.

For this kind of problem, the Arduino provides a function, map(), for converting a number from its original range, to another range of values.

We can place our finger on point a to get the (Xa,Ya) returned by get_touch(), and then get the (Xb,Yb) for point b.

So the conversion formula is as follows:

X_out = map(X,Xa,Xb,0,WIDTH).

Similarly, 0Y' and 0Y range inconsistencies are calibrated in this way:

Y_out = map(Y,Ya,Yb,0,HEIGHT).

The problem 2

The 0X' and 0X directions are reversed: When the finger touches point a and slides to point b, the X value output by get_touch() is from large to small.

So the conversion formula is as follows:

X_out = WIDTH - X.

Similarly for the y direction:

Y_out = HEIGHT - Y.

The problem 3

The 0Y' output direction is the same as 0X instead of 0Y. When the finger is sliding in the ab direction, which is also the x direction, the get_touch() output has no change in X but a change in Y. This often occurs when the screen was rotated but the touch coordinates have not been rotated.

So the conversion formula is as follows:

X_out = Y;

Y_out = X.

Of course, this is often accompanied by problems with mirroring and inconsistent range, which can be solved according to 1, 2.

The above three methods can solve all the problem which about the touch coordinates and display coordinates do not match.