Skip to content

USB HID Keyboard Example

Description

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:

    CIRCUITPY
    ├── boot_out.txt
    ├── code.py
    └── lib
        └── adafruit_hid
            ├── __init__.py
            ├── keyboard.py
            └── keycode.py
    
  3. Open the code.py 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')
                usb_keyboard.press(Keycode.A)
            else:
                print('Button is released')
                usb_keyboard.release(Keycode.A)
    
  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.

Reference

Create an Issue

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