Tags

Entries in ESP32 (1)

Monday
Jun252018

A bluetooth 'chorded' keyboard

Since way back in the 90s when a friend showed me details of a 'wearable' computer, as a programmer and electronics graduate, I was intrigued that a computer could be small enough to be carried around on your belt, that the screen could be tiny and attached to some large black rimmed glasses, and that the keyboard could be held and operated in one hand using only a few buttons. At the time this was research work going on in US universities and there was no way I was ever going to get my hands on that kind of equipment.
 
More recently with the easy availability of micro-controllers and smaller footprint computers, like the Raspberry Pi, this is far more accessible. However it's not really interested me because I can pretty much do everything I want on my smartphone, so why bother carrying another computer? What has stuck in the back of my mind though was that one handed keyboard, so I did some research.
 
The original Twiddler that I'd seen are still made, and have been updated, but for me there's no fun in buying one, I should be able to build my own. These devices are referred to as chorded keyboards, as you play a 'chord', similar to a musical instrument, with the switches in order to generate a key press.
 
What did I need?
  • A controller that could be easily programmed
  • Battery power
  • Bluetooth capability
  • Some switches
 
That should do it. Please note this is not intended to be a complete step by step to building a Chorded keyboard but an overview to help others get started with ESP32 and BLE. I'll write up the step by step at a later date.
 
So, on to the process. With an initial request for #makersHelp on Twitter, I got the suggestion from Wayne Keenan (@wkeenan) to try the ESP32 Dev board. The Adafruit feather was another option but the ESP32 was a lot cheaper. It also has a huge list of features including:
  • Wifi
  • Bluetooth
  • Digital and Analog IO pins
  • I2C
  • Capacitive touch

and on and on. I expect to be building and writing about many more projects based around this board.

I split the project into manageable pieces, each a working step towards the completed whole:
  • Setup for programming the ESP32
  • Configuring the ESP32 as a bluetooth keyboard
  • Switch input as trigger for keys
  • Create the case to hold the electronics and fit in my hand
Setup for programming the ESP32
There are a lot of details on the Esspressif website about how to configure the development environment for ESP32 but I didn't want to start with a completely new set of tools. I'd previously used the Arduino IDE for programming Arduinos and Wemos boards, so was familiar with it and had it installed. Another quick Google and I found a link with some build instructions:
 
What this didn't say, but I would highly recommend, is if you already have the IDE installed but are using an earlier version than 1.8.5 then uninstall it first and the then install atleast 1.8.5. I didn't and after my first run through I couldn't actually load any code onto the ESP32.
 
Another thing I found was not all ESP32 Dev boards are created equal. The Doit board I had uploaded new code fine from the IDE. However the low cost ebay ones I purchased required that the Boot button be held down on the board for the upload to take place.
 
Once I'd got Blink running though, I knew I was on my way.
 
Configuring the ESP32 as a bluetooth keyboard
It must be possible, I'd read and been told it could be done, how hard could it be?
 
Well, once you have the RIGHT instructions actually it's not too bad. There is a lot of information out there on Bluetooth LE, a little on presenting the correct interface to make a device think you are a keyboard, and after that it's pretty much up to you!
 
First I needed to understand about BLE, and for that I found these tutorials by Andreas Spiess:
 
That got me an understanding of how to get BLE working and be visible from my phone, the nRF Connect app that Andreas mentions is invaluable.
 
For this to work though I needed the device to present itself as a Human Interface Device (HID), specifically a keyboard, and to then send key press values to whatever it was connected to. Details of how to do that I found in this thread: https://github.com/nkolban/esp32-snippets/issues/230
 
Fortunately that was some time back and these are now all available in the ESP32 BLE Arduino library that can be installed in the Arduino IDE under Sketch->Manage Libraries.

Switch input
As I'd used the Arduino IDE, configuring the input was straightforward. I configured the ports to which the switches were connected as input, wired in pull up resistors and then could read values from them. Oddly if pin <> was connected when I uploaded code then the upload failed. I got around it by connecting to a different pin, and I may come back and investigate the root cause at some point. I also then did some more reading and found that similar to the Adruino I could configure internal pull up resistors on the ESP32 to save some wiring.
 
