Using an Xbox 360 Wireless Controller with Raspberry Pi

As part of a project I’m working on at the moment (more information to come soon… more information here) I’ve been attempting to get my Xbox 360 Wireless Controller for Windows talking to my Raspberry Pi. Having spent a fair amount of time chasing various options around the internet, I thought I would share my eventual (and rather simple) solution here.

The first thing I found was PyGame – a python library that offers support for joysticks and gamepads, but primarily designed for game development. This post suggested that PyGame may solve the connectivity problems, and gives some example code for echoing out events, however I could not get this to work.

The Ubuntu wiki suggested a module called xpad, which is included by default on Ubuntu, but not on the Rasbian image I am using (Rasbian “wheezy”), although it is available through apt-get in the default repository. Unfortunately, this didn’t work for me either.

The eventual solution that worked for me came up in a blog post by Zephod about using an Xbox controller to run a remote control car, which suggested using Xboxdrv – a userspace driver for the Xbox controllers in Linux. There were suggestions on the Raspberry Pi forums that this would require building, but a simple `apt-get install xboxdrv` on the Pi worked for me. Once installed, execute the program (as root), and then re-sync the Xbox controller – this had me stumped for quite a while, and seemingly only needs to be done the first time you attempt to use the controller since the module was loaded. A re-sync for the wireless controller means holding the button on the reciever for ~3 seconds until it starts to flash, and then holding the button on top of the controller (to the right of where ‘Microsoft’ is written) until the lights flash. Once this has completed, you should see a new line in stdout for every event that happens on the controller – so press a button and see what happens!

This is the output I saw when starting up xboxdrv (having already done the sync) and pressing the “A” button on my controller – notice the A:1 changing to A:0 as I release the button (about 3/4 of the way across the terminal). Success! (Click for a larger image)

Zephod has written a small Python class to read from the output of xboxdrv and allow it to be read in a more usable format – I’ve not yet had chance to fully digest what it does and how it works (this is an exercise in me learning Python as well!), but it looks very promising, and I’m looking forward to continuing with my project!

Update 05/01/2013
I’ve had chance to play around with the xbox_read.py file I linked above – and it works a treat! I’ve set up a quick test python script that you can use to print out events. In order to use it, do:

$ git clone https://github.com/zephod/lego-pi.git legopi
$ touch legopi/__init__.py
$ wget http://projects.mattdyson.org/projects/robotarm/readEventTest.py
$ sudo python readEventTest.py

You may have to re-sync your controller as described above, and then once you start to move sticks/press buttons you should see a single line for each event! Magic!

13 thoughts on “Using an Xbox 360 Wireless Controller with Raspberry Pi

  1. This is right down the alley of where I want to go, but I have only gone a few lines before getting an error message. First, “apt-get install xboxdrv” tells me it is unable to lock the administrative directory. The, I try putting sudo in front of that, and I get two 404 errors. They are from mirrordirector.raspbian.org. Any idea what is wrong?

    • Ed, you have to be using the Root Terminal for apt-get install xboxdrv to work. Are you in Root Terminal?

    • As Darren says, you need to be root to run this, so the `sudo` prefix will be necessary.

      If you’re getting 404’s from mirrordirector.raspbian.org, I would suggest running `sudo apt-get update` first – I’ve had this problem recently as well.

  2. The idea of using my XBOX 360’s wireless controller with the Raspberry Pi is interesting, specially with XBMC. It would definitely be a superior solution to using a wireless keyboard.

    But every article I’ve read about this only discusses this, only tells about the software side – the drivers. What about the hardware? What device on the pi connects to the Xbox controller?

    Am I missing something?

  3. Pingback: 라즈베리파이 활용 강좌 : 라즈베리파이 XBOX360 WIRELESS 사용하기 | Raspberry Pi Village

  4. Great that you got it working in the end.

    Is it possible to use two for more wireless controllers in this configuration on the Raspberry Pi?

    • Thanks david! I only own the one controller, so have no idea if you could use this same method to monitor two or more controllers – let me know if you get it working!

  5. Hey. great work! I have your testscript up and running. What i wanna do is to trigger code by Buttonpress in Python. I’m not quite familiar with Python. What I’m thinking about is: if event(BTTN_A==1) then do it. Can you give me a hint? I want to turn the page of my kindle via ssh with my xbox Controller.

  6. Really interesting stuff. I trying to use python so that for example when I push forward on my analog stick it starts up a motor? I can’t figure out how to alter this python test event script to actually do something when you press the button rather than just print which button you pressed. Any help?

    • ignore this comment after reading previous comments i figured it out. thanks to you my simple raspberry pibot might live to work another day!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>