Overview

Adafruit carries a lovely foot switch which has a single pole-double throw switch inside.  The switch can activate a number of items including an alarm or powerswitch tail to control your world.

A much-requested project by Adafruit customers is using the foot switch to interface with a PC via universal serial bus (USB).  To get a switch activation to talk to the USB bus takes a controller - some smarts.  

Adafruit's ATtiny85 based Trinket is low cost and contains enough smarts to perform USB communication (via some smart library software).

Only three parts are required.  The foot switch, a USB cable, and a Trinket 5V.  The entire project costs less than $20.  And it takes less than an hour's work.

The wiring is shown above.  The existing foot switch wires are cut short and two connected to Trinket pins #0 and GND (ground).  When the foot switch is activated, the switch contact pair that creates a short connects Pin #0 with ground. When the Trinket detects the pin connected to ground, it outputs one or more characters as a PC keyboard.

The Trinket Keyboard tutorial states a Trinket 5V should be used due to the microcontroller clock rate the library uses.

As you take apart and reassemble the switch, there are some small parts you will want to keep track of for reassembly.

Assembly

Disassembly of the foot switch is not hard but you will want to do so a bit slowly to get the parts gathered.  You might want a cup or a mat to gather the parts.

Turn the pedal over and using a medium Phillips screwdriver, carefully remove the metal back cover.  

 

There is a tiny metal pin in the back of the petal.  We do not want to lose this.

Carefully tip the pedal over and catch the small metal pin.  Place it in a safe place with the screws.

Hold the pedal together with one hand while putting your screwdriver on the metal hinge pin.  Keep pressure on the pedal (it is spring loaded) while using pressure to push the hinge pin out.

 

If you get the pin partially out, you can use pliers to grab one end and remove it.

 

Slowly release the pressure between the two halves of the pedal.  When you open it up, be sure to save the spring that provides the pedal its action.

So if you have carefully disassembled up to now, this is the collection of parts you have.

 

You can remove the spring and set it aside with the small parts.

Note the cable restraint - black with 2 screws, holding the cable onto the pedal base in the above picture. Using your screwdriver, carefully remove the two screws and the black plastic bracket holding the wire.  Do not remove the switch assembly in the center of the base yet.

Using wire cutters, cut the cable about 1.5 inches (4 centimeters) past the pedal base.

 

You can set the long cut grey cable aside, this project will not use it, but 3 conductor cable can be useful in another project.

You can carefully grab the grey outer plastic of the short cut cable and slide it off revealing three wires: black, white, and red.


Pull the wires through the hole. You can strip 3/8 inch (3 millimeters) of insulation off the red and white wires. We will not use the black wire, it will be tucked behind the black switch housing when reassembling.

Remove the screws holding the black switch in the center of the pedal base and remove the switch leaving just the grey base.  Using a rat tail file or a Dremel tool, carefully enlarge the hole that the wire feeds out of the base to accomodate the Mini-B part of the USB cable.  Slowly enlarge the top and a bit of the bottom.  The sides do not need much.  Try hard to not break through the top of the hole.

Feed the Mini-B side of the cable through the enlarged hole.  Place the Trinket 5V onto the cable end to get a feel for how much cable you want inside the housing (it is a tight fit).

 

When you have the cable in position, replace the black cable restraint with its screws.  If the restraint does not "grab" the wire tight enough to hold it well, you can place something else under the restraint like a small piece of the cable cut to 1/4" (6 millimeters).   

Alternatively, you can cut the extra piece of outer cable jacket you had in the last picture in half, then split it up each side to open.  Wrap the jacket pieces around the USB cable making it fatter.  Then secure down the restraint screws.

The USB cable should be held snug now by the restraint.  Replace the black switch in the center of the grey pedal base with its two screws.

If you have not done so, strip 3/8" (3 millimeters) of insulation from the red and white wires.  Solder the white wire to the GND pin on the Trinket.  Solder the red wire to the #0 pin on Trinket.

 

This will cause the switch to close the connection on Pin #0 to ground when the pedal is stepped on.

 

You can tuck the black wide under the switch or cut it off.

Reassembly

Tuck the Trinket and the cable up next to the switch.  The top of the trinket board should not protrude above the switch so it will need to be placed at a slight angle.  Put some pressure on the cable so the edge of the Trinket does not overlap the side of the pedal base.

 

Now you should secure the cable.  I suggest some hot glue between the black part of the Mini-B part of the cable and the switch.  Also place some glue where the cable hits the right part

of the pedal base (circled red in the picture).  Sugru would work very well also but you need to keep the parts in place until it sets.  Do not let the top of the Trinket board get above the switch or it will be hit by the pedal top.

Here you can see the hot glue in place. When replacing the spring, it should not touch the Trinket.  To be sure, I used a small piece of electrical tape.

 

Place the spring on the raised circle on the pedal base. There is also a raised circle on the pedal top that you'll need to mate with while you bunch the halves together to put the hinge pin back in.  It is not hard but you have to exercise some care.

Matching the raised circles on each half of the plastic with the spring, compress the spring (making a plastic sandwich of the halves again).  Align the holes in the bottom side with the hinge pin.  

Note the hinge pin has a narrow part closer to one side than the other.  Make sure this indentation is placed towards the side circled in the picture above.

Slide the large metal hinge piece through the holes while you compress the two halves between your hands.  Just a bit of metal sticks out of each end.  If you let go, the switch whould stay together and click when you compress near the top.

If you have the large hinge piece in and oriented correctly, then when you turn the foot switch over, the tiny pin will fit back in the hole shown.  You might have to adjust the large pin back & forth a bit to get the tiny pin to fall flush with the back.  If you inserted the large hinge pin backwards and the tiny pin does not fit, no worries, repeat the disassembly and reassembly steps.

Now place the metal back plate on and screw on with the two flat screws.  This will hold the tiny pin in place which holds the larger hinge pin.  The back plate also provides a tough surface for the switch.

That's it, all assembled.  Alot of steps were used to show the process, although the build is rather easy and does not take much time.

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!

Download: file
      /*
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 Adafruit.com
*/

#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
  pinMode(PIN_SWITCH, INPUT_PULLUP);

  TrinketKeyboard.begin();  // initialize keyboard library
}

void loop()
{
  TrinketKeyboard.poll();
  // 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(KEYCODE_MOD_LEFT_SHIFT, KEYCODE_PRINTSCREEN);
    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
  }
}
    

The USB foot switch should work with Windows, Linux, or Mac from the last few years (with plug and play, which has been around for quite a time).  When you plug in the USB cord, the computer will detect something is plugged in but it will not know it's the switch until the Trinket boot code finishes timing out in a few seconds. Then your operating system device list will show a keyboard device entitled "Trinket Keyboard".

Place the switch part in a comfortable position.  When the switch is activated by a foot press, the keycodes and/or text you have programmed will be output into the current window (the one with the focus in operating system speak).  So if you have a word processor and are typing text and the switch is programmed to type your name, it will output the characters for your name.  If you have an email program open and activate the switch, it outputs your name.  If you have a web broswer form open, it'll output your name, you get the idea.

As programmed in the example,  the switch will output the special keyboard code for Windows PCs (and only Windows machines) to place a copy of the displayed screen graphics into the Windows clipboard.  This is something authors or other content creators do frequently.

If you have a Linux machine or Mac, the screen capture keycode is probably something else. No worries, do an online search for the key action you want for your operating system.  Replace the keycodes into the Arduino code and upload to the Trinket.

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