Initially I combined the switch/key mappings from the Sparkfun code, and the keyboard example from the BLE library. This then gave me a working device! I could press the first button and get an 'a', the second and get a 'b', both together and get a 'c'.
 
However there were a number of issues here:
  • My device was constructed from 5 switches hot glued to a piece of cardboard
  • The switch presses were inconsistent and didn't always trigger the right character

More investigation required, and I needed a better case.

 
Creating a case
My initial prototype had just used switches that I found in the trove at Makespace but they were designed to be mounted on circuit boards rather then in a case. For the next iteration I selected these Adafruit ones from The Pi Hut https://thepihut.com/products/adafruit-16mm-panel-mount-momentary-pushbutton-red.
 
They were the right size, fairly cheap and could easily be mounted as I wanted. I also knew from my experimentation with the prototype that using a straight binary mapping from the switches the the alphabet was going to make typing hard. I mentioned this to some makers and was asked if this worked like a Micro Writer, having never head of it I headed back to Google.
 
Turns out that was exactly what I needed. The Microwriter was a chorded keyboard, invented in the UK in the 1970's, more details here: https://en.wikipedia.org/wiki/Microwriter. It used 5 buttons to map the the keyboard and another 'control' button to change input modes and allow for things like upper case letters, numbers, punctuation and editing. So, I ordered 6 buttons, with the control one being a different colour.
 
I am definitely not a designer, but I can create boxes in Fusion360 and add fillets to round off the corners - hence the utilitarian nature of the case currently.
 
I also know that my 3D printer is not the most accurate out there, but that doesn't matter as once I have a working version I can always print a higher quality one at Makespace. The first thing was fitting the switches and I didn't want to wait 5 hours to see on a completed box so just printed the face with the buttons in it. For the first version the holes were a little small, so I increased them and printed a box. The box comprised of two sections, one which housed 4 of the buttons, to be pressed by my fingers, and the other which housed the fifth button and the control, which were positioned so that they could be pressed separately, or together, using my thumb. I also added a power switch so that I didn't have to keep disconnecting the battery.
 
Nine hours of printing later and I had my new case. Although it easily fitted into my hand I added a velcro strap to hold it in place, and then fitted the switches, wiring, battery and controller. Which is when I discovered my next mistake - I had not taken into account the depth of the switches and with the battery in place the case didn't close! So, I remeasured, adjusted the models in Fusion360, another nine hours later I had a case that fitted.
 
Having read up on the Microwriter I decided it would be a good idea to follow their lead on the key mappings, as well as the button configuration. They appeared to have spent a lot of time thinking about the key configurations, how frequently letters are used, and also helping people learn to type using the buttons. So I updated the code to map the same configurations of buttons as the Microwriter to the same keys. I was fine for the alphabet but struggled with the other character mappings until I found this document: https://cdn.sparkfun.com/datasheets/Wireless/Bluetooth/RN-HID-User-Guide-v1.0r.pdf There is a table at the end which maps each of the characters to a numerical value which needs to be passed.
 
The Microwriter also had an interesting take on WHEN to read the keyboard input. I was reading the switches as they were pressed, which meant that if they were not all preassed at the same time I sometimes got the wrong character. The Microwriter triggers when the first switch is released, so all of the others are still in place. Once I'd changed the code to work this way the output was far more consistent with what I was trying to type.
 
My bluetooth chorded keyboard was finally working, I could connect to my phone and type emails or texts - excellent! It's not yet as fast as my thumbs, but that isn't really where I intend to use it. I started playing Elite Dangerous in VR some time back but struggled with the 'chat' window, as I had to remove my headset to see the keyboard. Once I learn to type with my knew keyboard that problem will be solved, and I'll be off to explore the universe!
 
Next steps:
  • Improve the Bluetooth - currently I have to re-pair the device every time I switch the power off and on
  • Improve the case - it's a box, it works, but I'd love some form or organic hand shaped case
  • Practice - I'm currently at about one word every 5 mins, that needs to improve!
 
Have fun.