How to Rotate a Raspberry Pi HDMI Touchscreen to Portrait Mode

I’m working on a project to create a writing device optimized for writing fiction. I figured one of the bigger reasons why I wasn’t writing anymore was the lack of a consistent writing platform. I’ve had some setbacks in the project, and compromises, and it’s not done yet, more on this project later. But the big hurdle I finally figured out, was how to get the Raspberry Pi 4 to project the screen in portrait mode.

In theory, according to most of the how-to’s on the internet (and there are quite few), this should be easy and accomplished using minimally invasive settings. However, in my case, I’ve got some odd hardware. I wanted a larger screen, to more accurately resemble a page of a book, and I wanted it touchscreen so I wouldn’t have to carry around a mouse when I was taking my bespoke word processor hardware out to the local coffee shop (once the world returns a little to normal). So I got a brandless 7-inch HDMI touchscreen off Amazon.

I quickly discovered what a host of forum posters before me complained about, that if you don’t use the Raspberry Pi official hardware, then all bets are off. Rotation won’t work through the GUI interface, and once you get it rotated by changing the config files, the touchscreen cursor shows up in a screen coordinate wildly different from where you touched. This is because the touchscreen device uses a separate driver than the screen itself, so it thinks the screen is still the orientation it used to be. Having a cursor appear several inches from where you touch the screen will drive a sane person to murder.

I ended up mostly using the system outlined in Instructables by GraysonL3, with a bunch of clues gleened from commenter grzbrz.

First step, rotate the screen. Open up the command prompt and edit the boot config file to change the rotation: sudo nano /boot/config.txt

I just added this line to swing it around 270 degrees: display_hdmi_rotate=3

The touchscreen hardware also required a bunch of new config lines to boost power to the HDMI port and such, so the boot config got a bit cluttered. Reboot and see if that works: sudo reboot

Presuming no problems with the rotation, next we need to work on the rotation of the touchscreen input. For that, we need to find the name of that input device. Typing into the command line: xinput list

This shows me that I’m using a mechanical keyboard and gaming mouse. By process of elimination, that gives me the name of the touchscreen device as: wch.cn USB2IIC_CTP_CONTROL

I know it’s a USB device, because a separate micro-USB port goes from the screen to the Raspberry Pi. At first I thought this was just for power, but using the HDMI cable as an input may not be possible, so of course the USB is the input, duh.

‘Xinput list’ will also give you the ID number (in my case 10), so you can drill down to the specific properties, including the current transformation matrix values with: xinput list-props 10

Now we find the Xorg calibration file and use the “TransformationMatrix” option to rotate that touch input:
cd /usr/share/X11/xorg.conf.d/
ls

You may find multiple config files in this folder, but you want the one with the input sections. Mine was named 40-libinput.conf so to edit:
sudo nano 40*

Then add this to the file as the first section and reboot:
Section “InputClass”
        Identifier “callibration”
        MatchProduct “USB2IIC_CTP_CONTROL”
        MatchDevicePath “/dev/input/event*”
        Driver “libinput”
        Option “TransformationMatrix” “0 -1 1 1 0 0 0 0 1”
EndSection

I intentionally misspelled “calibration” because the “Identifier” value is arbitrary and it allows me to verify in the logs that the system has implemented the new config settings. You buy that, right? The important part is the MatchProduct line. If it’s not clear from the xinput list which part is the product name (you’ll notice there’s an extra bit in the front for some reason that isn’t necessary in the config file), you may be able to use the whole string as “MatchDevice.” Or it might crash the system. Not sure which.

I’m not going to pretend to know how a transformation matrix works, if you end up using a different rotational direction, then consult the original Instructables article for other options. Also keep in mind that rotating the input the wrong way will look to you as if nothing happened.

In case of troubles, here’s some tools to help out. You can read the Xorg log, which has very verbose records of device setup and errors and will help you with the MatchProduct name.
cd /var/log/
ls
nano xorg.0.log

That’s presuming that’s the correct log name.

You can also look at the boot text, looking for errors, by typing:
dmesg|less
This is all that stuff that whips by really fast when your Raspberry Pi starts up. It’s actually saying some relevant stuff. To get out of that stream of text, just press ‘q’.

As usual, working with a Linux device is a little like having a pet that won’t take direction.

4 thoughts on “How to Rotate a Raspberry Pi HDMI Touchscreen to Portrait Mode

  • But I AM using “official raspberry pi hardware.” In the past, a simple lcd_rotate=2 would rotate screen orientation 180 degrees and allow my official pi touchscreen to sit as designed so I could velcro it to my dash. No such luck this time.
    As far as cd /usr/share/X11/xorg.conf.d/ is concerned–there’s no /X11/anything under /usr/share/. I’ve been banging away with instructions from helpful people that either can’t be followed, or they have no effect when they can be followed.
    My screen is the official Raspberry Pi 7″ touch screen, connected via ribbon cable to the pi 3B+ monitor port, and powered off the pi as well. I just upgraded my OpenAuto installation with a fresh install of OpenAuto Pro 12.0. OS is NAME=”Raspbian GNU/Linux”
    VERSION_ID=”10″
    VERSION=”10 (buster)”
    VERSION_CODENAME=buster
    Any help would be appreciated.

    • Roger, that’s an interesting problem. I’ve got the Pi4, so that might have different hardware interfaces. A lot of working with any Linux setup is making sure you’ve got the exact same setup as the myriad blog posts. Are you putting the lcd_rotate=2 into /boot/config.txt ? The /X11 settings are just to change the orientation of the touch-interface (which is different from the visual display).

  • Hello M_BEY !
    Thank you so much, It works perfectly, I’m finally able to rotate screen on my pi with touch working!

    The best solution đŸ™‚

Leave a Reply

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