Introduction: Create a Joystick Using the Arduino Joystick Library 2.0
Since I released the original Arduino Joystick Library (see https://www.instructables.com/id/Arduino-LeonardoMicro-as-Game-ControllerJoystick/ for more details) I have received numerous requests for enhancements. Most of these requests fall into the following two categories:
- Increase the precision of the axes.
- Make a version with only a specified set of features.
To accommodate these requests (and a few others) I have release Version 2.0 of the Arduino Joystick Library.
Step 1: Arduino Joystick Library 2.0
Out of the box the Arduino Leonardo and the Arduino Micro appear to the host computer as a generic keyboard and mouse. This article discusses how the Arduino Leonardo and the Arduino Micro can also appear as one or more generic Game Controllers or Joysticks. The Arduino Joystick Library Version 2.0 can be used with Arduino IDE 1.6.6 (or above) to add one or more joysticks (or gamepads) to the list of HID devices an Arduino Leonardo or Arduino Micro (or any Arduino clone that is based on the ATmega32u4) can support. This will not work with Arduino IDE 1.6.5 (or below) or with non-32u4 based Arduino devices (e.g. Arduino UNO, Arduino MEGA, etc.).
Step 2: Features
The joystick or gamepad can have the following features:
- Buttons (default: 32)
- Up to 2 Hat Switches
- X, Y, and/or Z Axis (up to 16-bit precision)
- X, Y, and/or Z Axis Rotation (up to 16-bit precision)
- Rudder (up to 16-bit precision)
- Throttle (up to 16-bit precision)
- Accelerator (up to 16-bit precision)
- Brake (up to 16-bit precision)
- Steering (up to 16-bit precision)
These features are configured using the Joystick_ class’s constructor.
Step 3: Installation
The latest build of Version 2.0 of the Arduino Joystick Library can be downloaded from the following GitHub repository:
https://github.com/MHeironimus/ArduinoJoystickLibrary/tree/version-2.0
The library can also be downloaded directly using the following: https://github.com/MHeironimus/ArduinoJoystickLibrary/archive/version-2.0.zip
Copy the Joystick folder to the Arduino Libraries folder (typically located at %userprofile%\Documents\Arduino\libraries on Microsoft Windows machines). On Microsoft Windows machines this can be done by executing deploy.bat. The library should now appear in the Arduino IDE list of libraries.
Step 4: Included Examples
The example Arduino sketch files listed below are included in this library. These will appear in the Arduino Example menu when the Arduino Joystick Library is installed.
JoystickTest – Simple test of the Joystick library. It exercises many of the Joystick library’s functions when pin A0 is grounded.
MultipleJoystickTest - Creates 4 Joysticks using the library and exercises the first 16 buttons, the X axis, and the Y axis of each joystick when pin A0 is grounded.
JoystickButton - Creates a Joystick and maps pin 9 to button 0 of the joystick, pin 10 to button 1, pin 11 to button 2, and pin 12 to button 3.
JoystickKeyboard - Creates a Joystick and a Keyboard. Maps pin 9 to Joystick Button 0, pin 10 to Joystick Button 1, pin 11 to Keyboard key 1, and pin 12 to Keyboard key 2.
GamepadExample - Creates a simple Gamepad with an Up, Down, Left, Right, and Fire button.
DrivingControllerTest - Creates a Driving Controller and tests 4 buttons, the Steering, Brake, and Accelerator when pin A0 is grounded.
FlightControllerTest - Creates a Flight Controller and tests 32 buttons, the X and Y axis, the Throttle, and the Rudder when pin A0 is grounded.
HatSwitchTest - Creates a joystick with two hat switches. Grounding pins 4 - 11 cause the hat switches to change position.
Step 5: Running the JoystickTest Example
The JoystickTest example sketch is included with the library. I recommend using this example to verify everything is working properly before beginning to write your own sketch files. Load, compile, and upload this example sketch file to an Arduino Leonardo or Micro using the Arduino IDE (version 1.6.6 or above).
Once you have uploaded the JoystickTest sketch file to the Arduino Leonardo or Micro, perform the following steps to verify everything is working properly. Note: the following steps are for Windows 10. If you have a different version of Windows or a different operating system, these steps may differ.
Open the “Devices and Printers” window. This can be done by clicking the Start menu or pressing the Windows Key and typing “Devices and Printers”.
Step 6: Arduino Settings Menu
The Arduino Leonardo or Arduino Micro should appear in the list of devices.
Right mouse click on the Arduino Leonardo or Arduino Micro
to display the settings menu.
Select “Game controller settings” to get to the “Game
Controllers” dialog.
Step 7: Select the Arduino
The Arduino Leonardo or Micro should appear in the list of installed game controllers. Select the Arduino Leonardo or Micro and click the Properties button to display the game controller test dialog.
Step 8: Testing the Arduino Joystick
While this dialog has focus, ground pin A0 on the Arduino to activate the test script.
The test script will test the game controller functionality in the following order:
- 32 buttons
- throttle and rudder
- X and Y Axis
- Z Axis
- 2 Hat Switches
- X, Y, and Z Axis Rotation
Step 9: Simple Gamepad Example - Hardware
Once the Arduino Leonardo or Micro has been tested using the JoystickTest example, I suggest making a simple gamepad controller. You will need five buttons to build this simple example. Each button will correspond to one of the following joystick functions: up, down, left, right, and fire.
Connect one end of each button to the ground pin of the Arduino. Connect the other end of each button as indicated below:
- Up Button => Pin 2
- Right Button => Pin 3
- Down Button => Pin 4
- Left Button => Pin 5
- Fire Button => Pin 6
Step 10: Simple Gamepad Example - Sketch File
Upload the GamepadExample example sketch file to the Arduino Leonardo or Micro. This example is included with the Arduino Joystick Library.
Step 11: Simple Gamepad Example - Testing
Open the game controller properties or use the joystick testing application of your choice to test the behavior of your gamepad.
Step 12: For More Information
More information about the Arduino Joystick Library Version 2.0, including the complete API documentation, can be found at https://github.com/MHeironimus/ArduinoJoystickLibrary/tree/version-2.0.
74 Comments
1 year ago
I'm interested in adapting the Joystick library to accept the X, Y and Z axis outputs from an ADXL 345 Three Axis Accelerometer. It's output are three values, representing the force of gravity, expressed in meters per second per second. I'm looking to map those values - which are basically -10 to +10, to a value between -512 and +512, for a total range of 1024, which would mimic the output of a potentiometer in an analog joystick. I know that I can use the Arduino Map function to do the mapping, but am unsure how to modify the Arduino code or Joystick Library to send that mapped value, using the Joystick.sendState(); command. I'm using an Arduino Leonardo with a custom built aircraft yoke. My mechanical design skills are much better than my coding skills. Any suggestions would be welcome.
Reply 4 months ago
I am also interested in a solution to this but with an MPU6050. Sorry I don't have the answer
Question 6 months ago
Will esp8266 nodemcu work with this ? If not can I buy https://robu.in/product/pro-micro-5v-16m-mini-leon...
My intention is to make couple of toggle switches for Microsoft Flight Simulator using https://robu.in/product/5a-3-pin-spdt-toggle-switc...
Any other electronic component needed other than Arduino Micro and Switch?
Answer 6 months ago
I do not think it will work with a esp8266 nodemcu. It should work with the Pro Micro 5V 16M Mini Leonardo Microcontroller Development Board For Arduino.
Question 1 year ago
I would like to think I am not a complete noob, but the "Joystick_ Joystick" is throwing an error for me. It says " 'Joystick_' does not name a type; did you mean 'Joystick'? "
Answer 11 months ago
I had the same issue. It turns out there was other libraries with similar names in my arduino library folder. Deleting them fixed the issue. Basically it's a name collision issue.
Reply 7 months ago
The issue I have is with the latest version uploaded to my Arduino Leonardo into a encoder that works find in serial monitor but no gamepad is showing at all. I have spent many hours on this to no avail. My usual expert also doesn't understand. Any ideas.
7 months ago on Introduction
Hi Matt! I need a small application of your code for a specific project...do you have any time for paid moonlighting? Curtis
Projecteinsteinvr @ gmail
Com
Question 7 months ago on Introduction
Thanx for all the great looking write ups but I am such a Noobi I don't know how to wire up the pots to the Micro Pro
Question 9 months ago
I need ckt 32 buttons 2 joystick
1 year ago
Hi Matthew,
I am building a B737 Throttle and I need to turn on a LED Parking Brake Light when I set the parking brake switch. How would you turn on a pin for the output with your joystick program. I know diddly squat about programing.
Best Regards,
Dale Reitz
Reply 9 months ago
Look up a GitHub project for the blackberry mini trackball using the arduino pro micro. It has some code in it that turns the light in the trackball on and off if you’re pressing down on it (a left click). That might give you a place to start.
Question 1 year ago
Why only Leonardo and micro? is it because of the amount of pins? can I use an elegoo?
Answer 1 year ago
No, the restriction is due to the chip on the board. AFAIK, the ATMEGA32u4 is the only arduino chip to feature full HID (Human Input Device) support.
Answer 1 year ago
I've forgotten to say I use an elegoo Uno.
It's pretty much the same as an arduino uno and even shares the driver
Question 1 year ago on Step 10
I am completely new to coding with arduino. I have done one project before. Is it possible for you to send me or release the rest of the code? This way I can look at it and get an idea of what I need to do to code my project.
Answer 1 year ago
The source code for this is included when you install the library into the Arduino IDE. You can find it under Examples -> Joystick -> GamepadExample.
You can find other various examples at https://github.com/MHeironimus/ArduinoJoystickLibrary/wiki/Examples.
Reply 1 year ago
Thank you!
1 year ago on Step 12
There is another option. Instead of using this library that only runs on the Leonardo or Micro, you can produce PPM signals in any Arduino board and feed them into the mic input of the audio board. IIt produces 8 channels out of the box but can be modified for more. You previously downloaded and installed SmartPropoPlus which is free and when running it creates a virtual joystick (vJoy) that can be later configured as the input controller to any app that requires it (flight sims, etc). I use it with the RC simulator.
Question 2 years ago on Introduction
searching the web I have not found a project that implements the 8 axes, the 32 buttons and the 2 hat swich. I don't think it's possible to get all this in an arduino leonardo because they wouldn't give the pins and I would have to use a multiplexer to achieve it