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

by IshanDatta in Circuits > Arduino

56 Views, 1 Favorites, 0 Comments

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

DIY(1).png
IMG_20241223_210635.jpg

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

9 wires.png

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

Installation of the IDE

Screenshot_20241223_174130.png

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

The Building Part

Circuit Diagram.png
Schematic Diagram.png

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

The Coding Part

Screenshot_20241223_190328.png

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

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!