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!
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?
were is the 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.
This is kinda useful, but here’s the thing:
1. I’m using RetroPie.
2. I want to connect 2 controllers; an Xbox 360 controller (wireless) and an SNES controller.
Is there a way to do that?
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?
Hi Rajesh. What I’m referring to in this post is the xbox 360 wireless controller for windows (http://www.amazon.co.uk/Xbox-360-Wireless-Controller-Windows/dp/B000H7FCB8/ref=sr_1_5?ie=UTF8&qid=1357254331&sr=8-5), which comes with a USB receiver, which can be connected to the Pi.
Pingback: 라즈베리파이 활용 강좌 : 라즈베리파이 XBOX360 WIRELESS 사용하기 | Raspberry Pi Village
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!
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.
Hi Ben,
Thanks! Take a look at the project I used this for – the Robot Arm (http://mattdyson.org/projects/robotarm/), you should be able to modify that code to make Python call an external command by using something like this – http://stackoverflow.com/questions/89228/calling-an-external-command-in-python
Good luck!
Thanks a lot, Exactly what i was looking for. Fuck GPIO Buttons, XBOX Controller roxXx!
Btw. I’ll soon be able to pageturn the kindle with my guitar hero drumpad :-D……. to go over the top
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!
How did you get it to work im working with a Raspirobot Board to make a Pi-bot. I couldn’t figure it out. Could you help me?
I had a question for an Xbox 360 wireless controller with the attached keyboard. Will they both still work (wired & wireless)? How is the key mapping for this? I figured since it is small, rechargeable, wireless, and fairly well designed to compliment a Raspberry Pi, it may free up and additional port, while having reasonable functionality. I’m also doubtful, but could the headset work too as a robust microphone for say, Skype or similar applications?
After installing the xbox wireless controller the controller doesn’t connect on startup.
I can connect the wireless xbox controllers by pressing F4 and quit the controller setup.
so I give two commands:
– sudo rmmod xpad
– sudo xboxdrv –detach-kernel-driver
I have two questions left
1) how can I get back to the screen where I can setup the controller?
2) how can I run this two commands on startup?
Thanks
I’m a couple of steps futher.
I put the two commands in /etc/init.d/rc.local
– sudo rmmod xpad
– sudo xboxdrv –detach-kernel-driver –silient & sleep 1
Now the controller works on the commandline, works in the emulationstation but not on the emulator (f.e. Nintendo).
Pingback: Controller Simulation | DIY BB-8
Hi, thanks for the article! I saw that above you posted a link to Amazon for a $60 item. If I have a USB Charging cable for a Wired Controller, can I plug that directly into the USB port on the Pi? 1) Will it cause power problems, and hurt my Pi, and 2) Will it serve as a connection that the Xboxdrv can detect? Thank you for the help. I am attempting a PiBot build, but saw this as the easiest method of controlling it.
Hi,
According to this StackExchange question the USB charging kit doesn’t transmit any data, so it won’t be possible to just use this with a wireless controller. However, if you have a wired controller then it should work fine with xboxdrv. Good luck!
Hi, Just a quick question how do I get back to the matt@venus: after running xboxdrv? Reason why I am asking this after I run xboxdrv I want to use moonlight to stream games from my pc. I can’t use Ctrl C because that stops it completely? I am net to raspbian and any help would be greatly appreciated. I know it is probably a simple command but I have not been able to find it after a lot of searching online.
Great article, the thing I don’t understand is which component is the wireless receiver for the Xbox 360 controller… Where did you get it from ?
Hi Andy,
I’m using the wireless receiver for Windows – http://www.amazon.co.uk/Xbox-360-Wireless-Controller-Windows/dp/B000H7FCB8/ref=sr_1_5?ie=UTF8&qid=1357254331&sr=8-5
Hey,
You can use this for £5.00
http://www.amazon.co.uk/gp/product/B00G8SK4W2/?ref=as_li_tl&linkId=TLLVXTXWCIGHLAIS&camp=1634&creative=19450&ie=UTF8
Pingback: 5 – Utiliser une manette sans-fil de Xbox 360 – Piwi
Pingback: Hoe verbind ik een Xbox controller met de Raspberry Pi – Creative Technologies
Hey guys. I’m in the beginning stages of mapping out what I want to do with the raspberry pi 3. I’m planning on building it as a game emulator as a project with my son for Christmas.
My question is this, and I’d seen it asked but never answered.
Can you use multiple wireless Xbox 360 controllers? He has a 360 now with 5 wireless controllers. I realize I’d have to buy the adapter. My line of thinking was third could save some money and have the joysticks for games, etc.
If this wouldn’t work, what’s the best solution for series controllers and up to 4 at a time (thinking GoldenEye 007, etc).
Thanks for your input!
Pingback: Utiliser une manette sans fil de Xbox 360 – Projet Picon
Pingback: Utiliser une manette sans fil de Xbox 360 avec Python 3 – Projet Picon