Integrating KVM Switching with PyQt and Arduino Leonardo

Posted by

KVM, which stands for keyboard, video, and mouse, switching is a technology that allows users to control multiple computers using a single set of input devices. This can be particularly useful for users who have multiple computers but do not want to have separate keyboards, monitors, and mice for each one. In this tutorial, we will explore how to build a KVM switching system using PyQt and an Arduino Leonardo.

To get started, you will need the following:

  • Arduino Leonardo
  • USB cables (to connect Arduino to computers)
  • PyQt library (if you don’t have it already installed, you can do so using pip install PyQt5)
  • Python (to run the PyQt code)
  • Two or more computers (to test the KVM switching)

Step 1: Setting up the Arduino Leonardo
First, you will need to set up the Arduino Leonardo as the controller for the KVM switching system. Connect the Arduino to your computer using a USB cable and open the Arduino IDE. Copy and paste the following code into the IDE:

#include <Keyboard.h>

void setup() {
  Keyboard.begin();
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(2) == LOW) {
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_SHIFT);
    Keyboard.write('s');
    Keyboard.releaseAll();
    delay(500);
  }

  if (digitalRead(3) == LOW) {
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_SHIFT);
    Keyboard.write('d');
    Keyboard.releaseAll();
    delay(500);
  }
}

This code sets up the Arduino Leonardo to receive inputs from two buttons connected to digital pins 2 and 3. When one button is pressed, it sends a keyboard shortcut to switch between computers (Ctrl + Shift + S for computer 1, Ctrl + Shift + D for computer 2).

Upload this code to the Arduino Leonardo using the upload button in the Arduino IDE.

Step 2: Creating the PyQt application
Next, we will create a PyQt application to control the KVM switching system from the computer. Create a new Python file and copy and paste the following code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt

def switch_to_computer1():
    # Code to switch to computer 1
    print("Switching to computer 1")

def switch_to_computer2():
    # Code to switch to computer 2
    print("Switching to computer 2")

app = QApplication(sys.argv)

window = QWidget()
window.setWindowTitle('KVM Switching')
window.setWindowIcon(QIcon('icon.png'))
window.setGeometry(100, 100, 300, 200)

btn1 = QPushButton('Computer 1', window)
btn1.move(50, 50)
btn1.clicked.connect(switch_to_computer1)

btn2 = QPushButton('Computer 2', window)
btn2.move(150, 50)
btn2.clicked.connect(switch_to_computer2)

window.show()

sys.exit(app.exec_())

This code creates a simple PyQt application with two buttons to switch between computers. When a button is clicked, it calls the respective function to switch to the desired computer.

Step 3: Testing the KVM switching system
Now, connect the Arduino Leonardo to the computers you want to control using USB cables. Press the buttons connected to pins 2 and 3 on the Arduino to test switching between computers.

Run the Python script with the PyQt code on one of the computers. You should see a window with two buttons to switch between computers. Clicking a button should trigger the Arduino to switch to the respective computer.

Congratulations! You have successfully created a KVM switching system using PyQt and an Arduino Leonardo. You can customize the PyQt application further to add more features or controls. Happy switching!