Versions Compared

Key

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

The Hid protocol is used for physical interaction devices such as the mouse, keyboard and much more. Many Arduino-compatible development boards support HID (but not all), allowing the microcontroller to behave just like a regular mouse, keyboard or gamepad. These examples are intended for ESP32 boards, such as the ESP32-S3 Thing Plus from Sparkfun, which also supports the MIDI protocol.

All the examples on this page can be tested with jumper wires connecting the appropriate pins to ground.

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

...


You may also have trouble finding your Arduino on the ports port 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 allow you to find it in the port list.

...

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

#define HIDdisablePin 10
#define message 1
#define shortcut 2

USBHIDKeyboard Keyboard;

void setup() {
  pinMode(HIDdisablePin, INPUT_PULLUP);
  delay(20);
  // 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(message, INPUT_PULLUP);
  pinMode(shortcut, INPUT_PULLUP);
  // initialize keyboard control:
   Keyboard.begin(KeyboardLayout_de_DE); // optional keyboard region setting
  USB.begin();
}

void loop() {
  

  if (digitalRead(message) == LOW) {
    // write a keyboard message
       Keyboard.print("Interaction Design ZHdK!");
      delay(400);
  } 
  if (digitalRead(shortcut) == LOW) {
    // focus on a random application window in OSX
      Keyboard.press(KEY_RIGHT_GUI);
      Keyboard.press(KEY_TAB);
      delay(100);
      Keyboard.release(KEY_TAB);
      delay(100);
      int randomCount = random(6);
      int count = 0;
      while (count < randomCount) {
        count++; 
      Keyboard.press(KEY_TAB);
      Keyboard.release(KEY_TAB);
      delay(400);
      }
      Keyboard.releaseAll();
  } 

  delay(10);
}

HID Volume

Here is a simple example for controlling your computer's volume. This uses the consumer control options from the HID library, which includes many common controls for browsing, music playback and more. See the source code for the full list. Again, be sure the pin 10 is connected to GND.

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

#define HIDdisablePin 10
#define volumeUp 1
#define volumeDown 2
#define mute 4
USBHIDConsumerControl ConsumerControl;

void setup() {
  pinMode(HIDdisablePin, INPUT_PULLUP);
  delay(20);
  // 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(volumeUp, INPUT_PULLUP);
  pinMode(volumeDown, INPUT_PULLUP);
  pinMode(mut, INPUT_PULLUP);
  // initialize keyboard control:
  ConsumerControl.begin();
  USB.begin();
}

void loop() {
  

  if (digitalRead(volumeUp) == LOW) {
    // Increase Volumne
    ConsumerControl.press(CONSUMER_CONTROL_VOLUME_INCREMENT);
    ConsumerControl.release();
    delay(200);
  } 
  if (digitalRead(volumeDown) == LOW) {
    ConsumerControl.press(CONSUMER_CONTROL_VOLUME_DECREMENT);
    ConsumerControl.release();
    delay(200);
  } 

  if (digitalRead(mute) == LOW) {
    ConsumerControl.press(CONSUMER_CONTROL_MUTE);
    ConsumerControl.release();
    delay(200);
  } 

  delay(10);
}