Writing code for websites is all good and well but sometimes you want to do something more, well, tactile. How about a nice ominous red button that you can push and make your PC launch your missiles, self-destruct your secret base, or distract the cat?
Buttons are usually connected through the USB port. Most people have large numbers of these buttons in front of them neatly stuck to a board. It’s called a keyboard. The button we’re going to construct is very similar; however, it’s a keyboard consisting of one key only.
Your PC is going to detect the pressing of this button just like it would a normal key, but we’re going to fake it: it isn’t really a keyboard the PC is talking to. Ha! It’ll be talking to our little device which will mimic a keyboard for us. Sneaky, heh?
There are three distinct areas we’ll have to deal with: the actual button hardware, the software running inside the button and, optionally, the software on your PC to tie it all together. This project is by no means exhaustive and just intended to give you an idea of what you can do and how easy it is to build these things.
USB is pretty versatile, but it is not a patient beast
USB, or the Universal Serial Bus (1), is in effect the only real way to get hardware devices to talk to your PC. It consists of a series of hardware standards and a protocol for the plugs and signaling. Since the signaling is standardized and thus OS independent, it doesn’t matter if you’re running Windows, Linux or Mac: they all understand the devices and, if provided with the correct driver, can use them.
To join the USB playground with your device, you need to communicate over the USB port. It needs to identify itself to the host by letting it know what type of device it is (‘I’m a keyboard device’, ‘I’m a printer device’, etc.), what capabilities it has, as well as other assorted things the PC needs to be aware of. Traffic over the USB port is tied to a very strict timing scheme and is thus rather unforgiving in nature. This means that while it is possible to create your own USB connection software for hardware that doesn’t normally support it (also referred to as bit-banging (2)), this method is still prone to timing errors. This is something I kept in mind while selecting a logic board to power the button.
At the start of this little project, I considered various possible hardware options, ranging from el-cheapo controller boards from AliExpress to a Raspberry Pi 3. I ended up somewhere in the middle with the Trinket M0, created by AdaFruit (3). It brings the hardware you need: the micro-processor has the desired built-in USB support, a sufficient number of input/output pins, and it is programmable, so it can be made to mimic the desired keyboard-like behavior.
You’re also going to need a few hardware bits and bobs other than your programming fingers. At the very least you’ll need:
- Adafruit Trinket M0 (the ATSAMD21R18 version)
- a push button of some sort (non-latching is the best)
- some wiring to connect the button to the Trinket
- soldering iron, solder (the rosin-flux kind)
- USB 2.0 cable, type A Male to Micro B (you can use the cable you use to connect your Android phone to your PC if you have one).
Ideally, you have access to the following to help you experiment (not strictly necessary though):
- breadboard
- jumper cables
- alligator clamps (make connecting the button easy)
- housing to put the trinket and button in when it all works
- medical supplies (depending on how handy you are with the soldering iron).
The Trinket M0 comes with CircuitPython already installed, but if you prefer to overwrite this installation and treat it like an Arduino (4), you can do so. For this project and simplicity’s sake, I’ll use the CircuitPython that comes pre-loaded on the board. CircuitPython (5) is a MicroPython variant, ideal for little hardware projects. MicroPython is, in turn, a Python variant that is specialized for microcontrollers.
Wires and switches and pins, oh my!
We’ll connect the Trinket up like this: we’ll use the pin marked ‘2’ on the board to signal the button being pressed. The pin marked ‘1’ is used to light up the red LED to indicate that the button is being pressed; it is optional. The green LED is wired directly into the USB port’s power to signal that the board is getting power.
If, for some reason, the LED does not light up, connect it the other way around. (Yes, I did it wrong the first time around.)