Interfacing with a USB Controller


In this section, we will discuss how to interface with a generic USB game controller and communicate with the Raspberry Pi on the robot. All code is written in Python 3 or higher. The scripts work by passing raw controller values from the client computer to the server (Raspberry Pi). The Raspberry Pi then interprets these values and controls the servos, interfacing through a separate python library

How To:


In order to start this task, you must have:

  • A functioning wireless access point on the Raspberry Pi
  • The following installed on the host computer

Materials needed

  • Generic USB Game Controller
  • Host computer
  • Robot (or at least its Raspberry Pi)

Basic Steps

  1. Power up the Raspberry Pi
  2. Connect the host computer to the Pi’s network
  3. Connect the game controller to the host computer



Green Flag

  1. Put in the Raspberry Pi’s /home directory and run it with IDLE.
  2. Put in the same folder as
  3. Start on the server computer (ensure that the steps above are followed)
  4. If the process has succeeded, the server computer’s IP address will appear in the output of
  5. The robot will start responding to joystick inputs

Orange Flag

Ensure the robot is working by following the Basic Steps listed above. Many features of the robot can be configured, if needed, follow the steps below for the modifications you want to make.

Change travel speed

speedMultiplier_rightStick is a multiplier for the joystick input. By default, this value is set to 100.0

Change arm servo speed

cameraSpeed keeps track of the travel speed for all arm servos. Change this to control the speed of the arm servos. Avoid changing speedMultiplier_leftStick or speedMultiplier_hat, as these are smoothing values. Changing these may result in shakier camera movements or extremely small movements.

Changing communication port

Change the TCP_PORT value in both and

Change button functions

buttonScripts maps all buttons to functions to execute when a button press is received. Replace printButton with the function that you want to be called when the button is pushed. The comments denote the button each element is connected to.


Red Flag

  • Establish a connection between the server and the robot
  • Send joystick positions from the server to the robot
  • Act on joystick input
  • (Optional) Robot sends operational data back to server (wheel rotation rate, arm position, servo modes, etc.)
Interfacing with a USB Controller
Tagged on: