Desktop Display Auto Rotate

by danionescu in Circuits > Arduino

8394 Views, 14 Favorites, 0 Comments

Desktop Display Auto Rotate

Display auto rotate with gyroscope and python
2016-07-16 17.53.28.jpg
2016-07-16 17.54.14.jpg

My home and work displays have an interesting feature: they can tilt left or right with 90 degreds, but the image remains orientated in the same direction. One day an work colleague asked me about the display, i flipped it and then we both looked in the settings to flip the image too, it took us a few minutes to find the setting.

So i came up with the idea to automate this. I had some experience with electronics and i thought it was an interesting project to make and share :)

For this project you will need only a few components and some basic knowledge about programming and how to upload code to a microcontroller.

There are some displays on the market that have this feature built in, but i never came across one so this tutorial is for those of you who don't have one of those displays that automatically send orientation information through HDMI.

Limitations:

First of all, i've tested the device on ubuntu 16, for other linux based systems there might be variations, i'll try to cover that in the last chapter.

For windows and Mac OS systems this will not work out of the box, you'll need to find and replace in my code the os command that flips the display.

How does it work:

The breadboard with the gyroscope, arduino and bluetooth will be sticked to the display back side. Te arduino will read from the gyroscope the "Y" position and if the incline will be greater than 45 degreds either left or right then it will send a signal through bluetooth to the computer to tilt the screen. The bluetooth is optional here, it can communicate with the computer with the USB cable on the serial port.

On the computer we'll run a little python program in the background. The program will listen on the bluetooth device or serial port (is configurable) and when the tilt message is received a command is run to tilt the display.

Things Required

2016-07-02 18.04.57.jpg

Parts:

Warning: The prices might vary a bit, those wore the prices available when i've created this instrucatable

1. arduino nano (eBay) 2.6 $

2. HC-05 bluetooth module (eBay) 3.8$ (optional if you want the connection over bluetooth)

3. bluetooth dongle (eBay) 2.5 (optional if you want the connection over bluetooth and you computer doesn't have one embeded)

3. MPU6050 gyroscope (eBay) 1.72

4. Breadboard (eBay) 1.5

5. male-male breadboard jumper cables (eBay) 2.7

6. small USB power supply (optional if we don't power through the computer USB)

7. double sticky tape to hold the breadboard

Tools:

USB to serial FTDI adapter FT232RL to programm the arduino pro mini

Skills:

Basic arduino programming, this tutorial might be useful

Assemble the Device

schematics.jpg
2016-07-02 18.06.04.jpg
2016-07-03 11.12.33.jpg

I've attached the fritzig picture and schematics above.

The steps are:

1. Put the arduino nano, gyro and the bluetooth on the breadboard

2. Connect the arduino nano vin to the breadboard "+" (red) and the gnd to the breadboard "-" (black)

3. Connect the + from the breadboard red line to the HC-05 positive pin, and to the mpu6050 positive pin

4. Connect the - from the breadboard black line to the HC-05 negative pin, and to the mpu6050 negative pin

5. Connect the D12 from the nano to the HC-05 tx

6. Connect the nano A4 to the MPU6050 SDA pin, and A5 to the SCL pin

7. Plug the mini usb cable into the nano

After you will upload the code in the next step, the device will be sticked to the display backside using double sticky tape or simple sticky tape

Downloads

The Code

First the arduino code:

You will need two external libs I2CDev and MPU6050, i've attached them here, and i've posted below the source of the code.
If you don't know how to install a library check this instructable:

Then copy paste or download my library and give it a try.

I2CDev library source: https://github.com/jrowberg/i2cdevlib

A few words about configuration:

- the trigger angle "const int SWITCH_ANGLE = 45", is the angle that will trigger the screen to flip

- choosing the gyroscope useful axys:

"if (isAngleChanged(z)) {

transmitNewRotation(z);

}"

Is the axys that we're interested in, gyroscopes track all 3 axys, but for monitoring the display tilt we'll need only one. It depends on the gyroscope position on the breadboard and the breadboard position on the display. To calibrate this the simple way, upload my code, mount the breadboard on the display, and using the serial print command, print all axys. Then tilt the display left or right and see which axys change. Then you will need to replace in the code above the "z" variable with the corresponding axys variable.

The python code:

The code is written in python 2.x, also you will need to install a serial library, you con download it from here.

I've attached a zip file displayFlip.zip containing the python code. The next step will include information about how to use it :)

For other operation systems rather than ubuntu you will need to find out how to call from command line the command responsable for rotating the display and then replace it in my code. In "run.py" you will need to replace the 3 commands "os.system("xrandr --output HDMI1 --rotate right")" ,"os.system("xrandr --output HDMI1 --rotate right")" and "os.system("xrandr --output HDMI1 --rotate normal")" with the corresponding commands for your system.

Using the Device, Further Improvements

2016-07-04 18.34.23.jpg

So, by now we have:

- the breadboard with the parts assembled

- the code uploaded to the arduino nano

- breadboard "sticked" to the back of the display

- arduino code calibrated (the display angle if needed, the axys if needed)

- the python code downloaded

Now we have two type of usages:

1. bluetooth usage:

In order for bluetooth communication to work, you will have to pair the bluetooth to the HC-05 or HC-06 module. The pairing is done in the operating system.

- for windows you can check this tutorial here

- for unix systems it's a bit more complicated, you can check this tutorial

There are many versions of unix out there, and the process might be different, you might need to check other resources about pairing bluetooth serial devices.

Next, unzip the python file, navigate to the folder and run the command after pairing:

python run.py bluetooth xx:xx:xx:xx:xx:xx reverse(where xx:xx:xx:xx:xx:xx is the bluetooth address)

The last argument "reverse" can be 0 or 1, if you notice that you tilt the display right and the image will be tilted left then you need to close the program and run it again with 1 instead of 0 or the other way around.

2. Serial usage:

For the serial usage we need to plug the arduino nano directly into a computer USB port, you'l probably need an usb male to female adapter for this.

Next you will need to determine the USB serial interface:

- in linux you can do a ls -l ls -l /sys/bus/usb-serial/devices and you'll get an output similar to this:

ttyUSB0 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/ttyUSB0 so our interface will be /dev/ttyUSB0

- in windows you can check this tutorial to check which com port is in use

Unzip the python file, navigate to the folder and run the command:

python run.py serial usb_serial_interface reverse;

- the "usb_serial_interface" will be something like COM3 for windows, or /dev/ttyUSB0 for linux

- the last argument "reverse" can be 0 or 1, if you notice that you tilt the display right and the image will be tilted left then you need to close the program and run it again with 1 instead of 0 or the other way around.

Thoughts and further improvements:

Only the second approach with the serial cable is more cross platform, the bluetooth pairing is more difficult in unix systems. An improvement here might be to pair two bluetooth serial devices (one master one slave) and send data directly between them. The other bluetooth device will be connected to the computer by a USB to TTL adapter so you will ending up using the python tool in serial mode.

You have noticed that we have build this project on a breadboard, you can reduce it's size and improve it's aspect if you'll enclose it in a box, and solder the components on a PCB, but this was just a demo project, i'll leave those aspects to you.

I will try to answer all question in the comments, and i hope this will be a fun project, and maybe a useful one.

You can check out my other tutorial about MPU6050, it's about dimming led's by tilting the angle of the breadboard. Check it out here