loading

The chip ATMega32u4 what is used in Arduino Leonardo, Micro or Pro Micro have ability to use custom USB HID Descriptors. Thanks this ability we can make our Arduino be as generic Gamepad!

This library contains a all things to "simulate" a PS2 gamepad (so two thumbsticks and 16 buttons).

Step 1: Installing the Library

The library need a Arduino IDE 1.6.6 or higher for proper running.
Next we need to download a library.
All the data and documentation are storaged in a GitHub repository:

https://github.com/GAMELASTER/ArduinoGamepad

For download click a "Download ZIP" button in right side.
Create a new directory named "Gamepad" in (typically located in Windows at) Documents > Arduino > libraries, and copy all files from "ArduinoGamepad-master" directory in downloaded ZIP into that directory.
Then start a Arduino IDE and library appear in list of libraries and in examples list.

Step 2: Comiple and Run a Test Sketch

Just open a example sketch as in image, compile it and upload it into your Arduino

Step 3: Lets Verify of Working a Library

Go to Control Panel > Devices and Printers and search your device (for me its a Arduino Leonardo) > Right click > Settings of game device > Properties > Test.

If buttons step by step going enabled/disabled, all is okay!

Step 4: Connecting Thumbaxises and Buttons to Arduino

For testing a real connection between hardware, Arduino and PC we need some buttons or thumbaxises.
I got two thumbaxises + 2 buttons (pressing a thumbaxises), connected it with Pro Micro and lets go make an Arduino Sketch.

I connected Left Thumbstick to:

  • X to A2
  • Y to A3
  • Z to 4

I connected Right Thumbstick to

  • X to A0
  • Y to A1
  • Z to 3

Step 5: Testing the Thumbsticks

https://tkkrlab.nl/wiki/Arduino_KY-023_XY-axis_joystick_module

Compile, deploy and open Serial Monitor.

Try to move with thumbstick to minimum - maximum, try to click etc.
The thumbsticks can be a different. For me its have a relative values.
Minimum value of axis is 0 and fully maximum is 1000. So logically the center is 500.
At my Thumbstick is problem, the center have (almost) still different value, so when we will be doing a integration with library, we must make calibration, stabilisation and final formatting of values.
And the Z (pressing) i have 0 when pressed and 1 when released (this is not a true, its inverted, the invert doing a INPUT_PULLUP).

Step 6: Integrating Values to Library

Sketch:

https://github.com/GAMELASTER/ArduinoGamepad/blob/master/examples/ThumbsticksTest/ThumbsticksTest.ino

In this sketch I integrated my thumbsticks as I refferenced in previous step.
Now I will describe it.

  1. For first we need a include a library (line 7)
  2. Next we need to define a centers values for all axises, i setted all 500, because its a default center (line 10 - 13)
  3. We defining a multiplier for formatting a values. Because maximum value is 127, we get multiplier by dividing 127 with the center, so 500 (line 14 - 17)
  4. Initializing a Gamepad class (line 20)
  5. Initializing a Pin modes (line 23 - 28)
  6. A calibration of thumbsticks begin (line 29)
    1. We define a variables, and other this (line 72 - 73)
    2. A while... This while is for "verifying axises of thumbsticks", if is in center area (line 74)
    3. We read a Analog Input from axises (line 76 - 79)
    4. We need to check if axises is in a "center area", i selected a 100 range (line 80 - 83)
    5. If is all good, while going again until is 8 good confirms, so we gets a actually real center. (line 84 - 89)
    6. We set axises values to global variables and we recalculate a multipliers for all axises, so realCenter divided by 127
  7. In loop we read analog inputs and some buttons (line 33 - 37 and some other lines)
  8. Now we need format and stabilise values.
    1. So for first we need to get axis value, where 0 is center, so Axis Value minus Axis Center
    2. Then we this multiple by axis multiplier
    3. And for stabilise we floor it (no round)
  9. And last thing is "send" variables into a library.

The numbers of buttons is described in image upper.

Step 7: Testing a Final Result

Compile, deploy and lets go play some game!

I selected a Mirrors Edge, but Mirrors Edge supports only a XBOX 360 gamepad, this can be simply solved by downloading X360CE.

Step 8: More Info, Bugs Etc.

This library is a part of my project where I build custom handheld Raspberry Pi Retro Console, what will be posted here in Instructables when it will be done, for now I writing a blogs of developing at my blog, dont forget to look it!

Documentation, Bugs etc. is in GitHub:

http://github.com/GAMELASTER/ArduinoGamepad/

Or tell me it into a comments!

<p>Hello<br>how can I use for example also Z axis?</p>
Hello,<br><br>Z axis is just a button, when you click with joystick.<br>As you can see, on Arduino joystick the button is a SW pin.<br>https://github.com/GAMELASTER/ArduinoGamepad/blob/master/examples/ThumbsticksTest/ThumbsticksTest.ino#L54<br>There is a conditions of buttons, its a PULLUP output so be careful !<br>If you have any other questions, let me know it!
<p>Thank you for the reply<br>but if I wanted to connect a third axis on pins A4 and A5 how can I do?</p>
<p>You want connect third joystick?</p>
<p>yes.. I need me for a third joystick. thanks</p>
Yes. Thanks.
<p>thankyou so much! im just a beginner in using arduino, this is so great just a little question? how can i invert the y axis? thanks in advance.</p>
Hello,<br>For inverting axis, just use something simple like this:<br>1023 - analogRead(your pin)<br><br>If you have any other questions, let me know!
<p>I like the fact that microcontroller like an Arduino lets you automate commands in the video game. </p>
<p>Yep, Arduino is powerfull thing!</p>

About This Instructable

6,731views

25favorites

More by GAMELASTER:Arduino Leonardo/Micro(ATMega32u4) as Gamepad/Game Controller 
Add instructable to: