This guide has been superseded by other guides which provide similar functionality. Note the methods used in this guide cannot be used with other microcontroller boards.

The Pro Trinket's USB connector is used for uploading sketches, but the connector is not a full USB port due to lack of dedicated hardware.  You can, though, use the connection to emulate some basic USB 1.1 devices via an Arduino software library presented in this tutorial. For example, via the USB 1.1 protocol, you can build low speed USB Human Interface Devices (or HID). Examples of HIDs are keyboards, mice, joysticks, gamepads, etc.

Using the Pro Trinket as a USB keyboard is demonstrated in an earlier tutorial here.   Pro Trinket can emulate a computer mouse with two examples to demonstrate translatating movement into on-screen cursor changes.

Software Library

The Pro Trinket Mouse library is available on GitHub. You can download the code from the link below.  See the tutorial All About Arduino Libraries on installing the code into your Arduino Integrated Development Environment (IDE) for your use.

The functions in the library for interacting with the USB port and sending text are shown below.  You can refer to the examples on the following pages to see how they are used.

// Starts the USB driver, place in the Arduino IDE setup routine:
void TrinketMouse.begin(); 

// Here is the do-all function. Pass the following values:
//  x - the number of pixels to move in the horizontal direction (-127 to 127)
//      Note: this is in relation to the current mouse position, not absolute 
//      screen location
//  y - the pixels to move in the vertical direction (-127 to 127)
//  wheel - amount to tell the PC the mouse wheel has been spun (-127 to 127)
//  buttonMask - the following values may be ANDed to press mouse buttons:
//      MOUSEBTN_LEFT_MASK		0x01  // Left button
//      MOUSEBTN_RIGHT_MASK		0x02  // Right buttom
//      MOUSEBTN_MIDDLE_MASK    0x04  // Middle Button
void TrinketMouse.move(signed char x, signed char y, signed char wheel, uint8_t buttonMask); // makes a mouse movement, must be called at least once every 10ms, even if no movement
// Examples: 
TrinketMouse.move(5,-5,0,0);  // Move 5 pixels up, 5 left from current location
TrinketMouse.move(0,0,0,MOUSEBTN_LEFT_MASK); // Click left mouse button

// This function checks if USB is connected, returns 0 if not connected
char TrinketMouse.isConnected();

The code works for both the Pro Trinket 5V running at 16 MHz and the Pro Trinket 3V running at 12 MHz.

This guide was first published on Jan 25, 2015. It was last updated on Mar 08, 2024.

This page (Overview) was last updated on Mar 08, 2024.

Text editor powered by tinymce.