Background
I have an older laptop that has a trackpad, but it is not clickable. You have to use the buttons below it. It is quite awkward in general, more so for drag and drop operations. Let's build some alternative mouse buttons to help. With this project you can move the mouse with your usual hand and click with your other hand. This guide covers USB HID Mouse, Debounce, and NeoKeys.
Hardware
- Feather RP2040: A powerful microcontroller board featuring the RP2040 chip. RP2040 Feather
- NeoKey 1x4: A small breakout board with four mechanical key switches, each with a NeoPixel LED. NeoKey 1x4
- USB Cable: To connect the Feather RP2040 to your computer.
- STEMMA QT Cable: To connect the NeoKey 1x4 to the Feather. SEMMA QT Cable
- Key Switches: Many to choose from. You will need 4.
- Key Caps: Your pick. You will need 4.
Setup
- Plug the key switches into the NeoKey 1x4. See NeoKey 1x4 Guide for instructions.
- Connect the NeoKey 1x4 to the RP2040 Feather using the STEMMA QT cable.
- Connect the RP2040 Feather to your computer using the USB cable.
- Install a recent version of CircuitPython: See circuitpython.org for details.
Software
Use `circup` to install the following:
- adafruit-circuitpython-neokey
- adafruit-circuitpython-debouncer
- adafruit-circuitpython-hid
# code.py import app
# SPDX-FileCopyrightText: Copyright (c) 2023 Randall Bohn (dexter) # # SPDX-License-Identifier: MIT # app.py import board from adafruit_neokey.neokey1x4 import NeoKey1x4 from adafruit_debouncer import Debouncer from adafruit_hid.mouse import Mouse import usb_hid # %% setup i2c = board.I2C() neokey = NeoKey1x4(i2c) # colors for keys IDLE=0xFF3399 ACTIVE=0xFF3333 for x in range(3): neokey.pixels[x] = IDLE neokey.pixels[3] = 0 # Mouse Buttons # 1 is the 'primary' button, 2 is the 'secondary', 3 is the 'middle'. # Left Hand Mouse: [2] [3] [1] # Right Hand Mouse: [1] [3] [2] mb1 = Debouncer(lambda: not neokey[0]) mb3 = Debouncer(lambda: not neokey[1]) mb2 = Debouncer(lambda: not neokey[2]) mouse = Mouse(usb_hid.devices) while True: mb1.update() mb2.update() mb3.update() if mb1.fell: mouse.press(Mouse.LEFT_BUTTON) neokey.pixels[0]=ACTIVE if mb1.rose: mouse.release(Mouse.LEFT_BUTTON) neokey.pixels[0]=IDLE if mb3.fell: mouse.press(Mouse.MIDDLE_BUTTON) neokey.pixels[1]=ACTIVE if mb3.rose: mouse.release(Mouse.MIDDLE_BUTTON) neokey.pixels[1]=IDLE if mb2.fell: mouse.press(Mouse.RIGHT_BUTTON) neokey.pixels[2]=ACTIVE if mb2.rose: mouse.release(Mouse.RIGHT_BUTTON) neokey.pixels[2]=IDLE
-
Setup: Initializes the I2C connection, the NeoKey1x4, and sets up initial colors for the keys.
-
Mouse Button Configuration: Uses the
Debouncer
class for each button to accurately detect press and release actions without false triggering due to "bouncing" of the switch. -
Main Loop:
- Continuously checks the state of each button.
- If a button is pressed (
fell
), it sends a corresponding mouse press command and changes the NeoPixel color toACTIVE
. - When the button is released (
rose
), it sends a mouse release command and resets the NeoPixel color toIDLE
.
-
Button Mapping:
- The first button (index 0) acts as the left mouse button.
- The second button (index 1) acts as the middle mouse button.
- The third button (index 2) acts as the right mouse button.
- The fourth button (index 3) doesn't have an assigned action and remains unlit.
Next Steps
Here are some ideas for extending the project:
- Customizable Button Functions: Add the ability to customize what each button does, such as implementing double-clicks, drag-and-drop actions, or even programmable macros.
- Wireless Capability: Explore adding wireless functionality using Bluetooth or Wi-Fi, turning it into a portable, wireless device.
- User Interface for Settings: Develop a small graphical user interface (GUI) that can run on a connected computer, allowing users to easily change settings like button mappings and LED colors.
- Sound Feedback: Integrate sound feedback for button presses using a small speaker or buzzer, which could be especially helpful for users with visual impairments.
- Battery Power: Add a battery and charging circuit to make the device fully portable.
- Ergonomic Design: Design a custom 3D-printed case that is ergonomically shaped for comfort during extended use.
- Key 4: Find a use for the fourth key.
Testing
Testing and troubleshooting are crucial steps in any electronics project. Here are some tips for effectively testing and troubleshooting your mouse button project:
Incremental Testing: Test each component separately before integrating them. First, ensure that the NeoKey 1x4 is responding to button presses, then test the mouse click functionalities.
Visual Feedback: Use the NeoPixels on the NeoKey 1x4 to provide visual feedback for button presses. This can help you quickly determine if the buttons are functioning correctly.
Console Debugging: Utilize print statements in your CircuitPython code to output debugging information to the serial console. This can help you track the state of your program and button presses.
Check Connections: Ensure all wiring connections are secure. Loose or incorrect connections are common issues in hardware projects.
Power Supply: Verify that your RP2040 Feather is receiving adequate power and that the power supply is stable.
Update Firmware and Libraries: Ensure that the Feather's firmware and all libraries (NeoKey, Debouncer, HID) are up-to-date, as older versions might have bugs or compatibility issues.
Code Review: Double-check your code for logical errors or misconfigurations, especially in the button mapping and debouncing logic.
External Testing Tools: Consider using tools like a multimeter to check for continuity and correct voltages in the circuit.
Seek Community Help: If you're stuck, consider asking for help from online communities like Adafruit Discord.
Document Issues and Solutions: Keep a record of the issues you encounter and how you resolve them. This documentation can be invaluable for future projects or for others who might work on similar projects.
Remember, troubleshooting can be a learning experience. Stay patient and methodical, and you'll likely find a solution to any issue you encounter. 🛠️🔍🎉
Credits
I referred to various guides on this project. I had to check the adafruit_hid source code for details on using the Mouse. ProtoEngineer wrote the Next Steps and Testing sections.