Imagine having one of those
cool customizable keypad that do custom keyboard actions
for vloggers like this
But, paying $53 dollars is too much, as you can just make one of your own
using USB-HID and a few lines of code! I'm using an ESP32-S3, a new ESP32
which can emulate USB devices.
Note- Only the following boards/microcontrollers can use this code. BTW,
if you don't have one, just buy an
ESP32-S3 for 9 bucks
Development Boards | Website |
---|---|
Arduino Micro | Website: |
Arduino Leonardo | Website: |
All Adafruit RP2040 Boards | Website: |
Raspberry Pi Pico | Website: |
Arduino Uno R4 WiFi | Website: |
Arduino Uno R4 Minima | Website: |
Arduino Due | Website: |
All ESP32-S3 Based Boards | Website: |
All ESP32-S2 based boards | Website: |
ESP32-Camera | Website: |
Arduino Uno R4 Minima | Website: |
For this project- we're gonna use CircuitPython-a programming language
designed to simplify experimenting and learning to program on low-cost
microcontroller boards using Python.The steps to use it are
here
.Y'all Might know python from my blogs, so you can check them out.
Download the latest version of Python, type
python3--version && pip --version in
the terminal or Command prompt after installing python, then
download Git. Go to terminal
or command prompt. Type
pip3 install --upgrade setuptools circup --break-system-packages
&& circup install asyncio usb_hid adafruit_hid, then press
,
and you're ready!
Please note that you have to connect a cable from the board's port or
the USB port to your computer. If a new flash drive shows up, then
you're good to go! If not, recheck the Circuitpython installation steps
or change the cable with another 'Data' cable.
Go to
the online coding editor
using Opera,
Google Chrome or
Microsoft Edge ONLY.
The, click USB. Now, you can write the code.
USB-HID is the ability to emulate USB devices.
The boards listed above are all compatible with USB HID.
We will be using an official Adafruit Library-
Adafruit_hid
The documentation is easy-to-understand (take my word for it), if you want
to explore other devices, read it. We're going to make a
scroller. To make the scroller, we need a mouse.
import usb_hid
from adafruit_hid.mouse import Mouse
m = Mouse(usb_hid.devices)
is some simple boilerplate you have to use.
m.move(x=100,y=200,wheel=-20)
moves the cursor to coordinates (x=100px, y=200px) and it makes scroll
wheel move down by -20 units.
m.click(Mouse.LEFT_BUTTON) clicks the left button and
m.press(Mouse.RIGHT_BUTTON)
m.release_all() presses and releases the right-click button on a mouse. That's all!
is the only schematic required for both projects. Here are the connections.
VCC | → | Left Leg of both Push-buttons |
D4 | → | Right leg of 1st Push-button |
D13 | → | Right leg of 2nd Push-button |
import asyncio import board import digitalio import usb_hid from adafruit_hid.mouse import Mouse # Setup HID mouse mouse = Mouse(usb_hid.devices) # Set up pins 13 and 4 for scrolling lPedal = digitalio.DigitalInOut(board.IO13) #lPedal = digitalio.DigitalInOut(board.D13) on some boards rPedal = digitalio.DigitalInOut(board.IO4) #rPedal = digitalio.DigitalInOut(board.D4) on some boards for i in (lPedal,rPedal): i.direction = digitalio.Direction.INPUT i.pull = digitalio.Pull.DOWN # Interval (in seconds) for reading the pins READ_INTERVAL = 0.1 async def upHandler(): while True: # If pin 13 is high, send scroll up if rPedal.value: # wheel parameter > 0 for scrolling up, adjust the value if needed. mouse.move(wheel=1) print("up") await asyncio.sleep(READ_INTERVAL) async def downHandler(): while True: # If pin 14 is high, send scroll down if lPedal.value: # wheel parameter < 0 for scrolling down, adjust the value if needed. mouse.move(wheel=-1) print("down") await asyncio.sleep(READ_INTERVAL) #Main thing async def main(): # Create asynchronous tasks for scrolling asyncio.create_task(upHandler()) asyncio.create_task(downHandler()) while True: await asyncio.sleep(1) # Start the asyncio event loop. asyncio.run(main())
I hope you liked this blog, also check out the code on Github.
See you next time with more blogs!