Beginner's Guide To Make A Game Controller
Beginner's Guide To Make A Game Controller
Beginner's Guide To Make A Game Controller
2
ARDUINO IDE BASICS ......................................................................................................... 3
ARDUINO LIBRARIES .......................................................................................................... 9
EXAMPLES ......................................................................................................................... 11
SINGLE BUTTON KEYBOARD........................................................................................ 12
MULTIPLE BUTTON KEYBOARD ................................................................................... 13
MULTIPLE BUTTON MOUSE ......................................................................................... 14
JOYSTICK WSAD MOVEMENT ....................................................................................... 15
JOYSTICK MOUSE MOVEMENT..................................................................................... 16
NUNCHUK WSAD MOVEMENT AND BUTTONS .......................................................... 17
NUNCHUK MOUSE MOVEMENT AND BUTTONS ....................................................... 18
REFRENCE ......................................................................................................................... 19
1
How to get the most out of this guide.
The Beginner’s Guide to Arduino Game Controllers is a summary of all the material I’ve
learned through researching my own projects. This guide is intended to teach you the basic
fundamentals of the Arduino IDE software and introduce you to the free resources
available on my website insertcontrollerhere.com. There is a lot of information to cover
for a beginner, so take your time with each section. This is an interactive guide, all red text
contain links to sources, downloads, and recommended parts. Any materials purchased
through these links helps support the channel at no additional cost to you and allows me
to produce more free content. Please consider clicking these links before making any
online purchases.
If you have never touched an Arduino before, the introductory tutorial will walk you
through writing, uploading, and executing for first program. These are the fundamentals
required to utilize the example sketches I have provided for making game controllers.
For those that already have Arduino experience, check out the recommended components
and example code to get started right away!
You will need to have an Arduino board to complete the next section of the guide.
2
Download the Arduino IDE software.
The Arduino IDE is the open-source software that you will use to write code and upload it
to your Arduino board. The software is free to download by clicking the button below.
Downloads are available for Windows, Mac, and Linux.
3
Write a basic program.
Let’s begin with a simple sketch that prints the phrase “Hello World.” In the loop section of
the sketch, write the following lines of code (lines 8 and 9):
Serial.println("Hello World");
delay(1000);
4
Select your board type.
You must select your board type before you can communicate with your connected
Arduino board. For the board type, go to Tools → Board → Select your board type
(Leonardo or Micro).
5
Verify the sketch.
Your code must be compiled/verified before it can be sent to the Arduino. Compiling code
will check for logical errors. Any found errors will be listed in the console window below
your code. Click the verify button. You will know the sketch was successfully compiled if you
see “Done compiling” in above the console window. If you run into any errors, check your
code again to ensure you copied it correctly.
6
Check the serial monitor.
The Arduino can be programmed to send useful information to the serial monitor using the
Serial.println() function. To open the serial monitor, go to Tools → Serial Monitor. For
this example, you will know that the program is successfully running if you see the phrase
“Hello World” appear once every second.
7
Congratulations!
You have successfully written, uploaded, and executed your first Arduino sketch! Once you
feel comfortable with the basics of Arduino and have browsed some of the resources
available on my website, it’s time to start playing around with example controller sketches!
This is where the real fun begins.
8
How to install an external library.
Libraries are extensions that add additional functionality to your sketch. All of my example
programs contain libraries that are required to function. Several libraries are included with
the IDE, but externally developed libraries can be installed via .ZIP files. External libraries
can be installed under Sketch → Include Library → Add .ZIP Library.
Default Libraries
These libraries are built into the IDE and do not require installation.
KEYBOARD
This library enables you to send keystrokes to a connected computer. The library is
essential to making controllers and is used in every program I make. This includes the
Keyboard.press() and Keyboard.release() functions.
MOUSE
This library enables you to send mouse commands to a connected computer. This includes
the Mouse.move(), Mouse.press(), and Mouse.release() functions.
WIRE
This library enables you to communicate with I2C devices over the SDA (data) and SCL
(clock) pins. Wire.h is included with IDEA and does not require installation. I2C devices that I
commonly used for game controllers include the MPU-6050 and the Wii Nunchuk.
9
External Libraries
These libraries must be downloaded and installed.
MPU-6050
Author: Jeff Rowburg - GitHub Link
This library is essential for using the MPU-6050 sensor. The library enables easy readings
for the MEMS accelerometer and MEMS gyroscope to find the position and acceleration
along the XYZ axes. Communication uses the I2C bus through the SCL and SDA pins.
This library enables simplified input readings from a wide selection of Nintendo extension
controllers; The most important of which is the Wii Nunchuk. The library provides data for
the joystick, C and Z buttons, and the built-in accelerometer. Communication uses the I2C
bus through the SCL and SDA pins.
HID BUTTONS
Author: David Madison - GitHub Link
This library greatly simplifies the code required to track the use of buttons and joysticks on
your controller. This removes the need to have statues variables and if statements to avoid
button spamming for every loop of the program cycle.
10
All of my example projects are free to download on my website. These are the
essential building blocks to my controller designs. To make these as user friendly as
possible, I broken the code down into sections and provided comments on as many
important lines as possible. This should make these examples easier to understand,
modify, and combine into your own designs.
If you are new to Arduino, take your time with these examples and be patient. This
is a collection of concepts that took me months of researching and practice to
understand. The projects progressively increase in difficulty, so make sure to
understand all the components and functions before moving on. Utilize the
reference section at the end of this guide to look up any syntax or functions you do
not understand. If you run into any issues, do not hesitate to ask for help on our
community Discord chat. We have members that are more than willing to assist
with people of varying skill levels.
11
Project Description:
This example shows how to emulate a key press with a single button.
Source Code:
Example Code Downloads – Single Button Keyboard
Parts List:
1. Arduino Leonardo
2. Micro USB Cable
3. Breadboard
4. Jumper Wire
5. Push Buttons
Required Libraries:
1. Keyboard.h (Included)
2. HID_Buttons.h (Download)
Wiring Diagram:
12
Project Description:
This example shows how to emulate key presses with multiple buttons. You do not
need all 10 buttons for this example, but the code does show you how 10 buttons
would funtion at once.
Source Code:
Example Code Downloads – Multiple Button Keyboard
Parts List:
1. Arduino Leonardo
2. Micro USB Cable
3. Breadboard
4. Jumper Wire
5. Push Buttons
Required Libraries:
1. Keyboard.h (Included)
2. HID_Buttons.h (Download)
Wiring Diagram:
13
Project Description:
This example shows how to move the mouse and emulate the left, right, and center
mouse click with buttons.
Source Code:
Example Code Downloads – Multiple Button Mouse
Parts List:
1. Arduino Leonardo
2. Micro USB Cable
3. Breadboard
4. Jumper Wire
5. Push Buttons
Required Libraries:
1. Mouse.h (Included)
2. HID_Buttons.h (Download)
Wiring Diagram:
14
Project Description:
This example shows how to emulate the WSAD movement keys with a joystick.
Source Code:
Example Code Downloads – Joystick Keyboard Movement
Required Libraries:
1. Keyboard.h (Included)
2. HID_Buttons.h (Download)
Wiring Diagram:
15
Project Description:
This example shows how to emulate mouse movement with a joystick.
Source Code:
Example Code Downloads – Joystick Keyboard Movement
Required Libraries:
1. Mouse.h (Included)
2. HID_Buttons.h (Download)
Wiring Diagram:
16
Project Description:
This example shows how to use the Wii Nunchuk joystick for WSAD movement and
the C and Z buttons for key presses.
Source Code:
Example Code Downloads – Nunchuk WSAD Movement and Buttons
Required Libraries:
1. Keyboard.h (Included)
2. HID_Buttons.h (Download)
3. NintendoExtensionCtrl.h (Download)
Wiring Diagram:
17
Project Description:
This example shows how to use the Wii Nunchuk joystick for mouse movement and
the C and Z buttons for key presses.
Source Code:
Example Code Downloads – Nunchuk Mouse Movement and Buttons
Required Libraries:
1. Keyboard.h (Included)
2. Mouse.h (Included)
3. HID_Buttons.h (Download)
4. NintendoExtensionCtrl.h (Download)
Wiring Diagram:
18
This is a curated reference list to the main parts of the Arduino programming
language. While the full reference list can be found here, I shortened the list to
those components that I find most important to controller design for beginners to
understand. This is a great resource for when you read example code and do not
understand the function of certain parts.
BASIC FUNCTIONS
digitalRead() – Reads the value of a digital pin, then returns HIGH or LOW.
analogRead() – Reads the value of an analog pin, then returns a value between 0
and 1023. This corresponds with the voltage reading between 0 and max voltage
(3.3v or 5v).
delay() – Causes the program to pause for the given time in milliseconds (1/1,000 of
a second).
KEYBOARD FUNCTIONS
19
MOUSE FUNCTIONS
Mouse.click() – Emulates a single mouse left click. This can also be modified to
send right and middle clicks.
VARIABLES
bool – Variable type that stores either TRUE or FALSE. Useful for comparisons.
char – Variable type that stores a single character such as ‘a’ or ‘z’. Useful for
assigning keystrokes to buttons.
float – Variable type that can store numbers with decimal points.
SKETCH
setup() – The first function that runs when the Arduino turns on. Used to initialize
variables, pin modes, and libraries.
loop() – The loop function is the main function that repeats continuously after the
setup function and will run indefinitely while the Arduino is powered on.
20
FURTHER SYNTAX
// (comment) – Used to write comments within the program. Starting a line with
“//” will cause the Arduino to ignore any text written on that line.
CONTROL STRUCTURE
else – Provides greater controller over an “if” statement. When an “if” condition is
FALSE, the program will move on to the subsequent “else if” and “else” statements.
ARITHMETIC OPERATORS
21
COMPARISON OPERATORS
== (equal to) – Used to compare one variable or value to another. Returns TRUE if
equal.
!= (not equal to) – Used to compare one variable or value to another. Returns TRUE
if not equal.
> (greater than) – Used to compare if one variable or value is greater than the
other.
< (less than) – Used to compare if one variable or value is less than the other.
>= (greater than or equal to) – Used to compare if one variable or value is greater
than or equal to the other.
<= (less than or equal to) – Used to compare if one variable or value is less than or
equal to the other.
BOOLEAN OPERATORS
! (logical not) – TRUE if the operand is FALSE and FALSE when the operand is TRUE.
22