Calibrate Waveshare 5″ Resistive Touchscreen (and 7.9″ Capacitive)

config.txt
max_usb_current=1
hdmi_group=2
hdmi_mode=87
hdmi_cvt 800 480 60 6 0 0 0
hdmi_drive=1

# install calibrator and evdev (allows multiple keyboards mice treated as separate device)
sudo apt install xinput-calibrator xserver-xorg-input-evdev

# make evdev higher number than libinput
sudo mv /usr/share/X11/xorg.conf.d/10-evdev.conf /usr/share/X11/xorg.conf.d/45-evdev.conf

# list devices
xinput

# view calibration option
xinput --list-props <deviceid>

# calibrate
xinput_calibrator

# copy paste the result like below to /usr/share/X11/xorg.conf.d/99-calibration.conf:
Section "InputClass"
Identifier "calibration"
MatchProduct "BYZHYYZHY By ZH851"
Option "Calibration" "182 3859 277 3944"
Option "SwapAxes" "0"
EndSection

# checkĀ  the values
DISPLAY=:0 xinput list-props "BYZHYYZHY By ZH851"

# manual set values
DISPLAY=:0 xinput set-prop "BYZHYYZHY By ZH851" "Evdev Axis Calibration" 200, 3800, 275, 3850

ALTERNATIVE (using libinput):
# check calibration values
xinput_calibrator -v
DEBUG: Found that 'BYZHYYZHY By ZH851' is a sysfs name.
DEBUG: Adding click 0 (X=129, Y=86)
DEBUG: Adding click 1 (X=655, Y=96)
DEBUG: Adding click 2 (X=133, Y=406)
DEBUG: Adding click 3 (X=669, Y=401)

# check screen resolution
xrandr | grep current
Screen 0: minimum 800 x 480, current 800 x 480, maximum 800 x 480

# calculate values
a = (screen_width * 6 / 8) / (click_3_X - click_0_X)
c = ((screen_width / 8) - (a * click_0_X)) / screen_width
e = (screen_height * 6 / 8) / (click_3_Y - click_0_Y)
f = ((screen_height / 8) - (e * click_0_Y)) / screen_height

a = (800 * 6 / 8) / (669 - 129) = 1.1111
c = ((800 / 8) - (1.1111 * 129)) / 800 = -0.0542
e = (480 * 6 / 8) / (401 - 86) = 1.1429
f = ((480 / 8) - (1.1429 * 86)) / 480 = -0.0798

# test new values
xinput set-prop "BYZHYYZHY By ZH851" "libinput Calibration Matrix" a, 0.0, c, 0.0, e, f, 0.0, 0.0, 1.0

xinput set-prop "BYZHYYZHY By ZH851" "libinput Calibration Matrix" 1.1111, 0.0, -0.0542, 0.0, 1.1429, -0.0798, 0.0, 0.0, 1.0

# if calculation incorrect, reset and repeat calculation above
xinput set-prop "BYZHYYZHY By ZH851" "libinput Calibration Matrix" 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0

# make changes permanent to /usr/share/X11/xorg.conf.d/99-calibration.conf:
Section "InputClass"
Identifier "calibration"
MatchProduct "BYZHYYZHY By ZH851"
Option "TransformationMatrix" "1.1111 0.0 -0.0542 0.0 1.1429 -0.0798 0.0 0.0 1.0"
EndSection

For 7.9″ Capacitive Screen:

add this lines to /boot/config.txt:
#disable these settings below
#display_auto_detect=1
#dtoverlay=vc4-kms-v3d
#max_framebuffers=2
max_usb_current=1

hdmi_group=2
hdmi_mode=87
hdmi_timings=400 0 100 10 140 1280 10 20 20 2 0 0 0 60 0 43000000 3
display_rotate=3 # 1 - 90deg, 2 - 180deg, 3 - 270deg

Rotate 0 degree:
xinput set-prop "WaveShare WaveShare" "libinput Calibration Matrix" 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0

Rotate 90 degree:
xinput set-prop "WaveShare WaveShare" "libinput Calibration Matrix" 0.0, 1.0, 0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 1.0

Rotate 270 degree:
xinput set-prop "WaveShare WaveShare" "libinput Calibration Matrix" 0.0, -1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0

# make changes permanent to /usr/share/X11/xorg.conf.d/99-calibration.conf:
Section "InputClass"
Identifier "calibration"
MatchProduct "WaveShare WaveShare"
Option "TransformationMatrix" "0.0 -1.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0"
EndSection

Source:
https://www.waveshare.com/wiki/5inch_HDMI_LCD_(B)
https://www.waveshare.com/wiki/7.9inch_HDMI_LCD
https://wiki.archlinux.org/index.php/Talk:Calibrating_Touchscreen

Leave a Reply

Your email address will not be published. Required fields are marked *