Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Warning! You may have a hard time re-programming your Arduino if it’s constantly taking over your mouse or keyboard!

You can build in a work around with a little logic and a jumper wire or button. Start your code with something similar to this, which would block the HID behaviour, unless pin 10 is connected to GND. GND already needs to be connected before powering up the Arduino in this example.

Code Block
languagecpp
#define HIDdisablePin 10

void setup() {
  pinMode(HIDdisablePin, INPUT_PULLUP);
}

void loop()
{   // Disable the mouse unlessUnless HIDdisablePin is connected to ground
  // This would need to be at the start of your loop() function. 
    if(digitalRead(HIDdisablePin) == HIGH) {
         while(1) { /* infinite loop */ }
    }
  // The rest of your code after this line
  
  
  
}

void loop() {
 

}


You may also have trouble finding your Arduino on the ports list in the Arduino IDE after turning it into a HID device. With the ESP32-S3 Thing Plus, hold the BOOT button while plugging into the USB C plug to set it into boot mode, which should alow you to find it in the port list.

HID Mouse

This example gets the Arduino to act like a mouse, based on two analog Inputs. Don’t forget to connect pin 10 to GND! This example uses basic digital inputs, so you can test it out with a jumper wire from GND to pins 1,2,3,5, and 7 to left click.

Code Block
#include "USB.h"
#include "USBHIDMouse.h"

#define HIDdisablePin 10
#define mouseXUp 1
#define mouseXDown 2
#define mouseYLeft 4
#define mouseYRight 5
#define click 7

USBHIDMouse Mouse;

void setup() {
  pinMode(HIDdisablePin, INPUT_PULLUP);
    // disable the mouse unless HIDdisablePin is connected to ground
  if (digitalRead(HIDdisablePin) == HIGH) {
    while (1) { /* infinite loop */
    }
  }
  // The rest of your code after this line
  pinMode(mouseXUp, INPUT_PULLUP);
  pinMode(mouseXDown, INPUT_PULLUP);
  pinMode(mouseYLeft, INPUT_PULLUP);
  pinMode(mouseYRight, INPUT_PULLUP);
  pinMode(click, INPUT_PULLUP);
  // initialize mouse control:
  Mouse.begin();
  USB.begin();
}

void loop() {

  if (digitalRead(mouseXUp) == LOW) {
    // move mouse up
    Mouse.move(0, -10);
  } else if (digitalRead(mouseXDown) == LOW) {
    // move mouse down
    Mouse.move(0, 10);
  }
  if (digitalRead(mouseYLeft) == LOW) {
    // move mouse left
    Mouse.move(-10, 0);
  } else if (digitalRead(mouseYRight) == LOW) {
    // move mouse right
    Mouse.move(10, 0);
  }

    // if the mouse button is pressed:
  if (digitalRead(click) == LOW) {
    // if the mouse is not pressed, press it:
    if (!Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.press(MOUSE_LEFT);
    }
  } else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.release(MOUSE_LEFT);
    }
  }
  delay(10);
}

HID Keyboard