Introduction: How to Make a DIY Game Controller (Keyboard Emulator) Using Arduino Leonardo

About: Hi, my name is IshanDatta. I make Arduino projects for a living! I hope you enjoy my projects!

Hey guys, it's IshanDatta here! Have you ever wanted to have a PS5/PS4/XBOX controller but you're too broke to buy it? Well worry no more! For I have made a fantastic game controller using the Arduino Leonardo and some buttons, wires, etc., known as the GameController™! This project was abandoned a year ago, but I finished the project 3 days prior to making this tutorial. The original project is here (made by Arduino themselves)

Supplies

You need 4 push-buttons (at least for this project), 9 wires, Arduino Leonardo (at least for this project as well), USB-B cable, computer, full breadboard (at least for this project as well), Arduino IDE (V1 or V2), 4 resistors

Step 1: Installation of the IDE

Install the Arduino IDE (V1 or V2) from your software manager, or from here

Step 2: The Building Part

Follow through the above image of the circuit diagram & plug in your USB-C Cable to your Arduino Leonardo & your computer

Step 3: The Coding Part

Now, we can get to the second half of the project, the coding

1) Open your Arduino IDE in your computer

2) Select the Arduino Leonardo board

3) Install the 'Keyboard' library (for IDE V2) by opening the 'Library Manager' tab in the left panel of the IDE, (for IDE V1) by pressing CTRL+SHIFT+i

4) After library installation, paste this code into the editor:-

/*
GameController™

Controls the keyboard from four pushbuttons, like a game controller on an Arduino Leonardo, Pro Micro or Due.

Hardware:
- Four pushbuttons attached to D2, D3, D4, D5

created 15 Mar 2012 - by Tom Igoe
modified 23 Dec 2024 - by Ishan Datta on https://www.instructables.com/How-to-Make-a-DIY-Game-Controller-Keyboard-Emulato/

Original example code is in the public domain.

https://www.arduino.cc/en/Tutorial/BuiltInExamples/KeyboardAndMouseControl
*/

#include "Keyboard.h" // add library

// set pin numbers for the five buttons:
const int upButton = 2;
const int downButton = 3;
const int rightButton = 4;
const int leftButton = 5;

void setup() { // initialize the buttons' inputs
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);

Serial.begin(9600);
Keyboard.begin(); // initialize keyboard control
}

// use the push-buttons to control the keyboard:
void loop() {
if (digitalRead(upButton) == HIGH) {
Keyboard.press('w'); // you can change the 'w' into any key on the keyboard like '1'
}
if (digitalRead(upButton) == LOW) {
Keyboard.release('w');
}
if (digitalRead(downButton) == HIGH) {
Keyboard.press('s');
}
if (digitalRead(downButton) == LOW) {
Keyboard.release('s');
}
if (digitalRead(leftButton) == HIGH) {
Keyboard.press('a');
}
if (digitalRead(leftButton) == LOW) {
Keyboard.release('a');
}
if (digitalRead(rightButton) == HIGH) {
Keyboard.press('d');
}
if (digitalRead(rightButton) == LOW) {
Keyboard.release('d');
}
}

5) Press the 'Verify' & 'Upload' buttons

6) Save your code by pressing CTRL + s on your keyboard

Step 4: Final Notes

If there are any errors while uploading the code to the Arduino, do look up on the internet to resolve your issues based on your OS

Now, you can have fun & start playing your games with your newly built homemade DIY game controller!

If you liked my project, press the 'heart' button on my project!

Till all are one!