# Rotate Raspberry Pi Display and Touchscreen

42,330

19

19

## Introduction: Rotate Raspberry Pi Display and Touchscreen

This is a basic Instructable to show you how to rotate the display and touchscreen input for any Raspberry Pi running the Buster Raspbian operating system, but I've used this method ever since Jessie. The images used in this are from a Raspberry Pi 3 B+ running Raspbian Buster with a 3.5" TFT LCD touchscreen.

The touchscreen used is fantastic, if you want one you can find it at this link from amazon:

## Step 1: Rotating the Display

The raspberry pi display is very easy to rotate because there is an option you can put in /boot/config.txt that allows you to rotate the screen with one line.

To rotate simply open your terminal (ctrl + alt + t) and then type "sudo nano /boot/config.txt"

Go to the bottom of the file and type in what you need to rotate your screen to how you want it:

# Default Orientation

display_rotate=0

# Rotate 90° Clockwise

display_rotate=3

# Rotate 180°

display_rotate=2

# Rotate 270° Clockwise

display_rotate=1

## Step 2: Why the Touchscreen Needs Rotating

The touchscreen is a little more complicated, it relies on a matrix to take inputs and map them to a new position. This is done with a 3 dimensional transformation matrix which are very common in robotics and space physics to describe the motion of an object in a 3D space. You may be thinking why does my 2D cursor need a 3D matrix? But your cursor actually has a third dimension that is unused. See the math below:

By default the matrix is set to and identity matrix, meaning a one-to-one mapping: (The dots are placeholders to help line stuff up, imagine they are not there, Inscrutables removes the spaces)

......| 1 0 0 |

I = | 0 1 0 |

......| 0 0 1 |

When this matrix is multiplied by the input vector given by your touchscreen this is what happens:

| 1 0 0 |....| 300 |.....| 300 |

| 0 1 0 | * | 200 | = | 200 |

| 0 0 1 |.......| 1 |..........| 1 |

As you see above, the identity matrix does not effect the output. Now the purpose of this instructable is not to teach you matrix multiplication, but if you are interested there is plenty of tutorials online. I will be showing the math side of this just so you can see the proof of how and why this is happening.

If we wanted to rotate the touschscreen 90° (clockwise) then we would use this matrix:

| 0 -1 1 |...| 300 |....|-200 |

| 1 0 0 | * | 200 | = | 300 |

| 0 0 1 |........| 1 |.........| 1 |

So as you see the x and y values have now switched but the new x value is also negative. It is a little difficult to visualize, so look at my example in the pictures. A line is traced from center to the right, now when it is rotated 90° (clockwise), you notice the traced line goes from center -> right (+x) to center -> down (-y) and this is why the input vector needs to be changed as such. The rest of the rotation matrices are listed in the next step but now you know a little more about what is going on!

## Step 3: Rotating the Touchscreen

Go to your terminal again and type in "cd /usr/share/X11/xorg.conf.d/", if your touchscreen is at least detecting touches then the configuration file should be in here.

Type "ls" to list the current files, your calibration file should be in there, if you don't know which one is yours open each one (Using "nano your_file_name") and find one that has a section that has "Identifier ... touchscreen catchall". Most likely it will either be one that has "evdev" or "libinput" in the title. Once you've found it do "sudo nano your_file_name" to get write access and edit the file.

Go to your section and add the correct "Option" the the bottom on the "Section".

All are with clockwise perspective:

90° = Option "TransformationMatrix" "0 -1 1 1 0 0 0 0 1"

180° = Option "TransformationMatrix" "-1 0 1 0 -1 1 0 0 1"

270° = Option "TransformationMatrix" "0 1 0 -1 0 1 0 0 1"

## Step 4: That's It!

Hopefully this helps plenty of starting Raspberry Pi enthusiasts! I see people struggling with this issue all the time so if you happen to run across someone in a forum who needs help just send them a link to here. Happy inventing my friends!

## Recommendations

28 3.9K
164 14K
1 33 3.2K
79 7.9K

• ### Big and Small Contest

Hello
I own a Raspberry PI 2 with Raspbian GNU/Linux 11 (bullseye) newly installed.
lcd_rotate=2 in /boot/config.txt works only partially .
At boot, the screen is OK, but about in the middle of boot sequence it goes upside down again, and the desktop is upside down too.
Where can I find a hint ?

In the xinput list my touchscreen shows 2 devices ‘ILITEK Multi-Touch-V3000’ and ‘ILITEK Multi-Touch-V3000 Mouse’. In the 40-libinput.conf file I have added the TransformationMatrix to the touchscreen catchall and to specific MatchProduct inputClass sections. After reboot only the ‘Mouse’ xinput has new the MatrixTransform in the xinput properties. I can do a xinput set-prop ‘ILITEK Multi-Touch-V3000’ 136 0 1 0 -1 0 1 0 0 1 from the command line and the touch works properly. I just can’t get it set right from the 40-libinput.conf file. Any suggestions???

I'm trying to use a square 720*720 touchscreen. When I changed the resolution for the display, would I also need to change the resolution for the touch input too? I can't seem to get it working :(

Is it possible to adjust the sensitivity of the touch screen by changing the z-axis value? if so could you please assist me on that?

I was able to rotate the display 180 degrees by adding the line "display_lcd_rotate=2" to the config.txt file, but now the touch interface still responds as if it isn't rotated. I tried the above lines adding to the "libinput" file as mentioned above with the lines Option "TransformationMatrix" "-1 0 1 0 -1 1 0 0 1" but it didn't work. I am using the newest version of Raspian on a Rpi 3B+, and an official 7" screen. Any help will be appreciated.

I figured it out...see guide here: https://www.raspberrypi.org/documentation/hardware...
I first added the line to /boot/config.txt "display_rotate=2", and then the line "dtoverlay=rpi-ft5406,touchscreen-inverted-x=1,touchscreen-inverted-y=1"
and that fixed both the orientation of the screen, and the touchscreen orientation!

I have a RaspberryPi 3B+ and just bought the official 7" LCD touchscreen. I used the latest build of Raspian, and of course, the display was upside down. So I searched online, and tried adding "lcd_rotate=2" in the file /boot/config.txt, but when I reboot, my screen was a bunch of vertical lines. So I found that if I used "display_lcd_rotate=2" did rotate the screen 180 degrees, but the touchscreen interface did not rotate. I have not found any solution online to rotate both together. Why is this so difficult? It's hard for me to believe that there isn't a more elegant solution to this! Any help would be appreciated, as long as it doesn't involve adding "lcd_rotate=2" to the config.txt file, because, as I said before, that kills the display, leaving a series of lines, no interface!

Thank you! The touch part worked for me!

Hi,

Thank you for the information. With my Rasberry Pi 4B, I tested a tweak to from this article: https://jlk.fjfi.cvut.cz/arch/manpages/man/libinput.4 with ctrl-f Option "CalibrationMatrix". In contrast to the option of the "TransformationMatrix" listed in the Instructables article, I used "CalibrationMatrix" instead. Then, I did sudo reboot (restarted device) in CLI.

When I booted up the Raspbian OS, I touched the screen and it worked: cursor goes to where I tapped. If I want to use a mouse (for redundancy), I sometimes I click my mouse to then use it. Later, I touch the screen to use touchscreen.

Thx a lot! Worked, gave me the right hints, thumbs up!

Just some remarks (that may provide some search terms for futher investigation):

Pi 4 can not only display-rotate, but also lcd-rotate, meant for official touchscreen, haven't checked, may be it does the touch translation right away. I did my screen rotation with 'Screen Configuration' from configuration menu, which is working on the fly with no reboot and stores this setting, so reset will not leave you with an unrotated screen

The math behind the matrice is a 2D affine transformation matrices, check wikipedia (transformation matrice) for a verbose description. It can shift, scale, rotate, shear, mirror or any combination of these, realy powerful. The right most column in the matrice for this implementation multipies with the actual screen resolution (-1, as coordinates starts with 0) in the top down order of X, Y, Z.
E.G. for 270° matrice (first row -1 0 1), an touch x of 200 and a screenresolution of 1920 it will be
200 * -1 + whatEverY * 0 + (1920-1) * 1
= -200 + 0 + 1919 = 1719, i.e. exactly what we need

Rather than manipulate "Identifier ... touchscreen catchall" I followed an adapted procedure as given at raspberrypi org /documentation/hardware/display/troubleshooting.md for the question 'How do I enable right click?'. As this is to do anyway I used that one to cover transformation as well. My screen has 1920x1080, 'xinput list' gave me my screen, its id and that its seen as pointer (so touchscreen wouldn't have worked anyway) and 'xinput list-props <#id>' gave me the name again and the device product as '10900, 20557'. After convert the figures to hex (2a94:504d), 'lsusb' offers a nameless device at one of the usb busses, at least it was recognised. With this I added a section as first entry in /usr/share/X11/xorg.conf.d/40-libinput.conf
>Identifier "calibration"
>>>Driver "evdev"
>>>MatchProduct "G2Touch Multi-Touch by G2TSP"
>>>Option "TransformationMatrix" "-1 0 1 0 -1 1 0 0 1"
>>>Option "EmulateThirdButton" "1"
>>>Option "EmulateThirdButtonTimeout" "750"
>>>Option "EmulateThirdButtonMoveThreshold" "30"
>EndSection
Now translation is limited to this screen with this input and does not interfere with other screens and devices.

Since 2 years I am looking for this solution to rotate the input of the touch screen.
Thank you very very much!

But one thing I still ask myself, how on earth do you come up with this solution! One more thing, Linux will never be my friend :-)

A lot of research and testing! It was a bit of time to create the guide so I'm glad it solved your 2 year long search!

DID NOT WORK FOR ME ON A PI4B RGB RAM. The desktop remained the same. I opened up all the files, 3 of them that had evdev and libinput and NODA. All my files I opened were blank, nothing in them, so I added your 180° = Option "TransformationMatrix" "-1 0 1 0 -1 1 0 0 1" to all three, did nothing!

Sorry for your difficulties, please note that user silverfox52z caught an important typo in my guide where it says "rotate_screen" it should say "display_rotate" for the new raspbian buster OS. I've updated the guide accordingly. If you still have issues I would reinstall raspbian if it still causes issues.

Thanks for this information. Until now, I have searched
several places without finding a solution.
One note. Possible due to upgrades, I found that none
of the command for rotating xxx degrees -
"rotate_screen=x" did not work.
In my case,
rotate_screen=2 did not work at all
But, upon examing the screen shots, I found the command
display_rotate=2
This word like a charm.
Thanks Again.
Bruce

Good catch, I think what I typed was an older raspbian command, I'll update the guide immediately.

Helloooo,
I searched for some kind of solution and this WORKS! On a raspberry pi 4!

Section "InputClass"
Identifier "libinput touchscreen catchall"
MatchIsTouchscreen "on"
MatchDevicePath "/dev/input/event*"
Driver "libinput"
#Touch rotate
Option "TransformationMatrix" "-1 0 1 0 -1 1 0 0 1"
EndSection