Friday, October 7, 2016

Digital input data acquisition by PC keyboard and Excel VBA


PC keyboard for data acquisition

Computer keyboard to piano keyboard

PC keyboard to piano keyboard

PC keyboard to arcade keyboard

Computer keyboard maximum number of simultaneous keys pressed together

Computer keyboard as digital input

Keyboard ghosting

One day my friend asked me if we can use a computer keyboard as a data acquisition DAQ module to monitor large number of digital inputs coming from proximity switches on Excel. The answer is below:

On the hardware level, the keyboard should be dismantled to get its encoder PCB. This why we should use a standalone keyboard.

Keyboard encoder board
On software level, to do this, we have to create an ActiveX control in a work sheet or in a user-form. Then we have to write our macro code in the event handler of keyboard event.

First of all, the user should keep the
Excel application maximized the whole time.

Once we will use the event handler of certain ActiveX control. Then, the user should not change focus of the control the whole time. The focus may be changed for some controls using mouse move, mouse key-press, or keyboard "TAB" key-press.

This means that the keyboard and mouse can no more be used for any user input in
Excel and it is not possible to interact with the software anymore.

If the digital signals are generated at high speeds (switched speed is higher than 30 Hz), then it is not possible to use the PC keyboard for this purpose since the keyboard -under Microsoft windows- has maximum input (repeat) rate of approximately 30 characters per second (30 Hz)

If the digital inputs are momentary, then there is a chance to use the keyboard as digital input module and the key-press event handler will be used. But, if the digital inputs are latching, then the key-down and key-up event handlers should be used taking in consideration that the keyboard does not support more than certain number of keys to be pressed simultaneously (this is called keyboard ghosting). The maximum number of keys to be pressed together is due to a hardware limitation and may differ from one keyboard to another because it depends on the internal circuitry of the keyboard. From ergonomics point of view, I don't expect more than 10 keys can be pressed together since the human hands has a total of 10 fingers.

Theoretically, the maximum number of keyboard keys that can be pressed together depends on the number of keyboard keys. For 101-keys keyboard with encoder having minimal number of pins, the maximum possible number of keys to be pressed together is 15 keys. This combination can be known by:

- Checking the circuit board of the keyboard
- Massive number of trials and errors. For 101 keyboard, you need to make 297,525,414,027,312,240 trials!!!

To better understand, I developed a an interactive test module to know the capability of pressing different keys together (alphabets and numbers). By trial, I figured out that the number of simultaneous keys to be pressed together depends on the keys and the order of pressing them.

In the picture shown below I could press 7 keys together (A,S,D,F,J,K,L)

In the picture below, I could achieve with assistance of my wife to press 10 keys simultaneously (she pressed only Y and U :D)

This also is an online keyboard ghosting demonstration tool from Microsoft:

Considering the keyboard a 2Dmatrix, most keyboard encoders have number of pins for columns and fewer number of pins for rows. In this case, the maximum number of combinations is almost the number of columns.

Excel VBA does not support multi-threading (parallel loops) and this means that the whole program code should be written within the event handler code.

It is preferred to set focus to non-writable or locked control, so the key press event will be tracked and no change to the user form will take place