Home > Projects > Interfacing(USB – RS232 – I2c -ISP) Projects > Arduino Joystick Mouse Control Code

Arduino Joystick Mouse Control Code

Summary of Arduino Joystick Mouse Control Code


This article explains how to control a computer's onscreen cursor using an Arduino Leonardo, Micro, or Due. It utilizes a joystick for relative movement and a pushbutton to toggle mouse control on and off, with an LED indicating the active state. The code translates analog inputs into cursor movements and includes safety warnings about taking over the user's mouse.

Parts used in the JoystickMouseControl:

  • Arduino Leonardo board
  • Arduino Micro board
  • Arduino Due board
  • 2 axis joystick
  • Momentary pushbutton
  • One 10-kilohm resistor

Using the Mouse library, you can controls a computer’s onscreen cursor with an Arduino Leonardo, Micro, or Due. This particular example uses a pushbutton to turn on and off mouse control with a joystick.

Arduino Joystick Mouse Control

Cursor movement from the Arduino is always relative. So every time the analog input is read, the cursor’s position is updated relative to it’s current position.

Two analog inputs ranging from 0 to 1023 are translated to ranges of -12 to 12. The sketch assumes that the joystick resting values are around the middle of the range, but that they vary within a threshold.

The pushbutton allows you to toggle mouse control on and off. A status LED connected to pin 5 lights when the Arduino is controlling the mouse.

Software Required

  • none

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Schematic:

Arduino Joystick Mouse Control Schematic

Code

/*
JoystickMouseControl

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

Hardware:
* 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

*/
Major Components in Project

Hardware Required

  • Arduino Leonardo, Micro, or Due board
  • 2 axis joystick
  • momentary pushbutton
  • One 10-kilohm resistor (if needed)

For more detail: Arduino Joystick Mouse Control Code

Quick Solutions to Questions related to JoystickMouseControl:

  • Which Arduino boards support this project?
    The project works with the Arduino Leonardo, Micro, or Due board.
  • How does the cursor move in this sketch?
    Cursor movement is always relative, updating the position based on the current location.
  • What range do the analog inputs translate to?
    The sketch translates analog inputs ranging from 0 to 1023 into ranges of -6 to 6.
  • Can I turn mouse control on and off?
    Yes, a pushbutton allows you to toggle mouse control on and off.
  • How can I tell if the Arduino is controlling the mouse?
    A status LED connected to pin 5 lights up when the Arduino is controlling the mouse.
  • What happens when the Mouse.move command is used?
    The Arduino takes over your mouse, so you must ensure you have control before using it.
  • Do I need any software required for this project?
    No software is required other than the standard Arduino environment.
  • Where are the pushbuttons connected in the schematic?
    The pushbuttons are connected to pin D2 and D3.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top