Create a Joystick Using the Arduino Joystick Library 2.0

125,702

189

61

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.

2 People Made This Project!

Recommendations

  • Battery Powered Contest

    Battery Powered Contest
  • Plywood Challenge

    Plywood Challenge
  • Plastic Contest

    Plastic Contest

61 Discussions

0
DaleReitz
DaleReitz

1 day 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

0
DwmFrancis
DwmFrancis

4 days 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.

IMG_0323.jpegIMG_0367.jpeg
0
pinkmilkgirl
pinkmilkgirl

Question 27 days 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.

0
pinkmilkgirl
pinkmilkgirl

Reply 20 days ago

Thank you!

0
rva19451
rva19451

27 days 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.

0
xp7777777
xp7777777

Question 10 months 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

0
NeilP43
NeilP43

Answer 8 weeks ago

Could be useful if you're getting your inputs from something other than direct connections, for example using an SBUS library to read inputs from an RC controller with 16 channels.

0
junior.camj
junior.camj

Question 2 months ago on Introduction

Hello!!

Is it possible to make this game controller recognized by PS4?
Or how to make an arduino joystick keyboard to play on PS4?
0
MattS501
MattS501

Question 9 months ago on Introduction

Hi, I'm utilizing the joystick library in a project I'm emulating from online at this link: https://www.youtube.com/watch?v=Z7Sc4MJ8RPM. My specific box has 4 rotary encoders and an additional 21 buttons. The way amstudio has the sketch setup, each encoder takes 2 buttons for CW and CCW rotation. Add to that the push button function of each encoder and the other 21 buttons/switches I have in play, and I now have 33 buttons to map. I see in the instructable that this library is defaulted to 32 buttons and I've tried changing the argument to 33, but I only ever see 32 of them show up in testing. Is it possible to have more than 32 buttons? How? All of my button pushes are wired in a 5x5 matrix to a micro pro, with each of the encoders wired to ground and two additional pins each for the rotation. I've been able to reassign that mapped buttons, but can't seem to get more than 32.

0
difinov
difinov

Answer 2 months ago

Hi Matt,
i had to manage the same (not)problem. I finally discovered that directx can manage up to 128 buttons and that the fact that you see just 32 buttons in windows device manager is just a Windows visualization limitation. You don't see the 33rd button but the system does. If you use a joystick test application like this http://www.planetpointy.co.uk/joystick-test-application/ you will realize all the buttons are there :-)

1
mmoghimi13
mmoghimi13

Question 1 year ago

Hi,

I have used your library to make a joystick, with 8 axes and 12 buttons. It works fine but the Throttle axis never shows up. If I disable the Rudder axis, then the Throttle shows up. Also if I turn all axes on I still see the same 7 axes (see attached). In another word if I activate more than 7 axes I only see 7!!!

Can you please help me out with this.

Best

1.PNG1-2.PNG
0
bandstreet8
bandstreet8

Answer 2 months ago

Hi bro. How I can disable the rudder axis?
I found library to make a joystick with 8 axis and 32 buttons.

0
EtienneD23
EtienneD23

Question 1 year ago

Hello,

How are you getting 16 bit resolution for the analog inputs since the board is only capable of 10 bit resolution?

Are you just padding the last 6 bits with zeros?
Are you oversampling?

Thank you,
E.

0
AlexL305
AlexL305

Answer 3 months ago

Also curious about this, haven't looked at the code yet though. Even if padding with zeros, 10bit resolution is quite fine and already a big step up from most off-the-shelf game controllers / joysticks. Except for the more enthusiast oriented joysticks they only use 8 bit resolution. You need hall sensors or good pots & gimbals to take advantage of 10 bit and greater resolution anyway, the cheap gimbals used in most game controllers and sold as Arduino modules are very imprecise, 8 bit is enough for them. Gimbals from a proper RC transmitter are precise enough though, companies like Frsky sell them as replacement parts.

0
Vergeten-65
Vergeten-65

11 months ago

Hi Matthew, i hope you can help me.

I have placed 5 momentum switches with resistors (parallel) on pin A11
and read this with the sketch below that I found on the internet and
adapted to my situation. In the serial monitor I can read the correct
numbers of the switches. Now, however, I want to see those switches in
the computer as joystick switches 21 to 25.

I have spent 2 days searching and trying but I can’t get it working. I
try to understand your Joystick Library API, but I still get
stuck. The variable in my sketch "pressed_button" therefore indicates the switch number and I want to be able to read that switch number as a joystick switch in the computer.
Just to be clear: I am a beginner with Arduino.

Hopefully you can help me further?



——————————————————–

#include <joystick.h>


Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,

25, 0, // Button Count, Hat Switch Count

false, false, false, // X and Y, or Z Axis

false, false, false, // Rx, Ry, or Rz

false, false, // rudder or throttle

false, false, false); // accelerator, brake, or steering

int old_button = 0;

int button;

int pressed_button;

int rs; //Resistor Switch

void setup () {

Serial.begin(9600);

pinMode(A11, INPUT);

}

void loop () {

rs = analogRead(A11);

delay(50);

if (rs > 1021) button = 0;

else if (rs > 680 && rs 840 && rs 920 &&
rs 960 && rs 1000 && rs < 1010) button = 25;

else button = 0;

if (old_button == button) {

old_button = button;

pressed_button = 0;

}

else {

old_button = button;

pressed_button = button;

Joystick.pressButton(pressed_button);

}
Serial.println(pressed_button);

delay(100);

}

——————————————————–

Thanks

Fer

0
LurcherK981
LurcherK981

Question 1 year ago on Step 1

Hi Matthew great work your doing here I've made a joystick with 10 buttons using your library which works great however ide like to be able to use that 1 joystick as 2 joysticks if that makes sence by adding a switch or somthing to switch between them I've seen it done but can't find any info on how to code it or wire it I'm a newbie with coding so any help would be great thanks

0
JózsefK16
JózsefK16

Answer 1 year ago

Hy LurcherK981!

Please help me i will made a similar gamepad as you made.
Can you help me with code?
thanks
Jozsef

0
MKLele
MKLele

Question 1 year ago

Can I change device name seen in gamepads menu? (Device name that will be saved on arduino not windows)

0
mauribmxponce
mauribmxponce

1 year ago

Hello, I am new to this arduino, but I have a certain base in electronics and very simple programming, I am currently designing a steering wheel for PC, using your library I have been able to create a code that handles 4 potentiometers such as axles, clutch, brake, accelerator and hand brake, I also have a rotary encoder to use on the steering wheel, and 8 microswitch inputs for the gearbox. but I have a problem, if I use a potentiometer for the steering wheel it recognizes me in almost all games (DIRT, F1, ASSETO CORSA, PROJECT CARS, RFACTOR, AUTOMOBILISTA, ETC) but if I use the rotary encoder on the steering wheel, I only know it recognizes a game, LFS, in the other games when I want to assign the direction axis of the encoder does not recognize me, is there any function or special code for the encoder to be recognized as a potentiometer?