Arduino UNO R4 WiFi USB HID

Learn how to use the UNO R4 WiFi as a mouse/keyboard.

In this tutorial you will learn how to emulate a mouse/keyboard using an Arduino UNO R4 WiFi board with the Keyboard and Mouse APIs.

This feature can be used to create game controllers, keyboard extensions or other HID devices.

Goals

The goals of this tutorials are:

  • Learn how to emulate a keyboard (keypresses),
  • learn how to emulate a mouse (x,y coordinates).

Hardware & Software Needed

Human Interface Device (HID)

Human interface devices (HID) are devices designed for humans (keyboards, mice, game controllers etc.), that frequently sends data over USB to a computer. When you press a key on a keyboard, you send data to a computer, which reads it and in turn activates the corresponding key.

The UNO R4 WiFi has built-in support for HID, a feature found on most modern day development boards, but not on previous UNO revisions.

To turn your board into an HID, you can use the keyboard/mouse API that is built in to the Board Package. You can visit the documentation for this API in the language reference at:

Sketch Upload Interference

As a consequence of the multi-processor design of the UNO R4 WiFi board, uploads may fail with a "

No device found on ...
" error when the board is running a sketch that uses the HID capabilities.

For this reason, you should use the following procedure to upload under these conditions:

1. Press and release the button marked "RESET" on the board quickly twice. The LED marked "L" on the board should now be pulsing.

2. Select the port of the board from the menu in Arduino IDE. The port might have changed after the previous step, so make sure to verify that it is selected.

3. Upload your sketch as usual.

Keyboard

To use keyboard functionalities, we need to include the library at the top of our sketch. The Keyboard class contains several methods that are useful to emulate a keyboard.

1#include <Keyboard.h>
2
3Keyboard.method()

Keyboard Example

To emulate a keyboard, we can use the

press()
and
releaseAll()
methods. This will emulate a keypress, as well as releasing the keypress. The following example prints a "w" every second.

1#include <Keyboard.h>
2
3void setup() {
4 Keyboard.begin();
5 delay(1000);
6}
7
8void loop() {
9 Keyboard.press('w');
10 delay(100);
11 Keyboard.releaseAll();
12 delay(1000);
13}

To see more examples, please refer to links below:

Mouse

To use mouse functionalities, we need to include the library at the top of our sketch. The Mouse class contains several methods that are useful to emulate a mouse.

1#include <Mouse.h>
2
3Mouse.method();

Mouse Example

The following example moves both axis of mouse just slightly (10 points), back and forth.

1#include <Mouse.h>
2
3void setup() {
4 Mouse.begin();
5 delay(1000);
6}
7
8void loop() {
9 Mouse.move(10,10);
10 delay(1000);
11 Mouse.move(-10,-10);
12 delay(1000);
13}

To see more examples, please refer to links below:

Summary

In this tutorial, we have demonstrated some basic HID usage with the UNO R4 WiFi. To view the full API, please refer to the following APIs:

In there, you will find a detailed reference along with some good examples to get you started with HID features.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.