Source: https://mokxi.com/libraries/joystick
Updated: 2026-11-02

Libraries / Communication
Arduino library · Communication

# The Arduino Joystick Library: make a USB game controller

Matthew Heironimus’s Arduino Joystick Library makes a Leonardo, Micro or Pro Micro show up on the computer as a game controller, with no driver. It is what most DIY fight sticks, hit boxes, button boxes, flight panels, pedals and sim-racing shifters are built on, because the ATmega32U4 can be a USB device itself.

Open the example in the editor See the calls

Built in on Mokxi: #include <Joystick.h> and it builds, with nothing to install. Free, and no account needed.

## What Arduino Joystick Library does

You describe the controller you want in the Joystick_ constructor: how many buttons (up to 32), how many hat switches (up to 2), and which axes exist, from X and Y through the rudder, throttle, brake and steering. The library builds the USB descriptor to match, so the computer’s game controller settings show exactly those inputs.

Each axis takes values in a range you set with setXAxisRange() and the like, 0 to 1023 by default to match analogRead(). By default every change is sent at once; begin(false) holds the changes until sendState(), which sends one report per pass of loop() however many buttons changed.

Library Manager name | Arduino Joystick Library

Author | Matthew Heironimus

Include | #include <Joystick.h>

Boards | Arduino Leonardo, SparkFun Pro Micro, Arduino Micro

In the simulator | Runs, with a gap the note below names.

## Install it

It builds on Arduino Leonardo, SparkFun Pro Micro, Arduino Micro.

In Mokxi it is built in: #include <Joystick.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Arduino Joystick Library" by Matthew Heironimus from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Arduino Joystick Library to lib_deps in PlatformIO. Mokxi follows the calls of release 2, so a sketch written here builds there too.

What to know in the simulator: There is no computer on the other end: the reports are shown on Serial.

Arduino Leonardo · SparkFun Pro Micro · Arduino Micro

## The calls you will use

In the order a sketch meets them.

### Joystick_ Joystick;

A controller with every input the library offers: 32 buttons, 2 hats and all the axes. Most sketches pass arguments to ask only for what they have.

### Joystick.begin()

Starts the controller. begin(false) turns off automatic sending so the sketch calls sendState() itself.

### Joystick.setButton(0, value)

Sets button 0 (counted from zero) to pressed (1) or released (0).

### Joystick.setXAxis(analogRead(A0))

Sets the X axis within its range; setYAxis(), setZAxis(), setThrottle() and the rest work the same way.

### Joystick.setXAxisRange(0, 1023)

Sets the values that mean full left and full right, for a sensor that does not read 0 to 1023.

### Joystick.setHatSwitch(0, 90)

Sets a hat (the d-pad) in degrees: 0 up, 90 right, 180 down, 270 left, -1 released.

### Joystick.pressButton(3)

Presses one button; releaseButton(3) lets it go.

### Joystick.sendState()

Sends the current state of everything in one report, used with begin(false).

## Example: Dance pad on a Pro Micro

On the SparkFun Pro Micro. It runs in the browser exactly as printed.

sketch.ino · SparkFun Pro Micro

Open it in the editor, wired and ready to run · Dance pad on a Pro Micro: the project page

## Common errors and how to fix them

### 'Joystick_' does not name a type

The library is not installed or the include is missing. Install "Joystick" by Matthew Heironimus (it is on GitHub rather than in every Library Manager list) and put #include <Joystick.h> at the top.

### The controller shows up but buttons light up the wrong numbers

Buttons count from 0 in the sketch and usually from 1 in the computer’s controller settings, so button 0 shows as button 1. Map them in the game rather than renumbering the wiring.

### Joystick.h will not build for an Uno or Nano

Only boards whose chip is its own USB device can be a game controller. Use a Leonardo, Micro or Pro Micro (ATmega32U4).

### Inputs feel laggy when many buttons change at once

With automatic sending on, every setButton() sends a report. Call Joystick.begin(false), set everything, then call sendState() once per loop().

## Projects that use Arduino Joystick Library

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Flight stick and throttle on a Pro Micro A two-axis stick with a center deadzone and an expo curve, a twist rudder, a hall sensor throttle, a hat switch and two buttons, sent as one Joystick.h report. Guitar controller on a Pro Micro Five fret buttons, a two-switch strum bar, a whammy knob and a tilt switch for star power, sent as a gamepad report through Joystick.h. Sim racing button box Two encoders, an ignition key with a start button, three toggles, four buttons and three LEDs on a Pro Micro, sent to the game as one USB controller. Load cell pedals on a Pro Micro A load cell brake on an HX711 that responds to force, a hall sensor throttle and a pot clutch, each calibrated and sent as one Joystick.h report. H-pattern shifter and handbrake Two pots read an H-pattern gate with hysteresis and push-down reverse, two microswitches make sequential paddles and a hall sensor is the handbrake, with the gear on a display. Autopilot and landing gear panel An autopilot knob for heading, altitude and vertical speed on a four-digit display, an AP button, and a gear lever with three green lights and a red one in transit.

## Keep going

### Learn

- Design Your Own Fight Stick or Leverless Controller
- Leverless Controller Build: Buttons, Pins and Code
- DIY Rhythm Game Controller: Guitar and Dance Pad

### More communication libraries

- OneWire
- TinyGPS
- TinyGPSPlus
- MFRC522
- SoftwareSerial
- AltSoftSerial
- i2cdetect
- Keyboard

Every built-in library · Open the editor

## Try Arduino Joystick Library without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
