IMPORTANT NOTE: This article is for Arduino IDE version 1.6.6 (or above). To add a USB Game Controller to an Arduino Leonardo or Micro using Arduino IDE version 1.6.5 (or below) see the following Instructable: Add USB Game Controller to Arduino Leonardo/Micro.

Out of the box the Arduino Leonardo and the Arduino Micro appear to the host computer as a generic keyboard and mouse. This article discusses how the Arduino Leonardo and the Arduino Micro can also appear as a generic Game Controller or Joystick. This project will only work with Arduino products based on the ATmega32u4 microcontroller (i.e. the Arduino Leonardo and the Arduino Micro). It will not work with the Arduino UNO, because it is based on the ATmega328 microcontroller.

The Arduino Joystick Library (a.k.a. Game Controller library) used in the Instructable provides the following:

  • X, Y, and Z axis
  • 32 buttons
  • X, Y, and Z axis rotation
  • Rudder
  • Throttle
  • 2 Point of View Hat Switches

Step 1: Installing the Library

The latest version of the Arduino library that allows the Arduino Leonardo and Micro to appear as a Game Controller can be downloaded from the following GitHub repository:


This GitHub repository actually contains three different versions of the Arduino Joystick Library:

  • Joystick - Causes the Arduino to appear as single Game Controller
  • Joystick2 - Causes the Arduino to appear as two, simple Game Controllers
  • Joystick3 - Causes the Arduino to appear as three, simple Game Controllers

Copy the Joystick (and/or Joystick2 and/or Joystick3) folder from GitHub to the Arduino Libraries folder (typically located at %userprofile%\Documents\Arduino\libraries). The library (or libraries) should now appear in the Arduino IDE list of libraries.

