Skip to content

DIYAT: Switch Interface

GIF of a switch trying to connect to computer not working and then connecting through an interface with success.

A switch interface is a common device used by persons with motor disabilities to connect their switches to a computer. Although most switches have an 3.5 mm jack output, simply connecting this output to a computer's audio jack will not be recognized by a computer. A switch interface works as a relay that reads the button press or switch activation and sends a HID (Human Interface Device) command, or in this case a Keycode, to the computer.

This project uses the Adafruit QTpy board and Circuit Python and is a beginner friendly build. The QTPy, is a low cost development board running the SAMD21 chip and supports HID libraries for keyboard and mouse emulation. CiruitPython, developed by Adafruit, is an off shoot of Micropython and created for the purpose of lowering the entry barrier to working with microcontrollers. This project may also be completed using the Arduino IDE. The build provides access to 4 switch connections although additional jacks and switches may be added.

Now let's get to building!

Materials:

  • Adafruit QTPy board
  • USB C cable (ensure this is a data transfer cable and not charge only)
  • Switch Interface Case Printed Parts
  • (4) 6-32 Nuts
  • (4) 6-32 1/2" screws
  • (3) Socket header cables
  • (4) 1" length wire with each end stripped at 1/4"
  • Soldering Iron + Solder

Solder the headers

The QTPy comes headerless (the pins are not soldered into the board). You will need to solder the pins into the board for easier access. Adafruit has a great beginner video on how to solder headers.


Print the Case

Download the STL files for the case available here. The case was printed with 20% infill using PLA. You may access the Tinkercad project if you'd like to modify the case or add additional jacks.

3D print images of case design

Join and Solder the Ground Cables

illustration showing a Socket header cable connected to 4 stripped wires Cut the socket header wires in half and separate out one of the wires stripping about 1/4" from the end. Split the wire into 4 parts and solder the 1" wires to each split segment.


Solder the Ground Cables to the Jack

illustration of jacks connected to ground cables Solder the opened end of the ground to the longer leg of the jack. Repeat this step for each jack used.


Solder the header cables to the jack

illustration of socket cables attached to end of jack Using the remaining 4 socket cables, solder a single cable to each available jack output. Please note, if you are not getting a successful switch connection the wrong jack output may have been soldered. Try soldering it the opposite output.

It might be helpful to test each jack to ensure all the wires and ports are correctly soldered and wired before the next step. This would involve skipping head and flashing the needed code. Alternatively, if you have some multimeter skills this might be a good time to put them to use.


Insert the jacks into the opening and connect the pins

Inside case with jacks and board inserted Insert the jacks into the openings and screw the tightening ring on from the outside of the case. Repeat for each jack. Then proceed to connect the socket pins as follows:

Ground > Ground

Remaining socket cables to pins: A0, A1, A2, and A6

** Note, depending on the hight of the plastic casing of the socket cable, you may need to carefully cut and remove a bit of the top of the casing to ensure the cover closes.

Insert the nuts and screw on the case top

switch interface case with screws Insert the nuts into each corner on the inside of the case within the available slots. Then place the cover and screw the top into place. Insert the USB cable into the board and connect the other end to your computer.

This project uses CircuitPython so you will need to install both the .uf2 file for the board as well as the HID libraries for sending key strokes to the computer.

Install the CircuitPython .uf2 file

Head over the the CircuitPython website and download the most current version of CP for this board. This tutorial is using 6.0.1.

Plugin the USB to your computer and press the reset button twice. You should now see a "QTPY_BOOT" drive visible.

Drag the .uf2 file (most likely from your downloads folder) into the drive. You will see a quick flash and the drive will reboot. You should now see the drive as "CIRCUITPY" drive.

Download and Install the HID Library

This project uses an additional library which will need to be downloaded and installed from the CP library website. You may download the CP Library Bundle which includes our needed HID library. Be sure to select the bundle version that matches the CP version you are using. For this tutorial, the 6.x version was downloaded and unzipped.

On your "CIRCUITPY" drive within the "lib" folder, create a new folder titled "adafruit_hid". From the downloaded and unzipped library folder (most likely in your downloads folder), locate the "adafuit_hid" folder and copy/paste the __init__.mpy, keyboard.mpy, and keycode.mpy files into the QTpy folder.

finder window indicating path to HID folder
Path to downloaded adafruit_hid folder

Note: the QTPy may not have enough memory to store the full hid library and only these .mpy files are needed for this project.

Finder window showing the lib folder containing an adafruit_hid folder and .mpy files.
Library files pasted into QTPy board

Create the code.py file

CP looks for a code.py file upon completing the boot sequence and runs the code provided within this file. If you already have a code editor program of your choice, you may copy and paste the code below and save it to the "CIRCUITPY" drive. Note, the code the will execute a few seconds after the file is saved. Provide enough time for the action to complete.

If you have not worked with code before nor have a code editor of your choice, Adafruit provides a great step by step tutorial on how to get started using the beginner friendly MU editor. Visit their tutorial here.

Customizing your Code

If you would like to make use of the additional pins or change the pins being used. You may add pins by creating a new instance of a pin adding it to the array in line 13. Be sure to use a novel name for the pin and indicate the desired pin number using the board.pin# syntax. See example below:

keypress_pins = [board.A0, board.A1, board.A2, board.A6, board.A3]

If you would like to change the keys that are being pressed, you may change the keycode that is being sent in line 17. For example, this code sends a tab press with pin A0 is enabled, a space press when pin A1 is enabled and so on. Here is a list of available keycodes you may use. Please note, that if you add a pin in the step above, you must add a corresponding key press here are well.

keys_pressed = [Keycode.TAB, Keycode.SPACE, Keycode.ENTER, Keycode.UP_ARROW]

Connect a switch into the interface and test it out! You may connect the switch within the system preferences in Accessibility settings on Mac OS or using 3rd party software in Windows OS.