Skip to content

USB HID Keyboard Example


The USB HID Keyboard Example demonstrates use of a USB Human Interface Device (HID) driver by Adafruit.

The example code enumerates the nRF52840 M.2 Developer Kit into a HID keyboard that has an A key pressed by the USER button. The code depends on:

Before you start coding, the Python Interpreter should be installed on the nRF52840 M.2 module correctly:

Run the code

Run the USB HID Keyboard Example by performing the following steps:

  1. Connect the Module USB port to your PC as shown in the figure below. A disk drive called CIRCUITPY should be automatically detected by the computer:

  2. The example code depends on the adafruit_hid driver. Download and extract it into your CIRCUITPY/lib folder. This should give you the following folder structure:

    ├── boot_out.txt
    └── lib
        └── adafruit_hid
  3. Open the file on your CIRCUITPY drive, then copy and paste the following code:

    import time
    import board
    import digitalio
    import usb_hid
    from adafruit_hid.keyboard import Keyboard
    from adafruit_hid.keycode import Keycode
    usb_keyboard = Keyboard(usb_hid.devices)
    button = digitalio.DigitalInOut(board.USR_BTN)
    button.direction = digitalio.Direction.INPUT
    button.pull = digitalio.Pull.UP
    last_value = button.value
    while True:
        if last_value != button.value:
            last_value = button.value
            if not button.value:
                print('Button is pressed')
                print('Button is released')
  4. Your code will run as soon as the file is done saving. The board will enumerate as a HID keyboard. Press the USER button and observe that a letter a will output on the host computer.


Create an Issue

Interested in contributing to this project? Want to report a bug? Feel free to click here: