Home > Arduino, TKJ Electronics, USB > Xbox 360 controller support added to the USB Host Library

Xbox 360 controller support added to the USB Host Library

A wireless Xbox 360 controller is now also supported via a Wireless receiver. For more information see the blog post.

Hi everyone. I just wanted to let you know, that I just added support for the Xbox 360 controller via USB to the USB Host Library. The code can as always be found at github: https://github.com/TKJElectronics/USB_Host_Shield_2.0.

Categories: Arduino, TKJ Electronics, USB Tags:
  1. January 16th, 2013 at 02:07 | #1

    Thanks Lauszus. Usb.Task(); gets run every time the loop starts. Actually, the loop starts and I run a function called serialListen (it listens for data comin in on the serial port, like acceleration info coming from the adxl from another UNO, via xbee). If an instruction comes in to rumble the controller, then scancontroller fires the delegate() function which will then fire the rumble. Then I run scancontroller(), a function that has at the top of it Usb.Task(); scancontroller checks the values of some of the sticks and buttons.

    My code is very rough and is on github. I just started this project, I hope you don’t mind too much a thicket of code.

    It can be seen here: https://github.com/blairkelly/stackedcontrol/blob/master/stackedcontrol.ino

    I was thinking I might try this with the ps3 controller later. See if it makes any difference.

    P.s. That sketch will also turn on and off rumble if you push the Xbox button. I have found this will also cause the problem of controller reset.

  2. January 16th, 2013 at 15:16 | #2

    @Blair Kelly
    I think I found your problem. You are calling this: https://github.com/blairkelly/stackedcontrol/blob/master/stackedcontrol.ino#L113 without checking if the Xbox controller is actually connected.

    Also try to add a 1ms delay in your loop: https://github.com/blairkelly/stackedcontrol/blob/master/stackedcontrol.ino#L312.

    Try to run Usb.Task() before you run serialListen(), as you are actually sending a command to the controller before you have run Usb.Task, as you execute scancontroller() after serialListen().

    I found that it’s never a good idea to send multiple commands to the controller without calling Usb.Task in between. The problem is that you are not polling the input endpoint at the same rate as you are sending data.

  3. January 16th, 2013 at 16:42 | #3

    I am astonished at how much you help everyone out. Thank you very much for taking the time to analyse my code. I will try your suggestions this evening and let you know how I make out.

  4. January 17th, 2013 at 06:59 | #4

    I’m a bit embarrassed to say I was starting to run out of ideas when I just happened to plug in an external power source to the Arduino Mega 2560. It started working properly. I had ruled-out that possibility because it was working just fine with your example sketch. Perhaps the adequate power supply keeps the processes and communications from tripping up. Anyway, I thank you again for the time and effort you put into this and I’m sorry it was such a silly little thing I should not have overlooked!

  5. January 17th, 2013 at 18:24 | #5

    @Blair Kelly
    No problem :) Don’t hesitate to contact me again!

  6. Jerry
    April 14th, 2013 at 21:14 | #6

    Hi Lauszus,

    Thanks for all of the work you’ve put in getting the game controllers working with the USB Host Shield.

    I’ve been assigning the button presses to the digital I/O pins, and using the output to a relay board. Everything was working fine until I tried using the non-analog buttons. The comments in the XBOXRECV.h file starting at line 108 is a bit confusing and I’m not sure if what I’m trying to do is even possible.

    Whenever I try to getButtonPress for a non-analog button (L1, L2, X, Y, A, B), I get no outputs. getButtonClick works obviously, but I’d like for a constant output while the button is pressed down. Is this possible with these types of button?

    To clarify, I’ve been using an if/else function with digitalWrite to output low if the getButtonPress is true, else output high if the getButtonPress is false….using your example as a starting point. I’m new to all of this (you addind support for these controllers is really what brought me to the arduino), so if theres a better way of doing it, I’d appreciate it.


  7. April 15th, 2013 at 17:38 | #7

    It was caused by a bug in the getButtonPress in the function, but has now been fixed: https://github.com/felis/USB_Host_Shield_2.0/commit/78cb524bcf8e78b65205b0c2d80f73748a998712.
    Thanks for your feedback!

  8. Jerry
    April 15th, 2013 at 18:38 | #8


    Awesome! Thank you so much! Everything works perfectly now.

    So it was just an addition of a bool command? I admit I tried troubleshooting it myself before I posted, but the .cpp file is well beyond my comprehension at the moment. Thanks again!

  9. April 15th, 2013 at 18:40 | #9

    Yes I only added a bool cast. The problem was that it’s was trying to return a uint32_t, but the function is only a uint8_t type, so the top 24 bits where actually thrown away.

  10. November 27th, 2013 at 09:20 | #10

    Hi, great job and thanks. We are using your library for controlling our robots ! We discovered two problems :
    - objects USB , XBOXUSB and XBOXRECV must be declared in global in the main file, otherwise, the usb.task reset the arduino.
    - the first time a button is pressed, it is not detected.

    Any idea or help ?

  11. November 27th, 2013 at 16:13 | #11

    Hmm that sounds strange. Are you checking if Usb is not null before calling Usb.Task()?

    Can you post your code somewhere, then I will have a look.

  12. Christoph
    April 9th, 2014 at 00:02 | #12

    Hi Kristian,

    thanks for your library and your hard work. It’s really great. Is it possible to use multiple wired xbox360 controllers if i connect them via an usb hub? Naively I would expect this works if I create multiple XBOXUSB objects. If this is not possible right now, could you give me a hint, how i could make this work?

  13. April 9th, 2014 at 00:06 | #13

    Yes. Simply include the hub library and create the instance like so:

    #include <usbhub.h>

    USBHub Hub1(&Usb);

    After that just go crazy with those XBOXUSB instances ;)

    But please be aware that there might be several hub chips inside a usb hub, so if you need to connect a lot of devices you properly have to create another hub instance:

    USBHub Hub2(&Usb);
  14. taylor
    February 24th, 2015 at 15:36 | #14

    hi i am trying to make a rov submersible and i need help. I want to control it with a wired xbox controller but i have’int found any code that has output all i can find is code that uses a docgen but i would like to be able output pins on the arduino.

    can some one help?

  15. February 24th, 2015 at 19:32 | #15

    Have you seen the examples: https://github.com/felis/USB_Host_Shield_2.0/tree/master/examples/Xbox? You can just use digitalWrite to set a specific pin if a button is pressed etc.

Comment pages
1 2 2767
  1. December 12th, 2012 at 19:02 | #1