Switch Interface

Connect an AT Switch as a PC Keyboard for under $20

Background: What are AT Switches?

People with physical challenges often are very limited in their ability to make precise movements with their arms, fingers, etc.  (This is known as having poor “fine motor” control.)

This severely limits their ability to control things like keyboards, mice, pens and pencils.  If the person is also unable to speak, they may have no way to communicate.

“AT Switches” are simple devices that allow people can trigger by hitting with an arm or foot, or even blowing on a reed.  In short they can use less precise movement (called “gross motor” control) to interact.  They come in many varieties, from many vendors, and many can be made from scratch–we’ll do that in future projects.

Connecting an AT Switch to a PC

One of the most useful things an AT Switch can do is to control a PC.  Usually this is done by making the Switch act like a keyboard with one button or a mouse that is being clicked.  There are commercial devices that are available that do exactly that:  here are some examples:

To be clear, all of these products are solid, supported devices from great companies, and there are great reasons to buy them.  They have gone through the testing necessary for Medicare/Medicaid approval.  They are supported by training and tech support.  If a person knows they’ll need a switch interface for a lifetime, they are great options.

However, if you’re trying to switch enable a particular item (perhaps a toy to stimulate a child) or if you want to try out a switch to see if is helpful, having a STEM or robotics group create one is a great option.

How To Video

How it Works

SwitchLogical

The interface is pretty simple.  We’re using an Adafruit Pro Trinket (although most Arduino compatible boards can be used) to relay button presses to the computer as keystrokes.

When the switch is pressed, it closes a contact that sends a high signal (5v in this case) to one of the Trinket’s GPIO pins (working in digital input mode).  The Trinket is programmed to watch for those signals and then send a specified keyboard letter to the PC.

The Trinket has a software library for just this purpose: it makes the Trinket “look like” a USB keyboard (also known as a human interface device or HID) to the PC.  All we have to do is tell the Trinket which letter to send when each switch is pressed and then wire up the GPIO pins to their right audio jack (where the switches will be plugged in).

A very big thank you for the great guide Adafruit created on how to turn a Pro Trinket into a Keyboard!  None of this would be possible without them!

Parts & Tools

This project requires very few parts and only basic soldering skills.  To complete an interface that can handle up to four switches, you’ll need:

  • A housing for the electronics ($1) – I used an Altoids box because it houses a half-size solderless breadboard well
  • 1 Half-size Solderless Breadboard ($5) – if you’re comfortable soldering or want a more permanent device, you can substitute a perf board (<$1) or Perma-Proto ($5)
  • Adafruit Pro Trinket board ($10).  You can substitute a spare Arduino compatible such as a Micro, Uno, but the $10 Pro Trinket works great.
  • 2 Stereo Audio Jacks ($1 ea).  Make sure you get the kind that will mount to the breadboard that you choose.  AAC Switches use the 3.5mm (or 1/8″) variety just like the “Aux” cords for your iPhone.
  • A few inches of Hookup wire (<$1).  I bought this great box of wire and it has lasted me years (I’m assuming most robotics clubs have this!).

Total cost of parts should be about $20.

The only tools you’ll need for this project are:

  • A decent soldering iron w/flux core solder to put the headers on the Pro Trinket (sadly, they don’t come attached).
  • A wire cutter/stripper for the hookup wires
  • Scissors/Tin Snips/Drill to cut holes in the housing for the switch wires and USB cord

Step-By-Step

1: Solder the headers on the Pro Trinket

If you’re using an Arduino or other board, this might be unnecessary, but Adafruit doesn’t solder on the headers on their board.  This is handy if you want to directly solder wires to the board (which might make sense here!) and it saves them money & shipping damage.  But it’s not that hard, and Collin Cunningham has a great video showing you exactly how to do it here:

If you need more help on soldering, ask in the Forums

2: Layout the components on the Breadboard

Here’s the basic layout for the interface circuit.

OpenATSwitch_bb
The connections are simple: A black wire connects Ground (PIN G) from the Pro Trinket to the SLEEVE pin on the jack.  A green wire connects the TIP pin of the jack to GPIO Pin 3.

Because the GPIO Pin will be in “ACTIVE LOW” mode, it will sit at 5V until the button is pressed which will drive it down to Ground.

3: Program Trinket to act as Keyboard

Here, Adafruit and the Arduino project have made life easy. As mentioned above, their Pro Trinket Keyboard Guide walks you through exactly how to program the device, including installing the Arduino environment, etc.  Please read their guide if you want to understand how it all works!

