Introduction: Control Your Computer With Arduino and a Joystick

This tutorial will teach you about potentiometers and buttons in interfacing with an arduino. In addition you will learn about some commands that will let you control the cursor on your computer using those components.

NOTE: This will only work with the Arduino Micro and Leonardo

Step 1: Materials

Most importantly you'll need a joystick, this can be salved out of most any console controller.
An Arduino, in this tutorial we'll be using a micro
Ribbon cable will make this look cleaner, and make the tutorial easier to follow, but any wire will work.
Something to mount the joystick on to make it easier to handle
One 220ohm resistor (pull down resistor, this is just a value I had on hand.)

Soldering iron

Step 2: Let's Establish Which Wires Are Which

The joystick has two potentiometers and a button on it. We will be using all the pins on the potentiometers, and two pins on the buttons. Each of the pots has 3 pins: vin, signal, and ground. The button has two sets of pins, the pins on the same side are connected when the button is depressed. It doesn't matter which you pick as pin 1 or 2.

Pot 1 (for the x direction, left and right)
1 - 5v
2 - A0
3 - GND

Pot 2 (for the y direction, up and down)
1 - 5v
2 -A1
3 - GND

1 - Directly to D2 and to 5v through the resistor
2 - GND

Step 3: Soldering

I chose to use an 8 wire wide piece of floppy drive cable and rip the ends into sections to make this easier on myself, but any wire will work, just be careful to connect them to the correct pins, else your joystick might not work or make your cursor move in the wrong direction. If you finish and it moves up when you push the joystick down or similar, check your wires. 

Step 4: The Code

Hook up your arduino to your computer, fire up the arduino program, then copy and paste this code.

A brief explanation: 
The potentionmeters feed values to the arduino based on their position. These values are used to determine the speed and direction the cursor should move. The arduino then send this information to the computer. When the button is depressed, the state of digital pin 2 changed to high, and the arduino send a command to click the mouse to the computer.



Controls the mouse from a joystick on an Arduino Leonardo or Micro.
Uses a pushbutton to turn on and off mouse control, and
a second pushbutton to click the left mouse button

* 2-axis joystick connected to pins A0 and A1
* pushbuttons connected to pin D2 and D3

The mouse movement is always relative. This sketch reads
two analog inputs that range from 0 to 1023 (or less on either end)
and translates them into ranges of -6 to 6.
The sketch assumes that the joystick resting values are around the
middle of the range, but that they vary within a threshold.

WARNING:  When you use the Mouse.move() command, the Arduino takes
over your mouse!  Make sure you have control before you use the command.
This sketch includes a pushbutton to toggle the mouse control state, so
you can turn on and off mouse control.

created 15 Sept 2011
updated 28 Mar 2012
by Tom Igoe

this code is in the public domain


// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 2;      // switch to turn on and off mouse control
const int mouseButton = 3;    // input pin for the mouse pushButton
const int xAxis = A0;         // joystick X axis 
const int yAxis = A1;         // joystick Y axis
const int ledPin = 5;         // Mouse control LED

// parameters for reading the joystick:
int range = 12;               // output range of X or Y movement
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = range/4;      // resting threshold
int center = range/2;         // resting position value

boolean mouseIsActive = false;    // whether or not to control the mouse
int lastSwitchState = LOW;        // previous switch state

void setup() {
  pinMode(switchPin, INPUT);       // the switch pin
  pinMode(ledPin, OUTPUT);         // the LED pin 
// take control of the mouse:

void loop() {
  // read the switch:
  int switchState = digitalRead(switchPin);
  // if it's changed and it's high, toggle the mouse state:
  if (switchState != lastSwitchState) {
    if (switchState == HIGH) {
      mouseIsActive = !mouseIsActive;
      // turn on LED to indicate mouse state:
      digitalWrite(ledPin, mouseIsActive);
  // save switch state for next comparison:
  lastSwitchState = switchState;

  // read and scale the two axes:
  int xReading = readAxis(A0);
  int yReading = readAxis(A1);

  // if the mouse control state is active, move the mouse:
  if (mouseIsActive) {
    Mouse.move(xReading, yReading, 0);

  // read the mouse button and click or not click:
  // if the mouse button is pressed:
  if (digitalRead(mouseButton) == HIGH) {
    // if the mouse is not pressed, press it:
    if (!Mouse.isPressed(MOUSE_LEFT)) {;
  // else the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_LEFT)) {


  reads an axis (0 or 1 for x or y) and scales the
analog input range to a range from 0 to <range>

int readAxis(int thisAxis) {
  // read the analog input:
  int reading = analogRead(thisAxis);

  // map the reading from the analog input range to the output range:
  reading = map(reading, 0, 1023, 0, range);

  // if the output reading is outside from the
  // rest position threshold,  use it:
  int distance = reading - center;

  if (abs(distance) < threshold) {
    distance = 0;

  // return the distance for this axis:
  return distance;

Step 5: You're Done!

Now you have your very own joystick. Some things to try:

Check your wires if the cursor moves in the wrong direction
Modify the code to change the sensitivity
Make a fighting or aircraft joystick
Use two joysticks and play an fps

Arduino Contest

Participated in the
Arduino Contest