Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

USB HID

The Arduino Uno R4 WiFi is not just a powerful development board; it also comes with built-in support for Human Interface Devices (HID). This enables you to use the board to emulate devices like mice and keyboards, adding a new level of interactivity to your projects.

HID, or Human Interface Devices, are a category of computer devices designed for direct interaction with humans, typically for input purposes. This category includes devices like keyboards, mice, and game controllers. With the Arduino Uno R4 WiFi, you can emulate these devices, thereby unlocking a host of possibilities for DIY projects.

Mouse Control

Controlling a mouse using the Arduino Uno R4 WiFi is straightforward. By using the Mouse.move(x,y) command, you can easily control mouse movement. When updating the cursor position, it is always relative to the cursor’s previous location.

Here’s a simple example that demonstrates mouse cursor control using a button.

Circuit Diagram

../_images/05_hid_1_bb.png

Upload the Code

Open the 05-hid_mouse.ino file located at elite-explorer-kit-main\r4_new_feature\05-hid_mouse, or paste the following code into your Arduino IDE.

Warning

When you use the Mouse.move() command, the Arduino takes over your computer’s mouse! To insure you don’t lose control of your computer while running a sketch with this function, make sure to set up a reliable control system before you call Mouse.move(). This sketch includes a pushbutton to toggle the mouse, so that it only runs after the button is pressed.

Warning

Due to the multi-processor architecture of the UNO R4 WiFi board, you may face “No device found on…” errors while uploading code that uses HID functionalities.

To upload under such circumstances, follow these steps:

  1. Quickly press and release the “RESET” button on the board twice. The LED marked “L” should start pulsing.

  2. From the Arduino IDE menu, select the board’s port. The port may change following the reset, so ensure it’s correctly selected.

05-hid_mouse.ino
 1/*
 2  This code is designed to control mouse cursor movement using an Arduino Uno R4.
 3  When the button connected to pin 7 is pressed, the mouse moves 10 units to the right.
 4  Uncommenting the respective sections below the main loop will change the direction of movement.
 5
 6  Board: Arduino Uno R4 
 7*/
 8
 9#include <Mouse.h>  // Include the Mouse library for mouse control
10
11const int button = 7;  // Define the pin where the button is connected
12
13void setup() {
14  pinMode(button, INPUT);  // Set the button pin as an input
15  Mouse.begin();  // Initialize mouse control
16  delay(1000);  // Wait for 1 second (1000 milliseconds) for hardware initialization
17}
18
19void loop() {
20  // Check if the button is pressed (HIGH)
21  if (digitalRead(button) == HIGH) {
22    Mouse.move(10, 0);  // Move the mouse 10 units to the right
23    delay(200);  // Wait for 200 milliseconds to slow down mouse movement
24
25    // Left
26    // Mouse.move(-10, 0);  
27    // delay(200);  
28
29    // Down
30    // Mouse.move(0, 10);  
31    // delay(200);  
32
33    // Up
34    // Mouse.move(0, -10);  
35    // delay(200);  
36
37  }
38}

In addition to controlling mouse movement, you can also handle mouse clicks. For more details, refer to Arduino Reference - Mouse .

Keyboard Control

The Arduino Uno R4 WiFi also provides keyboard emulation capabilities. It allows you to send not only individual keypresses but also execute complex key combinations.

Warning

When you use the Keyboard.print() command, the Arduino takes over your computer’s keyboard! To insure you don’t lose control of your computer while running a sketch with this function, make sure to set up a reliable control system before you call Keyboard.print(). This sketch includes a pushbutton to toggle the keyboard, so that it only runs after the button is pressed.

Example Code for Sending Shortcut Keys

In this instance, the Arduino Uno R4 WiFi is configured to emulate two frequently-used keyboard shortcuts: “Ctrl+C” for copy and “Ctrl+V” for paste. Two physical buttons connected to the Arduino serve as triggers. The button connected to pin 7 initiates the copy action, while the one connected to pin 8 triggers paste.

Upon pressing either button, the Arduino employs the Keyboard.press() and Keyboard.releaseAll() functions to mimic the respective keyboard shortcuts. This example illustrates how you can design a dedicated hardware interface for specific tasks, facilitating repetitive actions without keyboard involvement. This could be especially advantageous in workplaces requiring quick data manipulation or in accessibility setups that benefit from simplified controls.

Circuit Diagram

../_images/05_hid_2_bb.png

Upload the Code

Open the 05-hid_keyboard.ino file located at elite-explorer-kit-main\r4_new_feature\05-hid_keyboard, or paste the following code into your Arduino IDE.

05-hid_keyboard.ino
 1/*
 2  This code is designed to provide copy and paste functionality using two buttons with an Arduino Uno R4.
 3  When the button connected to pin 7 is pressed, a "copy" command (Ctrl + C) is sent to the computer.
 4  When the button connected to pin 6 is pressed, a "paste" command (Ctrl + V) is sent to the computer.
 5
 6  Board: Arduino Uno R4 
 7*/
 8
 9#include <Keyboard.h>  // Include the Keyboard library to enable keyboard functionalities
10
11const int copyButtonPin = 7;  // Pin number for the copy button
12const int pasteButtonPin = 6; // Pin number for the paste button
13
14void setup() {
15  Keyboard.begin();  // Initialize the Keyboard library
16  pinMode(copyButtonPin, INPUT);  // Set the copy button pin as input
17  pinMode(pasteButtonPin, INPUT); // Set the paste button pin as input
18  delay(1000);       // Wait for 1 second to allow hardware initialization
19}
20
21void loop() {
22  // Check if the copy button is pressed
23  if (digitalRead(copyButtonPin) == HIGH) {
24    Keyboard.press(KEY_LEFT_CTRL);  // Press the Ctrl key
25    Keyboard.press('c');            // Press the 'c' key
26    Keyboard.releaseAll();          // Release all keys
27    delay(200);                     // Wait for 200 milliseconds
28  } 
29  // Check if the paste button is pressed
30  else if (digitalRead(pasteButtonPin) == HIGH) {
31    Keyboard.press(KEY_LEFT_CTRL);  // Press the Ctrl key
32    Keyboard.press('v');            // Press the 'v' key
33    Keyboard.releaseAll();          // Release all keys
34    delay(200);                     // Wait for 200 milliseconds
35  }
36}

Caveats and Tips

  1. Cautionary Note on Mouse and Keyboard Libraries: If either the Mouse or Keyboard library is running continuously, it could interfere with your board’s programming. Functions like Mouse.move() and Keyboard.print() will assume control of your connected computer and should be invoked only when you’re prepared to manage them. It’s advised to use a control system, such as a physical switch or specific input controls, to toggle this functionality.

  2. If You Encounter Code Upload Issues: Due to the multi-processor architecture of the UNO R4 WiFi board, you may face "No device found on..." errors while uploading code that uses HID functionalities.

    To upload under such circumstances, follow these steps:

    1. Quickly press and release the “RESET” button on the board twice. The LED marked “L” should start pulsing.

    2. From the Arduino IDE menu, select the board’s port. The port may change following the reset, so ensure it’s correctly selected.

Reference