I’ve taken the Adafruit sample code and made modifications to support 4 switches and send the letters “A”, “B”, “C”, and “D” when the different buttons are pressed.  I’ve also added some “Debounce” timing to avoid problems caused by switches (or people!) that tend to double-tap by mistake.

#include <cmdline_defs.h>
#include <ProTrinketKeyboard.h>
#include <ProTrinketKeyboardC.h>
#include <usbconfig.h>

/*
 * 4-button AAC Switch Interface w/debounce
 * By Bill Binko (bill@lessonpix.com)
 * Based directly on ProTrinketKeyboard example
For Pro Trinket (ATmega328 based Trinket) by Adafruit Industries
Please use library TrinketKeyboard for the ATtiny85 based Trinket
Version 1.0 2015-01-01 Initial Version derived from TrinketKeyBoardExample Mike Barela
*/

#include <ProTrinketKeyboard.h> // Ensure the library is installed

// Switches are connected from ground to these defined pins
const int PIN_BUTTON_CAPITAL_A = 3;
const int PIN_BUTTON_CAPITAL_B = 4;
const int PIN_BUTTON_CAPITAL_C = 6;
const int PIN_BUTTON_CAPITAL_D = 5;

int lastButtonState[10]; // current state of the button


void setup()
{
 // Declare button pins as inputs
 pinMode(PIN_BUTTON_CAPITAL_A, INPUT);
 pinMode(PIN_BUTTON_CAPITAL_B, INPUT);
 pinMode(PIN_BUTTON_CAPITAL_C, INPUT);
 pinMode(PIN_BUTTON_CAPITAL_D, INPUT);
 
 // setting input pins to high means turning on internal pull-up resistors
 digitalWrite(PIN_BUTTON_CAPITAL_A, HIGH);
 digitalWrite(PIN_BUTTON_CAPITAL_B, HIGH);
 digitalWrite(PIN_BUTTON_CAPITAL_C, HIGH);
 digitalWrite(PIN_BUTTON_CAPITAL_D, HIGH);
 // remember, the buttons are active-low, they read LOW when they are not pressed

 lastButtonState[PIN_BUTTON_CAPITAL_A]= HIGH;
 lastButtonState[PIN_BUTTON_CAPITAL_B]= HIGH;
 lastButtonState[PIN_BUTTON_CAPITAL_C]= HIGH;
 lastButtonState[PIN_BUTTON_CAPITAL_D]= HIGH;
 // start USB stuff
 TrinketKeyboard.begin();
}

void checkButton(int pin, char code)
{
 int buttonState = digitalRead(pin);
 if (buttonState != lastButtonState[pin])
 {
 if (buttonState == LOW)
 { 
 TrinketKeyboard.pressKey(KEYCODE_MOD_LEFT_SHIFT, code);
 // this should type a code sent in
 TrinketKeyboard.pressKey(0, 0);
 // this releases the key
 }
 }
 lastButtonState[pin] = buttonState;
 return;
}

void loop()
{
 TrinketKeyboard.poll();
 // the poll function must be called at least once every 10 ms
 // or cause a keystroke
 // if it is not, then the computer may think that the device
 // has stopped working, and give errors
 checkButton(PIN_BUTTON_CAPITAL_A, KEYCODE_A);
 checkButton(PIN_BUTTON_CAPITAL_B, KEYCODE_B);
 checkButton(PIN_BUTTON_CAPITAL_C, KEYCODE_C);
 checkButton(PIN_BUTTON_CAPITAL_D, KEYCODE_D);
}

To use this code, simply copy it, paste it into the Arduino IDE and download it to the board.  You can learn all about setting up Arduino for the Pro Trinet at Adafruit’s great introductory guide.   Make sure you start the download quickly after plugging in the Pro Trinket since it switches to USB mode in a few seconds (watch the video for more information).

4. Testing it out!

Now that we’ve programmed the Pro Trinket to behave like a keyboard, we can disconnect it from the programming computer and plug it into any PC, Mac or ChromeBook that takes a USB Keyboard.  You can even plug it into a machine that already has a keyboard.

When you press the button, it will send an ‘A’ keystroke through the USB cable!  Open up a text editor (or Word, etc.) and make sure that when you press the button, it inserts A’s.  If not, join us in the Forums for some debugging!

What’s Next?

So, what’s the point?  It turns out this is a really big deal!  Now you (or your friendly programmer) can write software that responds to this keystroke in any language such as:

  • Javascript
  • Python
  • C/C++
  • PHP

For an example, check out the Switch Enabled Cariboo Game – this is the only custom hardware needed to make that happen!  Want to know how Assistive Technology Professions would use this?  Ask in the Forums – they’ll be happy to fill you in!

Great work, and make sure you send us pictures of your versions!

Leave a Reply