Raspberry Pi Touch Screen


If I was taking possession of a replacement blow-up-Suzie, I would understand the need for discretion. However as seems to be more the case these days, my newly-acquired LCD touchscreen from JayCar's pricy Dweeb Emporium came in a plain padded box, simply stamped 5" LCD Display. Succinct. A barcoded sticker on the back further revealed, Display Touch HDMI USB 800x480 5in. So what's the big secret..!? Or maybe there's just an ink shortage..

While I wasn't expecting a lightly-perfumed and personalised greetings card - "Congratulation you buy, proud owner best LCD!" I do remember when new gear came in attractive packaging with a name, make and model and little instructional leaflets and other feel-good paraphernalia. Warranty information even. I suppose it says something about me, but obfuscation - especially by manufacturers - automatically makes me suspicious.

My friends, we have truly lurched from the era of the cheap, cornershop generic brand to a time where we embrace (or are scraping the barrel's arse of) the ultimate in no-name branding. Literally. So does the packaging reflect the product quality?

The screen's PCB is marked thus:


The inevitable perusal of the internet (the instruction leaflet of our time) reveals.."The XPT2046 is a 4-wire resistive touch screen controller that incorporates a 12-bit 125 kHz blah..blah" - whoa, stop there I almost creamed. The screen is probably made by a Chinese company called Shenzhen Xptek Tech. The Chinglish description on their website did nothing to reassure me, but the thing seems to work to some extent.

While we're on the subject of lowbrow literacy, there's a lot of utter bollocks around the web relating to how to set these things up with your Raspberry Pi. So here's the abridged version using just a few steps..

For the record I'm attaching the above screen to a Raspberry Pi 3 Model B+ and using RasPi OS (formerly Raspbian) version 4.19 / May 2020. As usual, read through these instructions before starting as it will save much screaming and gnashing of teeth. It works on other Pi B versions too, if you insist.

I recommend starting with a fresh install of RasPi OS with desktop (Buster), followed by configuring it to receive the anticipated-if-slightly-anonymous touch screen, followed by attaching it to said touch screen and away you go.

Doin' it

Once booted with internet access, open a terminal window and make a backup of the original config file, just in case you fuck things up..

sudo cp /boot/config.txt /boot/config.bak

Then edit this file using your editor of choice (I like vi but I can be a bit masochistic sometimes)..

sudo vi /boot/config.txt

Copy and paste the following lines at the end of config.txt and then save..

hdmi_group=2
hdmi_mode=1
hdmi_mode=87
hdmi_cvt 800 400 60 6 0 0 0
dtparam=spi=on
dtparam=i2c_arm=on
dtoverlay=ads7846,cs=1,penirq=25,penirq_pull=2,speed=50000,keep_vref_on=0,swapxy=0,pmax=255,xohms=150,xmin=200,xmax=3900,ymin=200,ymax=3900
dtoverlay=w1-gpio-pullup,gpiopin=4,extpullup=1

Scrollable on small screens..

Secondly, run the following command to install the touch screen software..

sudo apt-get install xinput-calibrator

Nearly done. Shortly you will need to modify the final touch screen config file, 99-calibration.conf once the screen is attached. But first you need to find it (the file - not the screen!), because it could be in one of two places. Usually it can be found here:

/etc/X11/xorg.conf.d/99-calibration.conf

..but sometime's it's here:

/usr/share/X11/xorg.conf.d/99-calibration.conf

If the top path doesn't work, try the lower one and modify the following instructions accordingly. Note that the X in X11 is capitalised and yes, xorg.conf.d is a directory..

You will be instructed about this when you run the touch-screen calibration in a minute, when it will all make sense. For now go back to that terminal window (you didn't close it, did you..!?) and run these commands:

sudo chmod 777 /etc/X11/xorg.conf.d/99-calibration.conf

sudo cp /etc/X11/xorg.conf.d/99-calibration.conf /etc/X11/xorg.conf.d/99-calibration.conf.bak
Scrollable on small screens..

I may have used a slightly different naming convention when trying to make all this work. Open the Raspbian file explorer and take a look at the files..

Close everything and shut down the RasPi.

After flicking the Backlight switch on (power saver) the screen slides onto the first 13 pairs of pins of the RasPi's 40-pin GPIO header. If it's a bit tight spray everything with WD40 nah just kidding, probably don't do that. The easiest way is to carefully pop it in so that the two HDMI ports are aligned. Then you can insert the nifty double-doodah thingo into the HDMI sockets which was my favourite part..


Keep a USB mouse and keyboard plugged into the RasPi to help while you configure the pointer. Subsequent boot of the RasPi should bring up the screen and the Calibrate option under Preferences..


You may have to do this a few times to get the bundled plastic pointer working as well as the mouse. Make sure you can click on the Shutdown part at the bottom of the start menu in particular (for later).

Once calibrated you will be prompted by a screen. Use the USB mouse to only highlight the text from Section "InputClass" down to EndSection (not the whole lot). Then right click and choose copy..


Then open the file explorer once again, navigate to 99-calibration.conf as above and double click on the file to open it in a graphical editor. Delete everything in the file and paste in the new configuration data. Save and close.

You should now be able to reboot the RasPi and use the touch screen with the plastic pointer.

I'd still do this reboot with the USB keyboard and mouse to make sure it works. You can always recalibrate the RasPi or, if it only boots to the Command Line Interface (blank screen, white writing) it means an error has occurred. This should easily be fixed by restoring the original config file and starting again (aren't you glad you backed it up)..

sudo rm /etc/X11/xorg.conf.d/99-calibration.conf

sudo cp /etc/X11/xorg.conf.d/99-calibration.conf.bak /etc/X11/xorg.conf.d/99-calibration.conf
Scrollable on small screens..

Reboot. Repeat. This is a good example of why you need to be able to run commands at the CLI/Terminal window.

I haven't really got any use for a touch screen of this ilk, preferring a wireless USB mouse (and keyboard). Most of my systems run headlessly and are configured remotely using SSH.

This screen could be connected via the usual HDMI plug instead of using the GPIO pins and this may be necessary on some RasPi models. However this causes it to lose the touch-screen functionality and just act like a normal mini monitor, which might just be the best use for it in the long run..

The argument for usable interfaces is continued in Using and Android mobile device as a Pi Screen. And imagine what you could do if you were also able to control your Raspberry Pi from your smart phone...


AndyM | July 2020