Control your Raspberry Pi by using a wireless Xbox 360 controller

9. May 2017
1699 Views
9 Comments

In addition to the control of the Raspberry Pi via infrared remote controls, 433 Mhz transmitter, messengers and many more, also a wireless Xbox 360 controller can be read from the Raspberry Pi. This allows the gamepad to be used as an external mouse (in desktop mode) or in scripts and thus the GPIOs can be controlled. In RetroPie, the Xbox Controller can also be used as input media for games on the Raspberry Pi.

In this tutorial several of these possibilities are shown. From the installation of the required software, to the use as a mouse, to the switching of the GPIOs and the control of a servo motor with the joystick.

 

Accessories

To use the Raspberry Pi with the Xbox 360 controller, you do not need a lot of accessories:

  • Xbox 360 Wireless Controller (US / UK)
  • Xbox 360 USB Receiver (US / UK)

Each Raspberry Pi with at least one free USB port can be used, for example, the more powerful Raspberry Pi 3.

If you want to rebuild the small project below, you also need:

 

 

Raspberry Pi Software for the Xbox 360 Controller

For Linux distributions, there is a developed driver, especially for communicating with (wireless) Xbox controllers.

We first install the driver so that it can be communicated with the controller:

sudo apt-get install xboxdrv

Now the USB receiver can be connected. With lsusb, you can check if it has been detected:

pi@raspberrypi:~ $ lsusb
Bus 001 Device 005: ID 045e:0291 Microsoft Corp. Xbox 360 Wireless Receiver for Windows
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

 

Now you can turn the controller on and test if it is detected. To do this, start the driver using:

sudo xboxdrv --detach-kernel-driver

After that, press the keys to change the log, which indicates which key(s) is/are pressed and its value.

On the documentation page, there are all commands with a brief explanation. For example, it is possible to make the LEDs of the Xbox 360 controller light / flash (--led NUM)  or vibrate (-r 255,255). In total, up to 4 radio controllers can be used and addressed or read out.

Use the Xbox 360 Controller as Raspberry Pi Mouse

A feature I particularly like is the mouse option. Since I have rarely connected a keyboard to the Raspberry Pi (I use almost exclusively SSH and sometimes the Remotedesktop) and even more rarely a mouse, I find it very handy to use the Xbox 360 wireless controller as a mouse for the Raspberry Pi. Everything we need is already installed.

Only one other parameter must be specified:

sudo xboxdrv --detach-kernel-driver --silent --mouse

You can also change the speed and selection of the buttons (as described in the documentation). If the command should be executed at system start, you can either write an autostart script or use crontab.

The following (standard) assignment of the keys for use as a mouse applies:

  • A: Left click
  • B: Right click
  • X: Middle mouse click
  • Y: Enter
  • Left Joystick: Mouse movement
  • Right Joystick: Scroll wheel
  • D-Pad: Arrow keys
  • Start: Forward
  • Back: Back
  • LB: Page up
  • RB: Page down

 

 

Use the Xbox 360 Controller to start scripts and commands

One way to use the driver in your own scripts is the manual readout of the output values. In order to save us this effort, we can go back to an already created Python library.

git clone https://github.com/FRC4564/Xbox
cd Xbox

Attached is also an example (sample.py), which can be viewed if necessary.

I have therefore built a small example with a servo motor and 4 LEDs as inspiration. The wiring is quite simple (resistance to the LEDs), therefore only shown here as a schematic structure:

Raspberrya Pi Xbox 360 Steckplatine

 

The code for this is as follows (create a file with sudo nano xbox360_example.py and with CTRL + O, X save and exit):

Then you can run the code (sudo nano xbox360_example.py) and use the buttons to turn the LEDs on and off or use the left joystick to control the servo motor. To stop, press the BACK button.

In the following video you can also watch this small project:

In addition, many more things can be done with the Raspberry Pi and Xbox 360 Controller. For example, a robot control with the help of the joystick and changing the modes with the buttons.

Well, what will be your next scheduled projects with the controller? 🙂

9 Responses

  1. I am trying to make a robot, using the R-stick as a 2 axis servo controlled fpv camera; does the pi detect directional input from the joysticks?

    Reply
    • On my german site, I have actually wrote a tutorial on how you can control a self-made Robot with the Xbox360 controller. There is also a youtube video linked. If there is enough interest, I can translate them as well.

      Reply
  2. You might want to take a look at the python library I wrote to handle joysticks for raspberry pi robots, it’s documented at http://approxeng.github.io/approxeng.input – I haven’t added support for the 360 but it does handle the newer xb1 with Bluetooth along with a few others. Writing an extension for the 360 would be easy and helpful!

    Reply
      • It would, want to add it? 🙂 I can only add support for controllers I own, Keith Ellis has added a couple of ones I don’t have and said it was reasonably easy to do.

        There’s a section in the docs on how to add new controllers, I can help out with any bits that aren’t obvious via twitter (@approx_eng) and would obviously give credit for any code you can contribute. Basically you just need to know the event codes and ranges for the axes and buttons, then copy one of the existing controller classes (i.e. https://approxeng.github.io/approxeng.input/_modules/approxeng/input/xboxone.html#WirelessXBoxOneSPad). You’d want to get the code from github first.

Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!