Introduction

Sometimes it gets tiresome remembering keyboard combinations for software applications on the PC. Examples include CAD software which are very feature-rich. One could place labels/stickers onto the keyboard but maybe the keyboard isn’t sufficient for productivity. It would be nice to be able to have additional custom buttons or a keypad that could be attached to the PC. Electrically it should look like a normal USB keyboard as far as the PC is concerned, so that existing software applications on the PC do not need to be modified and so that it can work on any computer including Windows, Linux (including the Pi) and Mac machines.

 

I recently needed USB keypad functionality (I actually wanted to attach a footswitch to change the PC screen view while working on circuits), and so I started investigating what was already out there. I was quite shocked to see the prices, with some of the programmable USB keypads with in-built memory costing $50-$100! I also wanted to be able to consolidate some additional software and run it all in a single microcontroller.

 

Anyway, I felt that with low-cost, high performance microcontroller development boards costing next-to-nothing, we could do better and cheaper. There are many Arduino USB keyboard projects on the Internet but just for a change, I chose a NXP processor. The NXP FRDM boards are easier (and often cheaper) to use these days than the Arduino boards and environment.

 

So, I took one of my favourite boards, the FRDM-KL25ZFRDM-KL25Z and set to work. This blog post will be really short because this is a very trivial project, only eight wires need soldering. This project is suitable for complete beginners.

 

With a small amount of DIY effort, perhaps 3D printing, I'm hoping the final result could look just as good or better than an off-the-shelf USB keypad

The project uses a 4x4 keypad4x4 keypad but it could be replaced with any switches/buttons if desired.

usb-keyboard-project-s1.jpg

 

What Can it be Used For?

There are many scenarios where a USB keypad can come in handy.

 

  • Many laptop keyboards do not have a separate numeric keypad, so it could be useful for those who need to enter many numbers into a spreadsheet.
  • As mentioned earlier, it could also be handy for use with CAD software (such as EAGLE) where you may wish to have a dedicated keypad for special shortcuts. As an example, I can now press the ‘A’ key on the keypad and have it automatically invoke the correct key code to bring up the dialog to add a component to a schematic in EAGLE. Things like this will increase productivity.
  • Another use is to have buttons for a Raspberry Pi, for an embedded application where you may not need a full keyboard. An example would be a kiosk application.
  • Yet another use is to have non-traditional inputs to interface to software running on a PC. An example of this could be a foot-switch, or a novel keypad for a show event or competition.

 

Building the Hardware

There is very little to do. I soldered some header pins onto the FRDM-KL25Z, and then wired a 4x4 keypad matrix board to the I/O pins. That’s it!

The FRDM-KL25Z board is very thin (less than 7mm) without header pins, so for a slim keypad the wires could be soldered on directly without using header pins, and the board could be mounted underneath the keypad.

usb-keypad-diag.png

 

Software Development and Programming Overview

The FRDM-KL25Z board is ‘mbed enabled’. That means that it is possible to do all the software development and compilation using a web browser, with no need to download and install any software. This is really convenient and means that any PC can be used for development (even a mobile phone or tablet).

 

When the board is plugged into the PC, it appears as a USB memory stick. To program the board, the compiled program (downloaded from the web browser) can be drag-and-dropped over the USB memory drive. It is that simple. Note that this requires Windows or Linux or a Mac. I’ve not succeeded in doing this using Android so far.

 

Upgrading the FRDM board Bootloader and OpenSDA Firmware

The FRDM board as mentioned looks like a USB memory stick to the PC, and can be programmed using a drag-and-drop procedure. The FRDM board achieves this through two pieces of software which are pre-installed as shipped on the board; a bootloader and a debug application known as OpenSDA. Both are usually old and need upgrading. This only needs to be done once when you first receive the board.

 

Note that the upgrade procedure needs Windows version 7 or a Mac and may work with Linux. It doesn’t work with Windows 8 upwards unfortunately (although it is worth googling, some people may have found workarounds).

 

After the upgrade is complete, Windows 8 upwards can be used.

 

The procedure is:

  1. Go to the PE micro website and download the OpenSDA Firmware MSD and Debug 'Firmware Apps' zip file (you will need to either register, or supply an e-mail address to get a one-time download link to the firmware bundle)
  2. Unzip it, and also unzip the ‘OpenSDA Bootloader Update App’ zip file that is within the first zip file.
  3. Hold down the Reset button on the FRDM board and plug the board to the PC. There are two USB sockets on the FRDM board. You need to use the one marked ‘SDA’. Release the button. The board should appear on the PC as a USB memory drive titled ‘BOOTLOADER’.
  4. In the extracted and unzipped files, find the file titled ‘BOOTUPDATEAPP_Pemicro’. Drag that to the memory drive. Wait a few seconds for the transfer to complete, then unplug and re-plug the FRDM board into the PC. No need to use the Reset button this time, it is not needed from now on. The board should appear as BOOTLOADER again.
  5. In the extracted and unzipped files, find the file titled ‘MSD-DEBUG-FRDM-KL25Z_Pemicro’. There are many with near-similar names so it is important to find the one that explicitly has FRDM and KL25Z in the title.  Drag that to the USB memory drive. Wait a few seconds for the transfer to complete.
  6. Unplug and re-plug the FRDM board into the PC. The board should now appear as a USB memory drive with the name FRDM-KL25Z, not BOOTLOADER. The upgrade process is complete.

 

 

