Introduction: Add USB Game Controller to Arduino Leonardo/Micro

Picture of Add USB Game Controller to Arduino Leonardo/Micro

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

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. 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 generic Game Controller 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: Updating the Arduino Code

First make a backup copy of the following two files in the “%PROGRAMFILES%\Arduino\hardware\arduino\avr\cores\arduino” folder:

  • USBAPI.h
  • HID.cpp

Replace these two files with the ones attached to add a generic Game Controller to the Arduino Leonardo and the Arduino Micro.

Step 2: Running the Test Sketch

Compile and upload the attached UsbJoystickTest.ino sketch file onto the Arduino Leonardo or the Arduino Micro using the Arduino Software (IDE). I have tested this using version 1.6.1 through 1.6.5 of the software. For Arduino IDE version 1.6.6 and above, see the following Instructable: Arduino Leonardo/Micro as Game Controller/Joystick.

The following steps are for Windows 7. If you have a different version of Windows or a different operating system, these steps may differ.

Step 3: Windows Control Panel - Hardware and Sound

Picture of Windows Control Panel - Hardware and Sound

Go to the Windows Control Panel and select “Hardware and Sound”.

Step 4: Devices and Printers

Picture of Devices and Printers

Then select “Devices and Printers”.

Step 5: Arduino Should Appear

Picture of Arduino Should Appear

The Arduino Micro or Arduino Leonardo should appear in the list of devices.

Step 6: Arduino Settings

Picture of Arduino Settings

  • Next right mouse click on the Arduino Leonardo or Arduino Micro to display the settings menu.
  • Then select “Game controller settings” to get to the “Game Controllers” dialog.

Step 7: Game Controller Settings

Picture of Game Controller Settings

The Arduino Micro or Arduino Leonardo should appear in the list of installed game controllers. Select the Arduino Micro or Arduino Leonardo and click the Properties button to display the game controller test dialog.

Step 8:

Picture of

While this dialog has focus, ground pin A0 on the Arduino to activate the test script. The test script will test the game controller functionality in the following order:

  • 32 buttons
  • throttle and rudder
  • X and Y Axis
  • Z Axis
  • 2 Hat Switches
  • X and Y Axis Rotation

Step 9: Joystick Library

Now that the Arduino Leonardo or Arduino Micro has the Joystick library, the Arduino can be used for custom game controller projects. The following describes the Joystick library that is included in the updated USBAPI.h and HID.cpp files.

Joystick.begin(bool initAutoSendState)

Starts emulating a game controller connected to a computer. By default all methods update the game controller state immediately. If initAutoSendState is set to false, the Joystick.sendState method must be called to update the game controller state.


Stops the game controller emulation to a connected computer.

Joystick.setXAxis(byte value)

Sets the X axis value. Range -127 to 127 (0 is center).

Joystick.setYAxis(byte value)

Sets the Y axis value. Range -127 to 127 (0 is center).

Joystick.setZAxis(byte value)

Sets the Z axis value. Range -127 to 127 (0 is center).

Joystick.setXAxisRotation(int value)

Sets the X axis rotation value. Range 0° to 360°.

Joystick.setyAxisRotation(int value)

Sets the Y axis rotation value. Range 0° to 360°.

Joystick.setZAxisRotation(int value)

Sets the Z axis rotation value. Range 0° to 360°.

Joystick.setButton(byte button, byte value)

