The code uses the Trinket Keyboard library.  Not much code is required, basically looping waiting for the switch to activate so it can send your keyboard output.

You will want to install the Arduino IDE on your computer.  The instructions for doing this are in the Introducing Trinket tutorial

The code relies on an Arduino library for using the Trinket as a USB keyboard introduced in the Adafruit tutorial Trinket USB Keyboard.

Install the library.  Adafruit has helpful information on installing a library.  

Keep in mind that this library is designed specifically for the original ATtiny85 based Trinket. If you are looking to use a Pro Trinket instead of a 'classic' Trinket, look for the Pro Trinket USB Keyboard library although you might find the Pro Trinket a bit big for the space inside the switch.

The program is rather short (if you look past the liberal comments):

To upload the code to Trinket, you'll need to press the Trinket's Reset button and then press the Arduino IDE upload button (an arrow facing right) within the 10 seconds the red LED is flashing.  There may be some Arduino library warnings relating to the TrinketKeyboard library but that's ok.  See the Trinket tutorial if you are unfamiliar with loading code onto Trinket.

Custom Keyboard Output

The code currently outputs the keycode for a Windows Shift Printscreen key to copy the screen graphics to the WIndows clipboard.  

You can output any number of custom single characters with calls to TrinketKeyboard.pressKey (one to "press" the key, another with zero arguments to "release" the key).

If you want to output a fixed string of characters, replace (or add) call(s) to TrinketKeyboard.print.  In the last comment, at the end of the code, shows an example line you can uncomment and replace the message.

The codes for which keys and 'modifiers' (shift, control, alt and other special keys) are predefined and may be used are found in the Trinket Keyboard library file TrinketKeyboard.h.

This code for example, takes a screenshot when the switch is pressed, handy for documenting projects!

Trinket USB Foot Switch

Based on TrinketKeyboard example using the Trinket Keyboard Library
For Trinket (ATtiny85 based board) by Adafruit Industries

Version 1.0  2015-01-19 Initial version     Mike Barela

Support Adafruit tutorials by buying parts from

#include <TrinketKeyboard.h>  // Trinket keyboard library

const int PIN_SWITCH = 0;    // Trinket pin to connect to switch 

void setup()
  // Set button pin as input with an internal pullup resistor
  // The button is active-low, they read LOW when they are not pressed

  TrinketKeyboard.begin();  // initialize keyboard library

void loop()
  // the poll function must be called at least once every 10 ms
  // or the computer may think that the device
  // has stopped working, and give driver errors

  if (digitalRead(PIN_SWITCH) == LOW)  // If the foot switch grounds the pin
    // Select what key to press when the switch is tripped
    //   Possible keys are defined in TrinketKeyboard.h
    // Selected keys are Print Screen with the shift key modifier
    TrinketKeyboard.pressKey(0, 0);  // release key
    // If you want to send a string, replace the 2 calls above with the line below
    // TrinketKeyboard.print("Hello World!"); // use for string instead of char

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

This page (Code) was last updated on Jan 18, 2015.

Text editor powered by tinymce.