Working with the Online Tools

Go to developer.mbed.org and set up an account, and then in the top menu select Hardware->Boards and find the NXP FRDM-KL25Z board. Click on it to bring up an entire page about the board and then look on the right side about half-way down the web page for an orange button with the text ‘Add to your mbed Compiler’ and click on that too.

 

Next in the top menu select ‘Compiler’ to bring up a complete development environment in your web browser.

mbed-banner2-annotated.png

 

The development environment looks like this:

using-mbed2.png

 

To create a new program, click on New and in the New Program pop-up window select the FRDM-KL25Z board to be the platform, leave the Template at the default (‘Blinky LED Hello World”) and change the Program Name to (say) my_usb_keyboard.

 

In the source code pane you’ll see a main.cpp file. Double-click on it to open it, and there will be a short program there that blinks an LED. If you click on Compile then the software will be built and automatically downloaded to your PC file system as a file called mbed_my_usb_keyboard_KL25Z.bin.

 

You can test it by dragging it over the USB memory if the FRDM board is plugged in. After a few seconds the software will be transferred and will automatically start running on the FRDM board. This demo program will make the RGB LED on the board blink red. The code is now in Flash memory and so unplugging and re-plugging the board into the PC will retain the code and the LED will continue to flash. Changing the program on the board is simple, just drag another file onto the USB memory file system.

 

Compiling and Installing the USB Keypad Software

The USB Keypad software requires a USB device library. Click on the top-level project name (my_usb_keyboard for example) in the projects list on the left side, and then right-click on it and select Import Library->From URL. In the pop-up window type the following in the Source URL box and then click on Import: https://developer.mbed.org/users/mbed_official/code/USBDevice/

 

In the source code view double-click on main.cpp to open it. The USB keypad software here can be copied and then pasted into the main.cpp file in the source code view. Click on Save and then click Compile. The compiled result will automatically be downloaded to the PC. Drag it onto the USB memory as before and it will be programmed into the board.

 

Testing the USB Keypad

Open up a text editor (e.g. Notepad on Windows) and click in it so that it has focus. Unplug the programmed FRDM board and now plug it in using the other USB socket on the board, marked USBKL25Z on the board silkscreen. This USB connector is directly connected to the programmed microcontroller, and will look like a USB keyboard to the PC when it is plugged in.

 

Press any button on the keypad and you should see the appropriate character appear in the text editor!

 

Customizing the USB Keypad

Currently the keys are hard-coded. It would be possible to have a configuration file or some other method to modify it in future. To change the hard-coded values it is necessary to modify some values in the main.cpp file, save it, compile it and then drag it back over onto the FRDM board USB memory (don’t forget to use the USB port marked SDA to do this).

 

Here is the area to modify; search for the location marked ‘constants’, it is near line 43. There are two arrays there, one is called chardefault and the other is called modifierdefault.

 

Place the character mappings in chardefault. If you wish a control, shift or alt button to be simultaneously sent out, then place the text KEY_CTRL, KEY_SHIFT or KEY_ALT in the modifierdefault array. If you don’t want any modifier then use the value 0. The current code as an example sends Ctrl-A whenever the ‘A’ button on the keypad is pressed. See the snippet below, it is fairly self-explanatory.

 

const char chardefault[4][4]={  {'1', '2', '3', 'a'},
                                {'4', '5', '6', 'B'},
                                {'7', '8', '9', 'C'},
                                {'*', '0', '#', 'D'}};


// valid values for modifier: 0, KEY_CTRL, KEY_SHIFT, KEY_ALT
const char modifierdefault[4][4]={  {0, 0, 0, KEY_CTRL},
                                    {0, 0, 0, 0},
                                    {0, 0, 0, 0},
                                    {0, 0, 0, 0}};

 

Summary and Next Steps

With the FRDM board it is possible to create a USB keypad in half an hour, with very little soldering and easy-to-use mbed online development environment, and save a lot of money! Plus the solution is completely customizable since the source code is available.

 

With some modifications it would be possible to send entire strings if desired. I didn’t have an immediate requirement for this. If you make any interesting changes or spot any bugs, or come up with useful key mappings for particular software, please do share the details.