New Keyboard Shield
Overview
This guide will walk through the steps necessary to add ZMK support for a keyboard the uses a (Pro Micro compatible) addon MCU board to provide the microprocessor. The high level steps are:
- Create a new shield directory.
- Add the base Kconfig files.
- Add the shield overlay file to define the KSCAN driver for detecting key press/release.
- (Optional) Add the matrix transform for mapping KSCAN row/column values to sane key positions. This is needed for non-rectangular keyboards, or where the underlying row/column pin arrangement does not map one to one with logical locations on the keyboard.
- Add a default keymap, which users can override in their own configs as needed.
- Add support for features such as encoders, OLED displays, or RGB underglow.
- Update build.yml
It may be helpful to review the upstream shields documentation to get a proper understanding of the underlying system before continuing.
note
ZMK support for split keyboards requires a few more files than single boards to ensure proper connectivity between the central and peripheral units. Check the following guides thoroughly to ensure that all the files are in place.
New Shield Directory
note
This guide describes how to add shield to the ZMK main repository. If you are building firmware for your
own prototype or handwired keyboard, it is recommended to use your own user config repository. Follow the
user setup guide to create your user config repository first. When following the rest
of this guide, replace the app/
directory in the ZMK main repository with the config/
directory in your
user config repository. For example, app/boards/shields/<keyboard_name>
should now be
config/boards/shields/<keyboard_name>
.
Shields for Zephyr applications go into the boards/shields/
directory; since ZMK's Zephyr application lives in the app/
subdirectory of the repository, that means the new shield directory should be:
Base Kconfig Files
There are two required Kconfig files that need to be created for your new keyboard
shield to get it picked up for ZMK, Kconfig.shield
and Kconfig.defconfig
.
Kconfig.shield
The Kconfig.shield
file defines any additional Kconfig settings that may be relevant when using this keyboard. For most keyboards, there is just one additional configuration value for the shield itself, e.g.:
This will make sure the new configuration SHIELD_MY_BOARD
is set to true whenever my_board
is added as a shield in your build.
For split boards, you will need to add configurations for the left and right sides.
Kconfig.defconfig
The Kconfig.defconfig
file is where overrides for various configuration settings
that make sense to have different defaults when this shield is used. One main item
that usually has a new default value set here is the ZMK_KEYBOARD_NAME
value,
which controls the display name of the device over USB and BLE.
The updated new default values should always be wrapped inside a conditional on the shield config name defined in the Kconfig.shield
file. Here's the simplest example file.
warning
Do not make the keyboard name too long, otherwise the bluetooth advertising might fail and you will not be able to find your keyboard from your laptop / tablet.
Similarly to defining the halves of a split board in Kconfig.shield
it is important to set the ZMK_KEYBOARD_NAME
for each half of a split keyboard.
You'll also want to set which half is the central side. Most boards set it to the left.
Then on the peripheral half, you'll want to turn USB on so that it shows USB status on displays properly.
Finally, you'll want to turn on the split option for both sides. This can all be seen below.
Shield Overlays
ZMK uses the green color coded pin names to generate devicetree node references. For example, to refer to the node D0
in the devicetree files, use &pro_micro_d 0
or to refer to A1
, use &pro_micro_a 1
.
- Unibody Shields
- Split Shields
The <shield_name>.overlay
is the devicetree description of the keyboard shield that is merged with the primary board devicetree description before the build. For ZMK, this file at a minimum should include the chosen node named zmk,kscan
that references a KSCAN driver instance. For a simple 3x3 macropad matrix,
this might look something like:
(Optional) Matrix Transform
Internally ZMK translates all row/column events into "key position" events to maintain a consistent model that works no matter what any possible GPIO matrix may look like for a certain keyboard. This is particularly helpful when:
- To reduce the used pins, an "efficient" number of rows/columns for the GPIO matrix is used, that does not match the physical layout of rows/columns of the actual key switches.
- For non rectangular keyboards with thumb clusters, non
1u
locations, etc.
A "key position" is the numeric index (zero-based) of a given key, which identifies the logical key location as perceived by the end user. All keymap mappings actually bind behaviors to key positions, not to row/column values.
Without a matrix transform, that intentionally map each key position to the row/column pair that position corresponds to, the default equation to determine that is:
Which effectively amounts to numbering the key positions by traversing each row from top to bottom and assigning numerically incrementing key positions.
Whenever that default key position mapping is insufficient, the <shield_name>.overlay
file should also include a matrix transform.
Here is an example for the nice60, which uses an efficient 8x8 GPIO matrix, and uses a transform:
Some important things to note:
- The
#include <dt-bindings/zmk/matrix_transform.h>
is critical. TheRC
macro is used to generate the internal storage in the matrix transform, and is actually replaced by a C preprocessor before the final devicetree is compiled into ZMK. RC(row, column)
is placed sequentially to define what row and column values that position corresponds to.- If you have a keyboard with options for
2u
keys in certain positions, or break away portions, it is a good idea to set the chosenzmk,matrix_transform
to the default arrangement, and include other possible matrix transform nodes in the devicetree that users can select in their user config by overriding the chosen node.
Default Keymap
Each keyboard should provide an OOTB default keymap to be used when building the firmware, which can be overridden and customized by user configs. For "shield keyboards", this should be placed in the app/boards/shields/<shield_name>/<shield_name>.keymap
file. The keymap is configured as an additional devicetree overlay that includes the following:
- A node with
compatible="zmk,keymap"
where each child node is a layer with abindings
array that binds each key position to a given behavior (e.g. key press, momentarily layer, etc).
Here is an example simple keymap for the Kyria, with only one layer:
note
The two #include
lines at the top of the keymap are required in order to bring in the default set of behaviors to make them available to bind, and to import a set of defines for the key codes, so keymaps can use parameters like N2
or K
instead of the raw keycode numeric values.
Keymap Behaviors
Further documentation on behaviors and bindings is forthcoming, but a summary of the current behaviors you can bind to key positions is as follows:
kp
is the "key press" behavior, and takes a single binding argument of the key code from the 'keyboard/keypad" HID usage table.mo
is the "momentary layer" behaviour, and takes a single binding argument of the numeric ID of the layer to momentarily enable when that key is held.trans
is the "transparent" behavior, useful to be place in higher layers abovemo
bindings to be sure the key release is handled by the lower layer. No binding arguments are required.mt
is the "mod-tap" behavior, and takes two binding arguments, the modifier to use if held, and the keycode to send if tapped.
Adding Features
Encoders
EC11 encoder support can be added to your board or shield by adding the appropriate lines to your board/shield's configuration (.conf), device tree (.dtsi), overlay (.overlay), and keymap (.keymap) files.
- .conf
- .dtsi
- .overlay
- .keymap
In your configuration file you will need to add the following lines so that the encoders can be enabled/disabled:
These should be commented by default for encoders that are optional/can be swapped with switches, but can be uncommented if encoders are part of the default design.
note
If building locally for split boards, you may need to add these lines to the specific half's configuration file as well as the combined configuration file.
Testing
Once you've fully created the new keyboard shield definition, you should be able to test with a build command like:
and then flash with:
note
Further testing your keyboard shield without altering the root keymap file can be done with the use of -DZMK_CONFIG
in your west build
command,
shown here
build.yml
Updating Before publishing your shield to the public via a PR, navigate to build.yml
found in .github/workflows
and add your shield to the appropriate list. An example edit to build.yml
is shown below.
note
Notice that both the left and right halves of a split board need to be added to the list of shields for proper error checking.