Como obter entrada do mouse USB no Arduino

How to get USB mouse input on Arduino

Generally, Arduino boards do not operate as USB hosts; they don't have the peripheral nor the RAM to do this. However, it is possible to convert Arduino boards like UNO and Mega2560 into a USB host using an Arduino USB host shield. With a USB host shield, we can connect almost any USB peripheral to the Arduino – think of navigating an Arduino robot with a USB mouse. Connecting a mouse to Arduino can be a great advantage in many applications.

In this project, we will illustrate getting input from a USB mouse into Arduino using the USB host shield. The movement of the left and right mouse button openings is detected by the Arduino and requested from the Arduino IDE Serial Monitor. The code presented in this project can be reused and expanded to build any other application using a USB mouse as an input device.

Required components

  1. Arduino UNO/Arduino Mega x1
  2. Arduino x1 USB Host Shield
  3. USB mouse x1
  4. Micro USB cable (to connect Arduino and computer) x1

Arduino USB Host Shield
The Arduino USB Host Shield is open source hardware that allows full-speed implementation of a USB peripheral or USB host on USB 2.0 compliant Arduino boards. The shield can be used with Arduino UNO and Arduino Mega. The shield is based on a USB host peripheral/controller, the IC MAX3421E. The Arduino communicates with the MAX3421E on the shield via the SPI bus available through the ICSP header. The SPI bus on Arduino UNO is present on GPIO10, GPIO11, GPIO12 and GPIO13 pins. In Arduino Mega, the SPI bus is present on GPIO pins 0, GPIO50, GPIO51 and GPIO52. GPIO7, GPIO8 and GPIO9 pins on both boards are used for GPX, INT and RES respectively.

Arduino USB host shield example

The shield can be used to connect HID devices to Arduino, such as USB keyboards, USB mouse and USB joystick. It can be used to interface Arduino game controllers such as PS4, Xbox360 and Nintendo Wii. It can be used to connect digital cameras and mass storage devices like pen drives, external hard drives or memory card readers to Arduino. Even Bluetooth dongles can be connected to the Arduino and the USB host shield. ADK-compatible Android smartphones and tablets can be connected to Arduino via the shield. The shield can also be used to connect USB to serial converters with Arduino. The shield allows you to connect, add or use many useful devices with your built-in Arduino device.

Arduino library for USB host shield
The library required to work with the Arduino USB Host Shield is called “USB Host Library for Arduino”. The source code of this open source Arduino library is available on GitHub. To install the library in the Arduino IDE, navigate to Tools->Manage Libraries. Search for “USB host”. Scroll down to “USB Host Shield Library 2.0” and click install. Try installing the latest version of the library.

Installing the Arduino library for USB Host Shield

Circuit Connections
Insert the USB host shield into the top of the Arduino UNO or Arduino Mega as shown in the image below.

Connecting USB mouse with Arduino UNO via USB Host Shield

Insert the USB mouse into the shield and connect the Arduino to the computer via a Micro-USB cable. The Arduino setup with USB host shield and mouse will be as follows.

Connecting USB mouse with Arduino UNO via USB Host Shield

Arduino Sketch

How the project works
The hidboot.h and usbhub.h libraries are required when working with a USB mouse. hidboot.h is responsible for analyzing USB HID devices such as keyboards and mice. The library is used to detect mouse events on Arduino UNO or Arduino Mega. The sketch loaded into the Arduino detects mouse movement, left button press, right button press, left button release, and right button release. Mouse events are logged to Serial Monitor. When mouse movement is detected, the change in its x and y coordinates is recorded in the Serial Monitor.

The code
The outline starts with importing the hidboot.h and usbhub.h libraries. The hidboot.h library is used to parse input from USB HID devices such as keyboards and mice. The usbhub.h library is useful if the mouse is connected to the shield via a USB hub. Next, the SPI library is imported while the shield communicates with the Arduino through the SPI port.

Some global variables are declared LeftBtn, RightBtn, LeftBtnRls, RightBtnRls, MouseMoved, x and y for the status of left button pressed, right button pressed, left button released, right button released, mouse movement, change in mouse position along x -axis and change in mouse position along the y axis.

A MouseRptParser class is defined as a child of the MouseReportParser class of hidboot.h. The OnMouseMove, OnLeftButtonUp, OnLeftButto Down, OnRightButtonUp, and OnRightButtonDown methods are imported from the MouseReportParser superclass. These methods are overridden in the outline. The OnMouseMove method is overridden to store the change in mouse xy positions in global variables x and y respectively. The method also sets the MouseMoved boolean to true. The OnLeftButtonUp method is overridden to set the Boolean MouseMoved LeftBtn to true when the left mouse button is pressed. The OnLeftButtonDown method is overridden to set the LeftBtnRls boolean to true when the left mouse button is released. The OnRightButtonUp method is overridden to set the RightBtn boolean to true when the right mouse button is pressed. The OnRightButtonDown method is overridden to set bo lean RightBtnRls to true when the right mouse button is released.

A USB class object is defined together with the HIDBoot objects and the MouseRptParser user-defined class. In the setup function, the baud rate for serial communication with the Arduino IDE's Serial Monitor is set to 115200, and if serial communication is established between the Arduino and the computer, a “start” message is printed on the Serial Monitor.

The code then checks for USB device detection through the USB host shield. If it is not detected, it prints a message “OSC did not make the art. ” for Serial Monitor. The SetReportParser( method is called on the HidMouse object with the MouseRptParser class object passed as an argument.

In the loop function, the task method is called on the USB object. Boolean variables that indicate the status of each mouse event are checked using if statements and the variables are reset to false.

It should be noted that mouse sensitivity can be adjusted by increasing or decreasing the delay in the if statement by checking the status of the MouseMoved boolean.

Result
In this project, we detect mouse events using Arduino. The movement of the left mouse button and right button click is detected and recorded. Events can be used to control a robot, scroll a screen, or control other built-in tasks.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.