<p>Hi Matthew,</p><p>I'm using your library to realize an analog handbrake.</p><p>Does it make sense to reduce the amount of traffic sent to the PC by reducing the frequency of calling the send command (directly or indirectly with autosend)? Currently I call the send operation only if the input value has changed. I'm wandering if the data is anyway sent with 100Hz to the PC so that this does not make sense at all!?</p><p>Thanks for any help and your great library,<br>Peter</p>
Peter,<br><br>I think you are OK either way. You can get by with only sending data when the values change, but sending at a rate of 100Hz should be OK as well. I do not think you would want to send any more frequently than that, however.<br><br>Matt
<p>Hi Matthew,</p><p>Thanks for your help! Finally I implemented it so, that data is only set if the change of the value is larger than a specific change tolerance which can be set by a constant: <a href="https://github.com/phaase/analog-controller-input" rel="nofollow">https://github.com/phaase/analog-controller-input </a> </p><p>Peter</p>
<p>Glad to hear you got it working the way you wanted. Thanks for sharing your link.</p>
<p>If I understand correctly, it does not makes sense to reduce the frequency of send operations on the device side...</p><p>From [https://en.wikipedia.org/wiki/USB_human_interface_device_class]:</p><p>&quot;The host periodically polls the device's interrupt IN endpoint during operation. When the device has data to send it forms a report and sends it as a reply to the poll token.&quot;</p>
It does take some of the host CPU time to process the HID Reports coming in, so you do not want to overwhelm the host computer with non-stop messages, but 100Hz should be fine.
<p>Thank you so much for this Instructable. How ever. Is there a <br>tutorial that you know of that finishes it off with the coding and <br>button schematics to full fill the 32 buttons and axes?. </p><p>When I <br>saw this I was looking for a DIY button controller. When I saw this it <br>had enough buttons and axes for exactly what I wanted. I went and bought<br> the Leo and a kit and a bunch of buttons. Got the Leo to work on <br>Windows but now I am stuck and trying to find a way to wire 32 button <br>and all the axes. </p><p>Thanks for any replay you can provide. Thanks</p>
<p>would making a switch matrix and using the keypad register work?</p>
<p>JasonG255 - The easiest way to get more that 16 buttons on a Leonardo or Micro is to use a series of Shift Registers. The Arduino website has a tutorial on how to use these at <a href="https://www.arduino.cc/en/Tutorial/ShiftIn" rel="nofollow">https://www.arduino.cc/en/Tutorial/ShiftIn</a>. Hope this helps. -Matt</p>
<p>Funnt really, I asked in the Arduino community for some help with making my own HID type interface for such a gizmo and I got nothing but demands for code that I didn't have and no help whatsoever.</p><p>*thumbs up* for a nice instructable.</p><p>*thumbs up* for a very nice instructable.</p>
<p>Hi Matthew,</p><p>Great job on the Joystick library.</p><p>I have a few questions. The game controller shows up as an Arduino Leonardo, is it possible to change its name?<br>Connection shows as a com port, dis you ever think about porting to LUFA and be able to assign a VID and PID?</p><p>I want to implement reading buttons from CD4021 shift registers. There is code for it that I could use, but was just wondering if you have ideas about this?</p><p>regards,</p><p>Arend</p>
Arend,<br><br>There is a current thread out on GitHub that is discussing renaming the Arduino at https://github.com/MHeironimus/ArduinoJoystickLibrary/issues/14. I have been wanting to investigate this for some time, but have not had the time. :(<br><br>Using a shift register (like the CD4021) should be fine. I have used 74HC595 shift register on other Arduino projects without issue.<br><br>Matt
<p>Greatjob!!! you have taught me soo much. thankyou ;)</p>
<p>Hi Matthew,</p><p>thanks for this library!<br></p><p>I just found <br>out that MULTI_AXIS does not work on the Micro, GAMEPAD does. In my case<br> the Micro was recognized as a com port without the slightest chance to <br>enter the game controller properties... Took me only 5 hours to find out<br> :)<br></p><p>(tested on Win 7/64(10.6.13) and MacOS Sierra (10.6.8))</p><p>It<br> is tempting just to read several analog inputs one after another, but <br>that is a trap: The analog inputs affect each other if you do not place <br>&quot;delay&quot; or &quot;(void)analogRead&quot; between them. </p><p>First part of my project was reanimating some gameport rudder pedals for DCS.<br></p><p>In the next part I will use an old MS Strategic Commander for absolute view control with programmable presets...<br></p><p>I<br> used to work with Digisparks but they are limited to two axes if you <br>connect them via USB. Maybe I will multiplex around that problem, but I <br>learned a lot tonight and I really like the power of 32 buttons and 8 <br>axes. Thank you very much!</p><p>Regards,</p><p>Andy</p>
<p>just i test this and found arduino micro controller and buttons, but how add and use all axis 12 pcs analog input pin to potentiometers ? i try add pot but no response no found. pot only do button 3, on/of game controller.</p>
<p>I recommend going through the Analog Read Serial example on the Arduino website (https://www.arduino.cc/en/Tutorial/AnalogReadSerial). If you can get that working, you can use that value as an input into the Arduino Joystick Library.</p>
how add joystick code this, i no understand good ? i need only read all analog input and then send data to game (FSX) simulator need read data what joystick do, <br>i need add arduino micro to usb-hid read all 12 analog input and flight simulator X use this, yuoke,trim wheels,rudder,trottle,propel,mixture,brake toe, test and micro show my device manager ok, and game controller show too good. <br>have you example one potentiometer how add to micro and working ?<br>then i maybe can understand and add all more axis and sliders to micro.
<p>I am currently finalizing Version 2.0 of this library. Once that is finalized, I will try to do a Instructable on using a potentiometer with the new version of the library (it may take me a while to get around to it, though).</p>
<p>i no understand thats do 3 arduino micro joystick my device manager, and run something tes prcedure move joystic but i no have connect any potentiometer and button this ? how made own program and add pot this ? i know only pot left to 5volt, right to gnd and center to A0-5 or 12,11,10,9,8,6,,4 pin but how code read analog pins ?</p>
<p>Help please...this is exactly what I need to set up a 6 x 6 button matrix to interact with a flight sim. I am running 32 bit Win 7 and using IDE 1.6.6 (after failing to get the other instructable to work with 1.6.4) and using a genuine Arduino Micro. However, the code will not compile. The Micro is selected in the IDE, the correct port is selected, and the library is in the correct file location. Any ideas? ee the error message quoted below.</p><p>&quot;Arduino: 1.6.6 (Windows 7), Board: &quot;Arduino/Genuino Micro&quot;</p><p>In file included from C:\Users\User\Documents\Arduino\libraries\Joystick\examples\UsbJoystickTest\UsbJoystickTest.ino:9:0:</p><p>C:\Users\User\Documents\Arduino\libraries\Joystick/Joystick.h:31:2: error: #error. The Joystick library can only be used with a USB MCU (e.g. Arduino Leonardo, Arduino Micro, etc.).</p><p>#error The Joystick library can only be used with a USB MCU (e.g. Arduino Leonardo, Arduino Micro, etc.).</p><p>exit status 1</p><p>Error compiling. &quot;</p>
Try using the latest version of the Arduino IDE (Version 1.6.12 at the time of me writing this reply - https://www.arduino.cc/en/Main/Software) and see if that fixes your issue. If it does not, please let me know.<br><br>Thanks,<br>Matt
<p>pins A0-5 analog and 12,10,9,8,6,3, must be have analog input how ?</p>
<p>i use buttons/switch mobiflight software and wery easy use, just made FSX radio com 7-segment displauy radio /nav/com 1 all and working ifne, but proplem have mobiflight NOT support analog input, buttons and switch can add all maximum what arduino mega have easy to use.and support many mega board sametime, </p><p>now need only analog input and hat switch to arduino micro board, how make ?</p>
<p>Hi, im totally newbie whit arduino but i need help, how make my arduino micro(or mage 2560 whit ch340 chip) or nano, or uno, i have.</p><p>but i buy micro this use. i need only all maximum (micro 12 pin) analog input and hat switch only, no need other buttons/switch.</p><p>how make code ? how burn code ? what program i need ?</p><p>if someone can made to me code help lot me my FSX simulator i need make own joystic,bedal,trottle,trim wheel, hat switch,etc. proplem have my project how made all analog input uses ?</p><p>i looking many month now google but not found help, all have only 3 or 6 or little analog input :(</p><p>hardware i have win 8, flight simulator X gold, arduino micro (or mega 2560 if can) and 12 analog input need +HAT switch. no more . </p><p>how made ? im titally not understand code lot :(</p><p>thanks regard matti.virta </p><p>send mail my gmail name have this. you know. if you can help me project. </p>
<p>If anyone experiences the issue I just listed then upgrade to 1.6.12 and hopefully the issue will be solved as mine was. </p>
<p>Hi @<a href="https://www.instructables.com/member/MatthewH" rel="nofollow">MatthewH</a> , PLEASE Please Help!!!</p><p>Thanks for awesome code and tutorial. I'm very new to programming and Arduino and I'm slowly getting hooked. All I want to do is have all 32 buttons and both Hat Switches working as keys. So it will give me 40 push buttons.</p><p>I'm planning to make a simple (mfd: multifunction color display) key pad for a flight sim, something like: http://www.thrustmaster.com/products/mfd-cougar-pack</p><p>I can get a basic button matrix to work, no problem but:</p><p>I can not for the life of me get the bottom matrix to work with your setup... I think I'm way too new to this! </p>
<p>@Schalkdk You will need to send me more details. You can send me a private message with the details (specifics about your hardware, what pins on the Arduino you are using, your sketch file, etc.).</p>
<p>Hi Matthew,</p><p>I just created a <a href="http://rc-aviation.ru/mtech/34-komptehnology/1847-usb-pp-arduino-pro-micro" rel="nofollow">publication about PPM to USB joystick converter</a> (in Russian) about application where I used your library. Thank you very much - it works fine. :)</p><p>I want also to share very important knowledge - if someone going to use the library with popular board <a href="https://learn.sparkfun.com/tutorials/pro-micro--fio-v3-hookup-guide" rel="nofollow">Arduino Pro Micro</a> he has to <strong>solder a capacitor 0.1 mkF between the board RST and GND pins</strong>. </p><p>The problem is Arduino Pro Micro works 'almost' as genuine Arduino Leonardo or Arduino Micro but it is too fast in initialization. It start to communicate with USB before the USB driver on the computer finished its initialization which <strong>can result in communication error</strong> (communication LED on the board blinks slowly, communication speed slow down, etc.). <strong> </strong>The attached capacitor slows down the board initialization and after the soldering such problem disappears. </p><p>Andrey</p>
<p>What is a 0.1 mkF capacitor in uF? Thank you,</p>
<p>0.1 mkF is the same as 0.1uF. Andrey is Russian. mk is micro, F isFarad </p>
<p>Thanks for sharing your findings about the Pro Micro.</p>
<p>Hello, this library is perfectly and exactly what I'm searching for. At the moment I try to program a gamepad for the recalbox on a raspberry pi. My only problem is to handle the bouncing of the button and the joystick. When I push the joystick one time, the Leonardo runs the loop more than one time and the recalbox register 5 to 6 movements so that the controlling in the recalbox is not possible. I try to handle this with a delay from 150 but with this delay it is not possible to use the joystick for movement and the buttons for example shooting &quot;parallel&quot;. How I can handle this problem with this library.</p>
<p>You are correct, you do not want to use the delay method to<br>debounce your buttons for a game controller project. The Arduino website has a<br>tutorial on how to debounce a button without using the delay() method. I use a<br>slightly different technique in some of my joystick projects. If you take a<br>look at the sketch files in the following two instructables, you can see how I<br>handled the debounce issue: <a href="https://www.instructables.com/id/Ultimate-Classic-Game-Console-Joystick-to-USB-Adap/" rel="nofollow">https://www.instructables.com/id/Ultimate-Classic-Game-Console-Joystick-to-USB-Adap/</a><br>and <a href="https://www.instructables.com/id/Classic-Joystick-to-USB-Adaptor/" rel="nofollow">https://www.instructables.com/id/Classic-Joystick-to-USB-Adaptor/</a>.<br>Basically what you want to do is read the value of each button line every time<br>through the loop and add them up. Then every 10 milliseconds (which can be<br>determined by a call to millis()), I check the sum and if it is over some threshold<br>value (e.g. 4 or so) I consider the button pressed for that 10 millisecond<br>frame. I then reset my counters and start adding again.</p>
What the code I must write?<br>I write this code but is not work normally<br>#include <br>int asu = A0;<br>int sensorValue = 0;<br>void setup() {<br> // put your setup code here, to run once:<br>Joystick.begin();<br>}<br><br>void loop() {<br> // put your main code here, to run repeatedly:<br> sensorValue = analogRead(asu);<br> Joystick.setXAxis(sensorValue);<br>}
<p>Have you tried debugging your code to see what value sensorValue is after the analogRead call? The current version of setXAxis expects this value to be between -127 and 127.</p>
Hi Matthew,<br>Thanks for the code and tutorial.<br>But oh my fool. Im stuck in step7. I don't know what next?<br>I want use a 3 potentiometer to control throttle, X axis and Y axis likes steering wheel and some button. Can you help me?
<p>Take a look at the Arduino examples for reading potentiometer values (https://www.arduino.cc/en/Tutorial/AnalogInOutSerial). Once you have your potentiometer value, you can use that in your Joystick.setXAxis, Joystick.setYAxis, etc.</p>
<p>Works with Arduino Due and IDE 1.6.10.. Thanks a lot!</p>
<p>Just getting the problem that it isnt always recognized by windows 10 as gamepad..</p>
<p>What does it show up as in this situation? Can you post a screenshot or create an issue on <a href="https://github.com/MHeironimus/ArduinoJoystickLibrary" rel="nofollow">https://github.com/MHeironimus/ArduinoJoystickLibr...</a> with the details?</p><p>Thanks,</p><p>Matt</p>
<p>If i just connect the Native Port it gets recognized as &quot;Bossa programm Port (COM X)&quot;. If i connect Programming Port .. wait a few seconds and then connect the Native Port, Windows sometimes doesnt recognize it at all (Reporting it as unknown device, error code 43) or it recognizes it as HID &quot;Arduino Due&quot; (1 out of 3 cases most of the time).</p>
<p>This sounds like an issue with the Arduino Due itself or a Windows driver issue. I do not have an Arduino Due to test with, so I am afraid I am not much help on this issue. I do know others have gotten it to work on an Arduino Due, however.</p>
<p>Thanks for posting what board and version of the Arduino IDE you are using.</p>
<p>Hi Matthew.. sorry to bother, but I'm really new at this.. I've bought a Leonardo, installed the IDE and your Joystick library (2.0beta), loaded the test sketch, and the demo is running.. yeppee! :)</p><p>However, I'd like to use it now as a 2 player arcade controller.. but found no guide whatsoever on how to accomplish that. The only hint I found is on your blog, but about colecovision controllers.. and your JoystickButton.ino (that does not seem to use a matrix tho) I would want to mae a simple (xy) 2 joysticks with 8 buttons each Is there any guide/schematic to learn the first steps on how to phisically connect the microswitch matrix to the digital inputs? (ie: will I need diodes on each button to avoid ghosting? Is there a better way?)<br>Many thanks for your work!</p>
<p>Here are some links to help you on your research:</p><p><a href="http://electronics.stackexchange.com/questions/83720/a-good-circuit-for-a-large-number-of-push-button-inputs" rel="nofollow">http://electronics.stackexchange.com/questions/837...</a></p><p><a href="http://pcbheaven.com/wikipages/How_Key_Matrices_Works/" rel="nofollow">http://pcbheaven.com/wikipages/How_Key_Matrices_Wo...</a></p><p>I think the following video only allows for a single button press:</p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/URO042VrCKU (I think this one only allows a single button press)" width="500"></iframe></p><p>If you get your matrix of buttons working the way you want, be sure to post what you did.</p><p>Thanks,</p><p>Matt</p>
<p>Hi Matt</p><p>thanks for the kind reply, however I'm having issues with the code needed to handle a matrix using your library rather than with the 'electrical' matrix itself. </p><p>Thanks!</p>
<p>Hi Matthew,</p><p>Great project and maybe exactly what I'm looking for but being a complete novice I may need sone help.</p><p>I'm trying to get an inverse button to work, like as in a flight sim there is a 3 position switch, how do I make it such that if say buttons 1 or 2 is NOT pressed then button 3 is active? This would mean I can get an on-off-on switch to act correctly.</p><p>Additionally, any idea on how to interface a rotary encoder to act as the potentiometer to give an axis output?</p><p>Thanks</p>
<p>These code samples are just pseudo-code (but they should get you started):</p><p><strong>3 Joystick Buttons calculated from 2 physical buttons code sample</strong></p><p>int button1Val =digitalRead(2);<br>int button2Val =digitalRead(3);</p><p>Joystick.setButton(0, !button1Val);<br>Joystick.setButton(1, !button2Val);</p><p>if ((button1Val == 1) || (button2Val == 1)) {</p><p> Joystick.setButton(2, 1);</p><p>} else {</p><p> Joystick.setButton(2, 0);</p><p>}</p><p><strong>Rotary Encoder Example</strong></p><p>I have never used a rotary encoder, so I am not 100% sure on this, but I think you would just convert the digital value read in from the rotary encoder to a numerical value and call the Joystick.setXAxis function.</p>
<p>Hi Matthew, in theory it all looks good. I could even follow it and modified the 4 button press to resemble what you listed and I can get the buttons to map where I want them. However the If - else statement does not seem to work for me. The Psudo-button is always high.</p><p>Any further advice?</p><p>#include &lt;Joystick.h&gt;</p><p>void setup() {</p><p> // Initialize Button Pins</p><p> pinMode(11, INPUT_PULLUP);</p><p> pinMode(12, INPUT_PULLUP);</p><p> // Initialize Joystick Library</p><p> Joystick.begin();</p><p>}</p><p>void loop() {</p><p> // Read pin values</p><p> int button0Val =digitalRead(11);</p><p> int button1Val =digitalRead(12);</p><p> Joystick.setButton(0, !button0Val);</p><p> Joystick.setButton(1, !button1Val);</p><p> if ((button0Val == 1) | (button1Val == 1)) {</p><p> Joystick.setButton(20, 1);</p><p> } else {</p><p> Joystick.setButton(20, 0);</p><p> }</p><p>}</p>
<p>+++++ UPDATE +++++++</p><p>managed to get it working, it was down to the &quot;If&quot; statements. when I changed the values to:</p><p> if ((button0Val == 0) || (button1Val == 0)) {</p><p> Joystick.setButton(20, 0);</p><p> } else {</p><p> Joystick.setButton(20, 1);</p><p> }</p><p>It decided to work.</p><p>Thanks for your help.... Im sure I will be sending more inthe future to aid in my little simulator project.</p>

About This Instructable




More by MatthewH:Create a Joystick Using the Arduino Joystick Library 2.0 Remote Control Your Raspberry Pi Ultimate Classic Game Console Joystick to USB Adapter 
Add instructable to: