Arduino®-Leonardo USB HID Keyboard & Mouse Tutorial and Advanced Serial Port Examples

NOTE: Arduino 1.01 and the Arduino Leonardo have been released! We’ll be updating these pages soon to reflect the release version of the Leonardo features. Adruino 1.01 supports modifier keys directly, so it’s a great improvement! The basic examples here should still work fine.

This tutorial covers the Arduino “Leonardo” platform which uses the new USB-enabled ATmega 32U4 MCU, with a very powerful bootloader environment that emulates both a USB mouse and keyboard (standard HID devices, no drivers needed), as well as a virtual COM port!

Along with the emulated USB HID devices, another really cool feature of the Leonardo is that it effectively has two COM ports!  One is the emulated VCP port through the USB connection, the other is the hardware UART on the microcontroller itself.  You can independently “talk” to two separate devices; one via USB, the other via serial or Bluetooth, for example.

FYI, here are links to three of our Leonardo-compatible boards:

- StealthDuino! Leonardo BASIC with ATmega 32U4 – Arduino on a USB stick!
- StealthDuino! Leonardo with ATmega 32U4 – Stealthy Arduino on a USB stick!
- BreadBoarder-32U4 – Arduino-Leonardo Compatible Prototyping Board (Nano-esque!)


HID Keyboard Examples:

This is about the easiest way to have an Arduino Leonardo interact with a PC; press a key on the keyboard!  There’s no limit to the cool things you could do with this!

In the most basic implementation, you could simply capture keystrokes into an open Notepad window, or any other application window!  There are three keyboard-specific commands in Arduino 1.0:

  • Keyboard.write(variable or constant);  // Writes the ASCII equivalent code to the keyboard
  • Keyboard.print(variable or string);  // Prints either a variable value or string to the keyboard
  • Keyboard.println(variable or string); // Prints either a variable value or string to the keyboard, with a line-feed

FYI, there appears to be a way to send modifier keys (CTRL-ALT-DEL, etc.) as well, but this hasn’t been officially documented. (actually, none of this has been documented yet!  ;)   )

Here’s an adaptation/extension of a simple Arduino 1.0RC2 example sketch that watches digital pin 3 (D3) and types out a keyboard message every time pin D3 is pulled high (5V). It demonstrates the three functions above.  If you have a Leonardo-compatible board that isn’t a StealthDuino!, just touch pin D3 to 5V to toggle the input.  See: StealthDuino_Leonardo_Keyboard.ino

HID Mouse Examples:

A Leonardo-compatible board can also take over your mouse!  Make your mouse pointer look like it’s controlled by a ghost! ;)

The current HID mouse functions are:

  • Mouse.move(mouseX, mouseY, wheel);
  • Mouse.click(button);  // single-click of a button: left, right, middle = 1,2,4 or “MOUSE_ALL”
  • Mouse.press(button);  // press & hold selected button, numbered as above
  • Mouse.release(button);  // release selected button, numbered as above
  • Mouse.isPressed();  //  boolean test to see if any/all mouse buttons are pressed

With “Mouse.move()”: “mouseX” and “mouseY” are L/R/U/D displacements, negative for U/L and positive for D/R, and “wheel” is a negative/postive wheel movement. +/-1 displacement steps give a smooth movement.

Here’s a simple Arduino sketch that moves the mouse pointer in the outline of a little square five times, then stops!  As it moves up & down it holds the left mouse button, moving left & right it holds the right mouse button.  MS Paint, or some other drawing program is a neat way to see the effect.  See: StealthDuino_Leonardo_Mouse.ino

Multiple Serial Ports:

With the Leonardo environment, there are two available serial ports; one is the emulated VCP port through the USB connection, the other is the hardware UART on the microcontroller itself.

Arduino Leonardo-compatible output on two serial windows

The Arduino developers were presented with this option when the Mega series of Arduino boards were introduced (the ATmega 128 and 256 have multiple serial UARTs), and they quickly adapted the codebase to take advantage of it.  We now can benefit from the same enhancements with the Leonardo!

The code is very simple. Below is an example, or here: Leonardo_Analog_Read_Serial.ino


/*
 Cal-Eng Leonardo dual-serial example
 Reads an analog input on pin 0, prints the result to
 the two serial ports on the Leonardo
 */

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {

  int sensorValue = analogRead(A0);
  Serial1.print("32U4 Hardware UART - Input A0: ");
  Serial1.println(sensorValue);
  delay(1000);

  digitalWrite(13, HIGH);   // set the LED on
  Serial.print("Leonardo USB VCP - Input A0: ");
  Serial.println(sensorValue);
  delay(1000);
  digitalWrite(13, LOW);    // set the LED off

}

Comments are closed.