Introduction: Connect a USB Mouse to Your Arduino

About: 55+ years in electronics, computers, and teaching ... now retired.

A mouse allows you to control objects by detecting mouse-movement and mouse-button presses.

This instructable explains how to connect a USB mouse to an Arduino UNO R3 microcontroller with the aid of an Arduino USB Host Shield.

Construction is simple ... solder three jumpers and plug the USB Host Shield into your Arduino.

Sample code is provided.

The cost of this project is less than US $15 excluding the mouse and Arduino.

Photo

  • The opening photo shows a USB Host shield connected to an Arduino. The USB mouse is plugged into the top connector.

Step 1: Parts List

The following part was obtained from https://www.aliexpress.com/

  • 1 only USB Host Shield 2.0 for Arduino UNO / MEGA

The following parts were already on hand:

  • 1 only Arduino UNO R3 + USB CABLE
  • 1 only USB mouse

The cost of this project is less than US $15 excluding the mouse and Arduino

Step 2: Theory

Arduinos require a USB Host Shield before they can talk to USB keyboards, mice and thumb-drives.

The purpose of a USB host is to initiate the bus communications ... the function of a device is to provide/accept information.

Two things are required before the USB Host Shield will work:

  • it needs a USB software library
  • it needs three solder bridges to apply the correct voltages to the mouse and onboard chips, some of which are 3 volts.

Reference Information

Step 3: Configuring the USB Host Shield

The USB host shield carries a mix of 3 volt and 5 volt chips. All power is obtained from the Arduino.

A standard 5 volt mouse or keyboard requires solder bridges across each of the three links circled in green.

Plug the USB Host Shield into your Arduino once this has been done.

Step 4: Software

The host shield requires USB Host Shield Library 2.0

To install this library:

  • left-click “Sketch/Libraries/Manage Libraries” in your Arduino IDE and type USB Host in the search box. A list of possible libraries will eventually appear ...
  • select “USB Host Shield Library 2.0” and left-click install.

A list of example files may be found in your .../Arduino/libraries/USB_Host_Shield_2.0-master/examples/HID folder.

In the attached file USBHIDBootMouse_LINGIB.ino I have modified the USBHIDBootMouse.ino example file to keep track of how far the mouse has moved.

To install

  • Download the attached file USBHIDBootMouse_LINGIB.ino
  • Copy the contents to an Arduino sketch ... use a text editor NOT a word processor.
  • Save the sketch as USBHIDBootMouse_LINGIB
  • Now compile and upload the code to your Arduino.
  • Open your Arduino Serial Monitor
  • Set the baud speed to 115200
  • You should now see numbers scroll down your Serial Monitor when you move your mouse.
  • Any mouse clicks will also be displayed

Step 5: Results

Photo 1 shows the X-axis and Y-axis counts being summed in the right hand columns

The mouse counters change around 43 counts per mm.

Unfortunately the counters for my mouse do not return to zero when the mouse is moved back and forth between physical stops. I suspect the reason for this is that counts are being lost while the motion detect software kicks in.

Losing counts isn't a problem when using the mouse as a pointer but is bad news when tracking absolute locations.

The mouse buttons are equally easy to intercept ... just put your code in the appropriate subroutine.

Step 6: Summary

This instructable explains how to connect a USB mouse to an Arduino Uno R3 with the aid of an Arduino Host Shield.

The instructable also explains how to configure the USB Host for use with 5 volt devices such as keyboards, mice and thumb drives.

Demo code is provided that recognises mouse clicks and tracks how far the mouse has moved.

I found that my mouse is okay for use as a pointing device but is unsuitable for tracking absolute distance travelled as it loses a few counts each time the mouse wakes up

Mouse sensitivity is approximately 43 counts per millimeter movement.

Excluding the mouse and Arduino, the cost of this project is less than US $15 dollars

  Click here   to view my other instructables.