Sets the state of the specified button. The button is the 0-based button number (i.e. button #1 is 0, button #2 is 1, etc.). The value is 1 if the button is pressed and 0 if the button is released.

Joystick.pressButton(byte button)

Press the indicated button. The button is the 0-based button number (i.e. button #1 is 0, button #2 is 1, etc.).

Joystick.releaseButton(byte button)

Release the indicated button. The button is the 0-based button number (i.e. button #1 is 0, button #2 is 1, etc.).

Joystick.setThrottle(byte value)

Sets the throttle value. Range 0 to 255.

Joystick.setRudder(byte value)

Sets the rudder value. Range 0 to 255.

Joystick.setHatSwitch(byte hatSwitch, int value)

Sets the value of the specified hat switch. The hatSwitch is 0-based (i.e. hat switch #1 is 0 and hat switch #2 is 1). The value is from 0° to 360°, but in 45° increments. Any value less than 45° will be rounded down (i.e. 44° is rounded down to 0°, 89° is rounded down to 45°, etc.).


Sends the updated joystick state to the host computer. Only needs to be called if AutoSendState is false (see Joystick.begin for more details).


charleslinquist (author)2017-10-29

I believe I'm getting it. I have 5 channels working, and now I'll try to add the other 3. It helps that the program that this controls allows me to "map" the "channels" to whatever I want, so I don't have to concern myself with what each channel is called - or is usually used for.

I may yet need more help, but I'm making solid progress. Knowing programming (in general) has helped a lot, and I'm slowly learning the way Arduino does things.

Thanks for everything so far....

charleslinquist (author)2017-10-28

I'll read some more, but note this is NOT for a game.
This is for robot/drone control.

I have -

Two - two-axis joysticks = 4 analog inputs
MODE control = 1 analog
Camera tilt (pitch) = 1 analog
Camera yaw = 1 analog
Camera MODE = 1 analog
SPARE = 1 analog.

All inputs will be either from a potentiometer wiper or from a variable voltage source. The potentiometers produce an approx 1.2V - 3.8V signal, while the voltage source produce 0V - 5V. As mentioned before, there will be no buttons or switches. And just as in a game controller, the response times should be very short.

charleslinquist (author)2017-10-18

I'm a newbie as well. I have my board set up as a Leonardo, yet I get the message

'Joystick was not declared in this scope'.

I put the files you mentioned in the

PROGRAM FILES(x86)\Arduino\hardware\arduino\avr\cores\arduino

folder (I had to rename them so they matched the names you gave them - since during the download, the names were changed).

One thing that could be an issue: My Arduino files are in several locations on my hard drive. I can find no guide as to where everything should be in order to insure that all the various sketches and libraries are found by the IDE.

I need 8 ANALOG channels and no buttons.

I also cannot find any documentation on where to connect the potentiometers for the various channels. Is that somewhere?

MatthewH (author)charleslinquist2017-10-19

I no longer support this method of doing this. I have a much easier way to do this using the Arduino Library ( There are examples on how to install and use this library on GitHub and at

charleslinquist (author)MatthewH2017-10-22


I apologize for my lack of knowledge. I'm an experienced PIC Basic Pro (PBP) programmer. But Arduinos work a LOT different.

My goal is to use an ordinary hand-held R/C controller with 8 pots (pitch, roll, yaw ,throttle, mode, camera pitch, camera yaw, spare) to control a quadcopter using my laptop and the Mavlink Protocol. A program called MISSION PLANNER running on my laptop does the conversion between the joystick and Mavlink. This will allow me to use my laptop's 4G LTE connection to send commands to my quadcopter (which also has a 4G connection). This gives me virtually unlimited control range.

I need at least 8 ANALOG channels where an input voltage of 1-4V sets the output. I don't need any toggle or "hat" switches at all.
I can use ANALOG READ() to get a value. But how many bits are returned - 8? 10? 12? 16?. And what is the reference? 3.3V, Vcc, something else? I looked through the joystick library and I can't figure out how to make all channels analog, and what is the syntax to get an A/D reading into a 8-bit joystick "position".

MatthewH (author)charleslinquist2017-10-28

> I can use ANALOG READ() to get a value. But how many bits are returned - 8? 10? 12? 16?

This is documented on the Arduino website:

The Arduino uses a 10-bit A-to-D converter.

> And what is the reference? 3.3V, Vcc, something else?

Some links to help you with your question:

For the Leonardo and the Micro the default reference is 5V.

charleslinquist (author)MatthewH2017-10-24


I downloaded and installed the latest version of the joystick library.
I figured out how to read analog channels on an Arduino, and I can read them and send values to the terminal.

But I have some more questions:

In the Multiple Joystick Test program example -
There is a line
JOYSTICK_TYPE_MULTI_AXIS 32,1,true,true, false, true, true, false, false, false...

What are the numbers "32" and "1" for?
What does "true,true, false, true, true....." mean?
What is gcCycleDelta?
What is gcAnalogDelta?

If I want 8 analog joysticks and no digital buttons, how would set up that?

I look at each individual analog joystick as a "channel" . Is it correct that I can write a value to a channel by
Joystick[channel].setXAxis(analog value); ?

But I'm confused by the 'setXAxis' part. I was thinking if 'channel' is a variable that lets you write to any channel, but 'setXAxis' refers to a particular channel.

And if you have 8 joysticks, how do you refer to them? "X", "Y", "Z" .... then what?

I also assume that the value range expected by the library is -127 to +127. Is that correct?
If the above is true, then wouldn't
JoyStick_Value = (analogRead(1) >> 2) - 127
do the right thing?

MatthewH (author)charleslinquist2017-10-28

I think most of your answers can be found on the ReadMe file at

> What are the numbers "32" and "1" for?

The 32 is for buttonCount

The 1 is for hatSwitchCount

These are explained in the constructor portion of the Joystick Library API located in the ReadMe file.

> If I want 8 analog joysticks and no digital buttons, how would set up that?

Question: Do you want a single joystick with 8 axes or do you want 8 joysticks with an X and Y axis each?

Having 8 axes on a single joystick is tricky. The USB specification defines X, Y, Z, RX, RY, and RZ axes (which gets you to 6), but does not give you the ability to create more. You can also turn on some of the other analog inputs, like Accelerator and Throttle, but your game would need to support reading those values. A lot of games max out at 6.You can learn more about this at

Having 8 joysticks with an X and Y each is just a matter of creating 8 instances of the Joystick class and giving them each a unique hidReportId. I have only tested up to 4, however.

> Is it correct that I can write a value to a channel by Joystick[channel].setXAxis(analog value); ?


> And if you have 8 joysticks, how do you refer to them? "X", "Y", "Z" .... then what?

You refer to them as 0, 1, 2, 3, 4, 5, 6, 7. For example, to set the Y axis for Joystick #4 (the index would be 3) to 100:


> I also assume that the value range expected by the library is -127 to +127. Is that correct?

That was the range for Version 1.0. Version 2.0 aligns with the Arduino defaults (which is 0 to 1023). You can set the range to anything you want, however, using the setXAxisRange, setYAxisRange, etc. methods.

Hope this helps,


charleslinquist (author)MatthewH2017-10-19

I have successfully compiled the code and installed it into a Leonardo.

Then, when I go into Windows 10 "Devices and Printers". It identifies the device as "Arduino Leonardo", but the category is "Unspecified" and when I right-click on that icon, I get only

Create Shortcut
Remove Device

When I click on PROPERTIES, I get
"Report ID declaration outside of top level collection"

Apparently the joystick driver isn't loading properly.

SondreL1 (author)2017-06-30

Can anybody give me some tips on how to code for 14 digital buttons? Im very new to programming and im trying to make my first raspberry pi gameboy case. I would really apreciate a little help.
Looking forward to hear from you.

MatthewH (author)SondreL12017-07-06

The latest version of this Arduino Library ( has a simple Gamepad example. The following Instructable shows how to use that example: It get to 14 buttons, you just need to follow the patterns established by that example. Hope this helps.

米唐米 (author)2017-03-08

hi all,
just figure out the code i need for the joystick i need finally.

with 1 analog and 9 button mini joystick for my retropie.

i'm new in arduino, and not good in coding, just curious if any better way to do this.
btw, it's interest for try and error in coding arduino ;)

here is the code just modify by web research:

#include <Joystick.h>

Joystick_ Joystick(0x03, 0x04, 9, 0, true, true, false, false, false, false, false, false, false, false, false);

void setup() {

pinMode(2, INPUT_PULLUP); //A

pinMode(3, INPUT_PULLUP); //B

pinMode(4, INPUT_PULLUP); //X

pinMode(5, INPUT_PULLUP); //Y

pinMode(6, INPUT_PULLUP); //L

pinMode(7, INPUT_PULLUP); //R



pinMode(10, INPUT_PULLUP); //EXIT





const int pinToButtonMap = 2;

int lastButtonState[9] = {0,0,0,0,0,0,0,0,0};

void loop(){

int xAxis = analogRead(A0);

int yAxis = analogRead(A1);

xAxis = map(xAxis, 1023, 0, 0, 255);

yAxis = map(yAxis, 0, 1023, 0, 255);



for (int index = 0; index < 9; index++)


int currentButtonState = !digitalRead(index + pinToButtonMap);

if (currentButtonState != lastButtonState[index])


Joystick.setButton(index, currentButtonState);

lastButtonState[index] = currentButtonState;




-The_Hawk- (author)2016-05-26

Hi, is possible with this create a force feedback Sterring Wheel??? And witch board i can use??

MatthewH (author)-The_Hawk-2016-05-27

I think this library could be used as a starting point, but I have not researched this. This library currently only supports sending Joystick data to the host machine. Force feedback devices get data back from the host computer and act on it (e.g. making the steering wheel harder to turn).

The library I wrote should work with any ATmega32u4 - based Arduino (or clone). I have tested it with the Arduino Leonardo and Arduino Micro. I know of others who have used it with the Arduino Due and SparkFun Pro Micro. I know it will not work with the Arduino Uno (since it used the ATmega328P).

-The_Hawk- (author)MatthewH2016-05-27

Thank for your response..

I will try to go on this direction, if you have other info about it let us know them! I know that a lot of people have the same problem!

BorosR made it! (author)2016-05-23

I was build it, and works with Arduino Due.
Thank you!
It works with TrainSimulator 2016 very well!

MatthewH (author)BorosR2016-05-23

Glad it worked for you. Thanks for letting me know what Arduino you used.

AnthonyM143 (author)2016-04-02

Great library and very useful. However I have been experiencing the issue where I cannot use both the Joystick.h and either/both Mouse.h and Keyboard.h. It seems the arduino can't be recognized as both a game controller and an HID device. Is there any workaround that you know of?

MatthewH (author)AnthonyM1432016-04-03

Can you include an example sketch file (*.ino) that demonstrates your issue? I have successfully used Joystick.h and Keyboard.h together without issue (see for an example of this).

AnthonyM143 (author)MatthewH2016-04-03

Thanks for the response! I actually got them to work together by installing the Joystick Libraries in the Program Files folder, not in the Documents/Arduino folder. The past day has been very productive thanks to your code. Thanks!

MatthewH (author)AnthonyM1432016-04-04

I am glad to hear you got it working.

MattiV (author)2016-03-23

i no understand ino file anythink, how working ?????

how add all my switch to ino and how read and write ???? only need all toggle/button at my flight sim panel, start,run,accu,light,strobe, etc.

MattiV (author)2016-03-23

last no understand, what joystick library and were ? what need change ? and if need only max input what can, only button/switch, toggle sw, what need change and were ?

MattiV (author)2016-03-22

i not understand english good but i need my new arduino micro make maximum(how many?) toggle switch input only, how make ? (Motor and light+radio panel+audio control, whit fsuip. control ms9 simulator)

and how ? hid description, how arduino ino file ? how connect buttons ?

and second micro board need make only all (how many can?) encoders input.

if somebody can help me ?

ShushanshG (author)2016-03-15

I am constantly getting an error "'Joystick' was not declared in this scope." ...............plz help

MatthewH (author)ShushanshG2016-03-15

Not having the correct board selected (i.e. Arduino Micro or Arduino Leonardo) in the Arduino IDE is usually the cause of this error.

ShushanshG (author)MatthewH2016-03-15

I am using Arduino Uno, and I have selected this board only, so it won't work on Arduino Uno ?

MatthewH (author)ShushanshG2016-03-16

This only works with the Arduino Leonardo and Arduino Micro.

Valerio75 (author)2016-03-06

Good evening and sorry for the trouble.
But who wrote this guide, you may add links to do to connect:

X, Y, and Z axis (including rotation), 32 buttons, 2 hat switches, a throttle, and a rudder.

MatthewH (author)Valerio752016-03-07

Jakkul has a good example of how he used the library on his blog at His example uses three potentiometers (connected to A0, A1, and A2). and two buttons. I have an example at Ultimate Classic Game Console Joystick to USB Adapter where I show how to use the library to connect classic game controllers to a modern PC. These should get you started.

Valerio75 (author)2016-03-06

Good evening and sorry for the trouble.
But who wrote this guide, you may add links to do to connect:

X, Y, and Z axis (including rotation), 32 buttons, 2 hat switches, a throttle, and a rudder.

Valerio75 (author)2016-03-06

Good evening and sorry for the trouble.
But who wrote this guide, you may add links to do to connect:

X, Y, and Z axis (including rotation), 32 buttons, 2 hat switches, a throttle, and a rudder.

Then it would be possible to bring the 4 hat switches

RajatS35 (author)2016-02-12

is there any way to control a arduino with a quantum gamepad via usb host shield

MatthewH (author)RajatS352016-02-14

Theoretically yes, but I have not done anything like it. The USB Host Shield will allow the Arduino to act as the USB Host. The library that comes with the USB Host Shield should allow you to read the HID report. This might help:

RajatS35 (author)MatthewH2016-02-15

thank you for the suggestion

baritonomarchetto (author)2016-02-12

aaaaand ... a few seconds later I had success! :)

You have to select the Arduino Leonardo/micro board for the sketch to compile! Good to know ;)

baritonomarchetto (author)2016-02-12

Just noticed that I wrote in the "version <= 1.6.6 instructible, but I correctly tested the updated library/files.

I tested both, after my first comment, but I had no success...

baritonomarchetto (author)2016-02-12

MattewH I am sorry but no matter what i try to do, after loading the sketch if i run a check of the "JoystickButton" example arduino returns 'Joystick' was not declared in this scope.

I am using the 1.6.6 version of the IDE and the libraries are listed in the contributed libraries ... any hint?

Jorri11 (author)2016-02-07


Is it possible for me to use the joystick library, and still use "serial.print" for a different function?

I tried using the joystick.begin and .end as well as the serial .begin and .end and putting the different code between them but i get no information in the serial monitor. The game controller buttons does still work.

MatthewH (author)Jorri112016-02-10

This change should not affect the serial.print functions. They should work like they did before. I have used the Serial.print function on many occasions when I was debugging projects that use the Joystick library.

You might want to verify the baud rate on your serial monitor matches the Serial.begin function.

stanv2 (author)2016-01-18

Hi MatthewH,

I'm sorry for my silence past 15 days.....

It might be a bit dumb, but I don't know much more after you wrote an reaction about JAKKUL on my first comment.

Meanwhile I was waiting 3 weeks for my pushbuttons to arrive from China.

MatthewH (author)stanv22016-01-19

I would start simple (i.e. just try to get a single button working) and then go from there. Create a simple sketch that reads the value of a physical button and calls the Joystick.setButton function. Once it is written, test it out and see if it works. If it does not, you can post the code to a comment and I (or someone else in this community) can take a look at what you have done and provide guidance. You can find a simple button press example on the following GitHub repository:

stanv2 (author)MatthewH2016-01-20

keeps saying:


Arduino: 1.6.7 (Windows 8.1), Board:"Arduino/Genuino Micro"

C:\Users\Eelco\Documents\Arduino\UsbJoystick\UsbJoystick.ino:18:22: fatal error: Joystick.h: No such file or directory

#include <Joystick.h>


compilation terminated.

exit status 1

while Joystick.h is in the directory....

MatthewH (author)stanv22016-01-20

Verify you have the Joystick library installed. You should find this library in your C:\Users\<your user name>\Documents\Arduino\libraries folder. You will know you have the library installed properly if it appears in Arduino's Sketch -> Include Library menu.

GorgeousJack (author)2016-01-16

Hello MatthewH,

Thank you
very much for your tutorial.

We build a small customized Joystick within hours,
thanks to your description … but I still have a problem with one function.

It borders
me, that it isn’t possible to see the z-Rotation in the Arduino Micro
Properties (Preview of Joystick signals) I
also cannot use it in a game (although
the games supports this axis) … but I can’t find the error in the
HID-definition. The pointer definition seems legit.

Do you have
a clue how to fix that issue (so I can use also the z-Rotation)?

Thank you
in advance!

MatthewH (author)GorgeousJack2016-01-16

What operating system are you using (e.g. Windows 7, Windows 10, Linux, etc.)?

What game are you trying to play?

If your game has a controller configuration menu/screen, you could try to use that and see if the Z-Axis rotation can be configure. As far as I know, the Windows Game Controller Test dialog does not display/support the Z-Axis rotation. It should work in a game that supports a Z-Axis rotation, but I do not have a game like that I can test it with.

GorgeousJack (author)MatthewH2016-01-17

Hello MatthewH,

thanks for
the fast reply. I’m using Win 10, but I also noticed the same issue on Win 7. The
only reason I come up with that is that I already saw another Demo implementation where the Z-Rotation was displayed in the Game Controller Test dialog (see

But I cannot
see the difference between the two definitions.

However you saved me a lot of time with your detailed tutorial ^^. Thanks again!

If I find
some solution I’ll inform you too.

MatthewH (author)GorgeousJack2016-01-18

One more thing...

There are various tutorials out there on the web for creating your own Report Descriptors. Here are a few I have come across:

MatthewH (author)GorgeousJack2016-01-18

I see from your link that someone got a Z-Axis Rotation working. Unfortunately I am very busy right now, otherwise I would try to solve that issue for you, but I will give you an idea of something can try:

I noticed on the Test Dialog shown on the link you provided, their joystick had fewer features than the one provided in this Instructable. I would try simplifying the Joystick Definition to only include the items you absolutely need. This will probably be easier to do using the instructions provided in the Arduino Leonardo/Micro as Game Controller/Joystick Instructable, but it can be done by modifying the HID.cpp and USBAPI.h file provided in this Instructable. The main thing you will need to change is the _hidReportDescriptor, defined in the HID.cpp file (or the Joystick.cpp if you are using Arduino 1.6.6). You will also need to update the data array in the Joystick_::sendState function to match the new structure you define in the _hidReportDescriptor.

Good luck.

stanv2 (author)2016-01-04

Dear MatthewH,

Everything was going good untill I came at step 9. Because I am new to arduino's and programming,I was trying to understand how step 9 works, But I couldn't.

My goal is to get as much pushbuttons connected including the X, Y and Z axis (for gas, brake and a streer).

I am using An Arduino Micro.

I would really appreciate if you could help me, or someone else.

About This Instructable




Add instructable to: