Introduction: Game Controller With Arduino
In this tutorial we’re going to make a DIY game controller similar to an Xbox controller for half the price! Check out the video or scroll past it for the written tutorial!
Supplies
First, let’s gather all of the materials we’ll need for this project:
The Arduino Pro Micro, which is the smallest Arduino board that has the 32u4 USB chip, which we need to communicate with the computer as a controller.
The Custom Controller PCB, which will hold all of our components together (and looks awesome!).
2 Joysticks, I’m going to use the standard Arduino joysticks but you can also pick up smaller ones and modify the pub design for that.
10 Buttons for the face of the controller: four for the D-Pad, four for the face buttons (A, B, X, and Y), and the menu and back buttons.
4 Angled Buttons for the top of the controller (normally there would be 2 buttons and 2 analog triggers on the top, but we’re just going to use 4 buttons).
A USB Type A-C Cable to connect the Arduino Board to your computer. (You probably already have this.)
A 5mm LED and 100 ohm resistor for a status light. (This is completely optional.)
(The above links are affiliate links, meaning that if you use them, I get a small commission at no extra cost to you. Thanks!)
ALTERNATIVELY, if there are any in stock, you can pick up a kit here:
Why pick up a kit? It's cheaper (because you're only paying for the exact number of materials you need, not bulk packs with extra items) and it helps me keep making tutorials.
Step 1: DESIGN
Now, let’s use a free software called KiCad to make the schematic and PCB.
This is much easier to show on video as there’s a lot of clicking and dragging stuff around, so I suggest you watch these videos where I’ll show you the step-by-step process of making the schematic and the PCB.
If you’re not interested in learning PCB design (or you’ve already learned!), skip to step 3 where I’ll show you how to order the PCB for pretty cheap.
Step 2: ORDERING
Now that we have our PCB designed, we can go ahead and order our PCB. If you don’t have the kit, you can follow this link to find the files on PCBWay.com. All you have to do is click ‘Add to Cart’ and you can checkout and get the PCBs manufactured and shipped ASAP.
Keep in mind that the minimum order quantity is 5, so you do have to buy 5 PCBs. I guess you’ll have to make some controllers for your friends as well!
(NOTE: As a PCBWay affiliate I may earn a small commission from each order at no extra cost to you. Thanks!)
Step 3: ASSEMBLY
Now that we’ve got our PCB and all of our components, let’s assemble the controller.
The first thing we have to do is prepare a couple of the components that aren’t quite ready to be dropped into the board.
First, we’ll desolder the joysticks from the PCBs they came on. It’s a bit tricky to avoid melting the plastic in the joystick, but the best process I found is this:
- Pry back the potentiometers from the joystick body and desolder them.
- Cut the bars of the metal casing that go into the PCB so you don’t have to desolder them.
- Desolder the 4 button pins and remove the whole joystick assembly from the PCB.
Next, we have to solder the pin headers onto the Arduino Pro Micro. This is pretty simple, just make sure the pin headers are level, solder the two ends of the pin header in place, and then solder all of the pins in between.
You don’t need to solder on the 2x3 ISP connector pin header, just the two long pin headers on the sides.
Now that all of the components are prepared, we can drop each and every one of them into the PCB and then solder them on.
The only trick is with the buttons, you want to bend the legs of the buttons in a bit so they are the same distance apart as the holes in the PCB. You’ll get a knack for it after one or two.
The rest of it is pretty simple, just make sure you get the components all the way into the PCB and try to get that perfect volcano shape of solder around each pin.
If you want more instruction, check out the video. But be warned, I didn’t follow my own advice on the Arduino Pro Micro, so it ended up a little crooked (with one set of pins not as far into the PCB as the other).
Step 4: PROGRAMMING
The last thing we need to do is program our controller!
We'll be using the Arduino XInput Libraries (one for the board and one for the code) which will allow us to emulate the functionality of an Xbox controller.
First, you'll need to copy the following URL into the Arduino IDE so it knows where to look for the XInput boards library. Under File>Preferences there's an 'Additional Boards Manager URLs' field:
https://raw.githubusercontent.com/dmadison/ArduinoXInput_Boards/master/package_dmadison_xinput_index.json
Under the Boards Manager you'll want to search for 'XInput' and install the first result. With that installed, you can go to Tools>Board and select XInput AVR Boards>Arduino Micro w/ XInput.
Then you'll want to install this library into the Arduino IDE: https://github.com/dmadison/ArduinoXInput
Check out this video right here for an in-depth walk through or just grab the code below.
#include <XInput.h>
void setup() {
// put your setup code here, to run once:
XInput.setAutoSend(false);
XInput.begin();
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// Left Trigger - 0
XInput.setTrigger(TRIGGER_LEFT, digitalRead(0) * -255 + 255);
// Left Button - 1
XInput.setButton(BUTTON_LB, !digitalRead(1));
// Back Button - 2
XInput.setButton(BUTTON_BACK, !digitalRead(2));
// Up DPad - 3 Down DPad - 5 Left DPad - 4 Right DPad - 6
XInput.setDpad(!digitalRead(3), !digitalRead(5), !digitalRead(4), !digitalRead(6));
// Right Trigger - 7
XInput.setTrigger(TRIGGER_RIGHT, digitalRead(7) * -255 + 255);
// Right Button - 8
XInput.setButton(BUTTON_RB, !digitalRead(8));
// B Button - 9
XInput.setButton(BUTTON_B, !digitalRead(9));
// A Button - 10
XInput.setButton(BUTTON_A, !digitalRead(10));
// Y Button - 14
XInput.setButton(BUTTON_Y, !digitalRead(14));
// Menu Button - 15
XInput.setButton(BUTTON_START, !digitalRead(15));
// X Button - 16
XInput.setButton(BUTTON_X, !digitalRead(16));
// A2, A3 LEFT
int leftXAxis = (analogRead(A3)-512) * 64;
int leftYAxis = (analogRead(A2)-512) * 64;
XInput.setJoystick(JOY_LEFT, leftXAxis, leftYAxis);
// A0, A1 RIGHT
int rightXAxis = (analogRead(A1)-512) * 64;
int rightYAxis = (analogRead(A0)-512) * 64;
XInput.setJoystick(JOY_RIGHT, rightXAxis, rightYAxis);
XInput.send();
}
NOTE: If you didn't watch the video, setting up the Arduino Pro Micro will make you unable to upload code to it normally.
First, you'll have to tick the box 'Show verbose output during compile' to true.
Then when it says it's started uploading, you'll need to manually short between the Reset pin and the Ground pin before it times out. You ONLY have to do this after you've uploaded the program one or more times, so if you just copy and upload the program once, you're fine.
Step 5: Congratulations!
You've finished making a controller with Arduino and now you can play games on your PC with a device you made. Well done!
If you need any help with your project or just want to show it off, join us on Discord: https://discord.gg/jP7hHr65yt