Skip to content

Commit 0dfb04a

Browse files
Add usb_hid keyboard example and link in docs
Adds examples/usb_hid_keyboard.py with a complete USB HID keyboard demo for Raspberry Pi Zero (W), including prerequisites, wiring, and usage instructions in the docstring. Links the example in docs/examples.rst. Closes #628
1 parent 7debd39 commit 0dfb04a

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

docs/examples.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ See the `CircuitPython docs <https://circuitpython.readthedocs.io/>`_ for extens
3535
.. literalinclude:: ../examples/piblinka.py
3636
:caption: examples/piblinka.py
3737
:linenos:
38+
39+
.. literalinclude:: ../examples/usb_hid_keyboard.py
40+
:caption: examples/usb_hid_keyboard.py
41+
:linenos:

examples/usb_hid_keyboard.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# SPDX-FileCopyrightText: 2023 Björn Bösel for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
USB HID Keyboard Example for Raspberry Pi Zero (W)
7+
===================================================
8+
9+
Demonstrates how to use Blinka's ``usb_hid`` module to turn a
10+
Raspberry Pi Zero into a USB HID keyboard.
11+
12+
Prerequisites
13+
-------------
14+
1. Enable the dwc2 overlay (once, then reboot)::
15+
16+
sudo bash -c "echo 'dtoverlay=dwc2' >> /boot/config.txt"
17+
sudo reboot
18+
19+
2. Load the libcomposite kernel module::
20+
21+
sudo modprobe libcomposite
22+
23+
To make it persistent across reboots::
24+
25+
sudo bash -c "echo 'libcomposite' >> /etc/modules"
26+
27+
3. Install the CircuitPython HID library::
28+
29+
pip3 install adafruit-circuitpython-hid
30+
31+
Wiring
32+
------
33+
- Wire a button between GP20 and GND (types Shift+A).
34+
- Wire a button between GP21 and GND (types "Hello World!").
35+
- Wire an LED + 1k resistor between GP16 and GND.
36+
- Connect the Pi Zero to the host computer via USB.
37+
38+
Run with::
39+
40+
sudo -E python3 usb_hid_keyboard.py
41+
42+
The ``-E`` flag preserves the user environment so pip-installed
43+
packages are found when running as root.
44+
"""
45+
46+
import time
47+
48+
from adafruit_hid.keyboard import Keyboard
49+
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
50+
from adafruit_hid.keycode import Keycode
51+
52+
import board
53+
import digitalio
54+
import usb_hid
55+
from usb_hid import Device
56+
57+
# Button pins (activate internal pull-ups)
58+
keypress_pins = [board.D20, board.D21]
59+
60+
# What each button sends: a Keycode or a string
61+
keys_pressed = [Keycode.A, "Hello World!\n"]
62+
control_key = Keycode.SHIFT
63+
64+
# Set up button inputs with pull-ups
65+
key_pin_array = []
66+
for pin in keypress_pins:
67+
key_pin = digitalio.DigitalInOut(pin)
68+
key_pin.direction = digitalio.Direction.INPUT
69+
key_pin.pull = digitalio.Pull.UP
70+
key_pin_array.append(key_pin)
71+
72+
# Set up LED output
73+
led = digitalio.DigitalInOut(board.D16)
74+
led.direction = digitalio.Direction.OUTPUT
75+
76+
# Create the USB HID keyboard
77+
usb_hid.enable([Device.KEYBOARD], boot_device=0)
78+
keyboard = Keyboard(usb_hid.devices)
79+
keyboard_layout = KeyboardLayoutUS(keyboard)
80+
81+
print("Waiting for key press...")
82+
83+
while True:
84+
for key_pin in key_pin_array:
85+
if not key_pin.value: # Button pressed (grounded)
86+
i = key_pin_array.index(key_pin)
87+
print("Pin #%d is grounded." % i)
88+
89+
led.value = True
90+
91+
while not key_pin.value:
92+
pass # Wait for release
93+
94+
key = keys_pressed[i]
95+
if isinstance(key, str):
96+
keyboard_layout.write(key)
97+
else:
98+
keyboard.press(control_key, key)
99+
keyboard.release_all()
100+
101+
led.value = False
102+
103+
time.sleep(0.01)

0 commit comments

Comments
 (0)