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

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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

rotate_screen=0

# Rotate 90° Clockwise

rotate_screen=3

# Rotate 180°

rotate_screen=2

# Rotate 270° Clockwise

rotate_screen=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!

Be the First to Share

    Recommendations

    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest

    Discussions