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:
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:
The example code depends on the adafruit_hid driver. Download and extract it into your
CIRCUITPY/libfolder. This should give you the following folder structure:
CIRCUITPY ├── boot_out.txt ├── code.py └── lib └── adafruit_hid ├── __init__.py ├── keyboard.py └── keycode.py
code.pyfile 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)
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
awill output on the host computer.
Create an Issue¶
Interested in contributing to this project? Want to report a bug? Feel free to click here: