In the previous article, I described the steps needed to install an LCD touchscreen on the Raspberry Pi. In this article, I will show you how to adjust the screen rotation of the LCD to landscape mode, and will show you how to calibrate the touchscreen pointer for optimal accuracy. Just follow the steps below to compete the process of setting up your Raspberry Pi LCD touchscreen:

1. First we need to change the setting for screen rotation in the /boot/cmdline.txt file. This setting is called fbtft_device.rotate=X. By default, this is set to X=0, which results in a portrait mode screen orientation. In order to switch the orientation to landscape mode, change fbtft_device.rotate=0 to fbtft_device.rotate=90. Enter sudo nano /boot/cmdline.txt at the command prompt. There should only be one line in this file. Go to the end of it and you will find the fbtft_device.rotate=X setting. Change the value from 0 to 90:

lcd boot.cmdline.txt screen rotate

Press Ctrl-X then Y to exit nano and save the changes, then enter sudo reboot or sudo poweroff to reboot the Pi.

You should now see that the screen rotation has changed to landscape mode:

IMG_7672

However, if you try to touch the screen now, you will find that the pointer movement does not correspond to your finger movement. This is because the LCD screen driver and the touchscreen controller driver have separate settings for screen rotation. We need to change the rotation of the touchscreen controller driver to match the rotation of the LCD screen driver.

2. You probably noticed that dragging your finger to the right moves the pointer up, not to the right. This indicates that the x and y axes of the touchscreen are swapped. To correct this, we need to swap the x axis for the y axis. This can be done by changing the swap_xy=X parameter in /etc/modules.

Enter sudo nano /etc/modules at the command prompt to edit the file. Go to the line for the ads7846_device parameters and move the cursor to the right to find it:

lcd etc.modules after edit

Change swap_xy=0 to swap_xy=1:

lcd rotate swap_xy

Press Ctrl-X then Y to exit nano and save the changes, then enter sudo reboot to reboot the Pi.

Now if you drag your finger around the screen, you will notice that the y axis (up and down) is correctly aligned with the motion of your finger. However, the x axis (left and right) is still inverted. To fix this, we need to install two more kernel modules, xinput and evtest. xinput is a Linux utility that will allow us to configure input device settings for the touchscreen controller, and evtest is an input device event monitor and query tool.

3. Install the utilities by entering sudo apt-get -y install xinput evtest at the command prompt:

lcd install xinput and evdev

Press Ctrl-X then Y to exit nano and save the changes, then enter sudo reboot to reboot the Pi.

Now, we need to edit the /etc/X11/xinit/xinitrc file. Enter sudo nano /etc/X11/xinit/xinitrc at the command prompt to edit it:

lcd rotate invert x axis

Now add this code above the./etc/X11/Xsession line:

DISPLAY=:0 xinput --set-prop 'ADS7846 Touchscreen' 'Evdev Axis Inversion' 1 0

The file should now look like this:

lcd rotate invert x axis after edit2

Press Ctrl-X then Y to exit nano and save the changes, then enter sudo reboot to reboot the Pi.

After the Pi finishes rebooting, you should notice that when you move your finger across the touch screen, the pointer should follow correctly in both axes. If you are using the Raspberry Pi 2 Model B, you will need to complete the calibration steps below before the pointer follows your finger correctly (and make sure that you have enabled startx to load automatically – see step 6 in this article).

Change Screen Rotation to Any Orientation

You can rotate the screen 90 degrees (as we did in this tutorial) and the power connector will be at the bottom of the screen, but you can also rotate it 270 degrees so that the power connector is at the top of the screen. To do this, simply enter fbtft_device.rotate=270 in the /boot/cmdline.txt file. Then change the DISPLAY=:0 xinput --set-prop 'ADS7846 Touchscreen' 'Evdev Axis Inversion' 0 1 line in the /etc/X11/xinit/xinitrc file to DISPLAY=:0 xinput --set-prop 'ADS7846 Touchscreen' 'Evdev Axis Inversion' 1 0. All you need to do is switch the values of the 0 and 1 at the end of this line.

Here is a short video tutorial showing the steps above:

Touchscreen Calibration

Now that we have our LCD touchscreen up and running, the final step in the installation is the calibration of touch control. This will make the pointer much more accurate and easier to use.

1. This process needs to be done using super user (root) privileges. Enter sudo su at the command prompt to enter root mode:

lcd sudo su

2. Now we need to install the calibration tool we will be using, xinput_calibrator; and other filters for controlling the touchscreen response. Install the tslib library by entering aptitude install libts-bin:

lcd calibrate after tslib install

This will create a configuration file called /etc/ts.conf, which contains settings for variance and jitter that can be changed to optimize pointer response. See here for information about configuring ts.conf.

3. The calibration tool we will use is called ts_calibrate. We will also be using a program to check the results of the calibration called ts_test. In order to use ts_calibrate and ts_test, we must first set proper environmental variables. Enter export TSLIB_TSDEVICE=/dev/input/event0 into the command prompt, then enter export TSLIB_FBDEVICE=/dev/fb1:

lcd calibrate libts before ts.calibrate

You will need to re-enter these two lines each time you want to use ts_calibrate or ts_test if you log out of PuTTY or reboot your Raspberry Pi.

4. Now we can use ts_calibrate. Enter ts_calibrate at the command prompt (make sure you are still in root mode) to run the ts_calibrate program. The program will consecutively display five crosses on different parts of the screen, which you need to touch with as much precision as possible:

IMG_7689

PuTTY will output the calibration data after the calibration is complete:

lcd calibrate putty

This calibration data will be written to a calibration file called /etc/pointercal. To view the contents of this file, enter cat /etc/pointercal at the root command prompt.

5. We should now test the calibration by using the ts_test program. Enter ts_test at the root command prompt to start the program:

IMG_7713

Drag the cross around the screen and observe how closely it follows your finger or stylus to test the accuracy of the calibration. Now press the “Draw” button to enter the drawing mode:

IMG_7736

Drag your finger or stylus around the screen to test the accuracy of the calibration.

Press Ctrl-C to stop the test.

lcd calibrate ts test

If necessary, run ts_calibrate again to improve the calibration.

Now enter sudo reboot at the command prompt to reboot, and you should be all set to explore the Raspbian desktop on your LCD touch screen!

Watch the video for the calibration tutorial here:

This is kind of a long process, but it is well worth it if you want to get the LCD touchscreen set up properly. So if you have any trouble setting this up or have anything to say, please leave a comment below. Also, if you found this article useful, please share it with your friends!