Using the M5Stack CardKB mini keyboard

The M5Stack CardKB is a full specification mini keyboard, with a built-in controller IC. It is supplied with a Grove connector and uses an I2C interface, plus the Wire library, to transfer keypress data to a connected Arduino-compatible microcontroller. Surprisingly, the entire keypad is the size of a credit card.

M5Stack CardKB mini keyboard with built-in controller IC and Grove I2C interface
Figure 1. The M5Stack CardKB is a mini keyboard with a built-in controller IC, which connects via a Grove I2C connector.

The CardKB module offers a convenient alternative to manual connection of a mini-keypad, for two key reasons – pardon the pun!

Firstly, consider the case of a manually connected 12- or 16-button keypad. This would require a separate digital I/O connection for each of the rows and columns, which is assumed here to be in addition to the standard range of general purpose inputs and outputs used with the plcLib library (X0X3, AD0AD1, Y0Y3). Typical keypad connection arrangements are illustrated below.

Hardware interfacing arrangements for 12- and 16-button keypads
Figure 2. Hardware interfacing arrangements for 12- and 16-button keypads.

Miniature keypads are arranged as 2-dimensional arrays of rows and columns. Pressing a key momentarily connects the associated row and column and this is detected by a program which scans the keypad many times per second. A 16-button keypad will have 4 rows and 4 columns, hence requiring 8 additional I/O lines, while a 12-button keypad will require an extra 7 I/O lines.

A second issue is the extra software complexity associated with scanning the keypad and detecting keypresses. However, in practice, the heavy-lifting is typically performed by a dedicated keypad library. The process of scanning the keypad is similar to the scan cycle of the plcLib library, so these should be mutually compatible, provided there are no hard-coded delays.

The CardKB unit has a dedicated ATMega8A microcontroller which performs all keyboard scanning operations and offers a simple I2C interface via a standard Grove connector. The device status may easily be read by using the Wire library. The following sketch reads the status of keys ‘1’ to ‘5’, and uses these to activate or de-activate LEDs connected to digital outputs Y0 to Y3.

Listing 1. Using a keyboard to control digital outputs (Source: Extras > M5StackCardKBDigitalOut).

The above sketch demonstrates three different approaches to using keyboard entry to control digital outputs.

  • Buttons ‘1’ and ‘2’ enable or disable output Y0, respectively. Hence two keys are used to control a single digital output.
  • Buttons ‘3’ and ‘4’ independently control outputs Y1 and Y2. Pressing a key either enables or disables the output, based on its previous state. Hence each key controls a dedicated digital output.
  • Button ‘5’ uses a ternary operator to momentarily enable an associated auxiliary variable and this in turn triggers a 0.5 second fixed width pulse, via the timerPulse command.

Which approach is best will depend on the application. Hence it is assumed that the reader will adapt the supplied sketch, as required.

Final Thoughts

There are many potential uses of keyboard input in plcLib-based applications. One possibility is to use it as an alternative to dedicated digital inputs, as shown here – most likely for testing purposes. However, the ability to update auxiliary variables based on keyboard input means that virtually any application which can accept input from a variable can also be controlled from a keyboard. Hence the range of applications of keyboard input is mainly limited by the user’s imagination!


Posted

in

, ,

by

Tags: