Introduction: Arduino Leonardo/Micro(ATMega32u4) As Gamepad/Game Controller
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:
In this sketch I integrated my thumbsticks as I refferenced in previous step.
Now I will describe it.
- For first we need a include a library (line 7)
- Next we need to define a centers values for all axises, i setted all 500, because its a default center (line 10 - 13)
- 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)
- Initializing a Gamepad class (line 20)
- Initializing a Pin modes (line 23 - 28)
- A calibration of thumbsticks begin (line 29)
- We define a variables, and other this (line 72 - 73)
- A while... This while is for "verifying axises of thumbsticks", if is in center area (line 74)
- We read a Analog Input from axises (line 76 - 79)
- We need to check if axises is in a "center area", i selected a 100 range (line 80 - 83)
- If is all good, while going again until is 8 good confirms, so we gets a actually real center. (line 84 - 89)
- We set axises values to global variables and we recalculate a multipliers for all axises, so realCenter divided by 127
- So for first we need to get axis value, where 0 is center, so Axis Value minus Axis Center
- Then we this multiple by axis multiplier
- And for stabilise we floor it (no round)
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!
1 Person Made This Project!
- yasserhcn made it!
14 Comments
Question 4 years ago
Hello,
I tried to do this. When I compile the "simple test", a error was occurred as " 'Gamepad' does not name a type". ( see the picture. ) Please help.
Hemantha
Answer 3 years ago
Go to tools, then click Board: then select Arduino/Genino Micro. Should fix it.
4 years ago
Hi there, For some reason my Arduino detects it as a keyboard/mouse and therefore I can not go to the game controller setting. Do you know why this might be happening? Using the latest windows 10.
5 years ago
Is there a way to change the name of the Controller when its connected to the computer? For example "My Controller" ??
5 years ago
Hello
how can I use for example also Z axis?
Reply 5 years ago
Hello,
Z axis is just a button, when you click with joystick.
As you can see, on Arduino joystick the button is a SW pin.
https://github.com/GAMELASTER/ArduinoGamepad/blob/master/examples/ThumbsticksTest/ThumbsticksTest.ino#L54
There is a conditions of buttons, its a PULLUP output so be careful !
If you have any other questions, let me know it!
Reply 5 years ago
Thank you for the reply
but if I wanted to connect a third axis on pins A4 and A5 how can I do?
Reply 5 years ago
You want connect third joystick?
Reply 5 years ago
yes.. I need me for a third joystick. thanks
Reply 5 years ago
Yes. Thanks.
5 years ago
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.
Reply 5 years ago
Hello,
For inverting axis, just use something simple like this:
1023 - analogRead(your pin)
If you have any other questions, let me know!
6 years ago
I like the fact that microcontroller like an Arduino lets you automate commands in the video game.
Reply 6 years ago
Yep, Arduino is powerfull thing!