Rotate Raspberry Pi Display and Touchscreen

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:

https://www.amazon.com/Raspberry-320x480-Monitor-Raspbian-RetroPie/dp/B07N38B86S/ref=asc_df_B07N38B86S/?tag=hyprod-20&linkCode=df0&hvadid=312824707815&hvpos=1o19&hvnetw=g&hvrand=5789897662091576261&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=9027898&hvtargid=pla-667157280173&psc=1

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!

1 Person Made This Project!

Recommendations

  • The 1000th Contest

    The 1000th Contest
  • Battery Powered Contest

    Battery Powered Contest
  • Hand Tools Only Challenge

    Hand Tools Only Challenge

11 Discussions

0
delgadosouza
delgadosouza

4 weeks ago

Thank you! The touch part worked for me!

0
n.volk
n.volk

3 months ago

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.

0
grzbrz
grzbrz

4 months ago

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.

0
kaeseralex
kaeseralex

8 months ago

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 :-)

0
GraysonL3
GraysonL3

Reply 5 months ago

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!

0
DeanD12
DeanD12

8 months ago on Step 4

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!

0
GraysonL3
GraysonL3

Reply 5 months ago

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.

1
silverfox52z
silverfox52z

7 months ago

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

1
GraysonL3
GraysonL3

Reply 5 months ago

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

0
tristan.kaupa
tristan.kaupa

6 months ago on Step 3

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