Button Mouse With XIAO
Greetings.
So here's an intriguing project that uses an XIAO M0 to emulate a HID mouse, which can be used in place of a traditional mouse.
As previously stated, the XIAO M0 is combined with a few buttons and a 3D enclosure to create this simple mouse.
This Project is based on the HID functionality of the Atmega SAMD21 MCU inside the XIAO, HID stands for "Human Interface Device," and it refers to a type of device that allows humans to interact with computers. Examples of HID devices include keyboards, mice, game controllers, and touchscreens.
HID is being used to enable communication between the microcontroller and a computer using USB. This allows the SAMD21 to function as a USB HID device, such as a keyboard or mouse.
The SAMD21 microcontroller has a built-in USB controller that can be configured to act as a USB HID device, allowing it to communicate with a computer through a USB port.
This is accomplished using the USB module provided by the SAMD21, which handles the USB protocol and communication with the host computer.
By configuring the SAMD21 as a HID device, the microcontroller can send and receive data to and from the computer through USB. This can be useful in a variety of applications, such as creating custom input devices or controlling computer software through a physical interface.
This article is about the whole building process of this mouse, so let's get started.
Supplies
MATERIAL REQUIRED
These are the materials used in this project
- XIAO M0 DEV Board
- Custom Enclosure 3D printed
- Tactile buttons
- Perf Board
- Wires
XIAO SAMD21 M0 DEV Board
The Seeed XIAO is a small microcontroller board that uses the SAMD21G18A microcontroller from Microchip. It is similar to an Arduino board, but smaller in size and has a different form factor. The SAMD21G18A is a powerful 32-bit ARM Cortex-M0+ processor that runs at 48 MHz, and has 256KB of flash memory and 32KB of SRAM.
The Seeed XIAO board also includes a USB port for programming and communication, as well as a variety of other features such as digital and analog I/O pins, I2C, SPI, and UART interfaces, and a built-in regulator for powering the board from a 5V source.
The Seeed XIAO board is compatible with the Arduino IDE and can be programmed using C/C++ or MicroPython. It is a great choice for projects that require a small, low-power microcontroller with plenty of I/O options.
The main goal of using the XIAO M0 is to utilise its HID functionality.
You can check out more details about the XIAO M0 below link-
https://www.seeedstudio.com/Seeeduino-XIAO-Arduino-Microcontroller-SAMD21-Cortex-M0+-p-4426.html
Seeed Fusion
Do you know, Seeed Studio also offers PCB and PCBA services?
Seeed Fusion PCB Service offers one-stop prototyping for PCB manufacture and PCB assembly and as a result, they produce superior quality PCBs and Fast Turnkey PCBA within 7 working days.
Seeed Studio Fusion PCB Assembly Service takes care of the entire fabrication process, from PCB manufacturing and parts sourcing to assembly and testing services, so you can be sure they are getting a quality product.
After gauging market interest and verifying a working prototype, Seeed Propagate Service can help you bring the product to market with professional guidance and a strong network of connections.
CO-CREATE
Also, they are currently running a co-creation campaign that lets you launch your idea into life.
All licensed projects, whether hardware, software, tutorials, or complete products, will feature on the Seeed Studio store (Bazaar) individually or with Seeed Studio's products for sale, and you will get a share of the profits. We are looking for unique projects that strongly identify with Seeed Studio's audience, solve real-life problems, fill a niche, and/or spur inspiration and creativity. Find out how you can co-create and license your ideas and talents with us now: https://www.seeedstudio.com/co-create.html
3D DESIGN OF MOUSE
To begin with this project, first its cad file was modelled in fusion360.
The design is similar to a traditional mouse, but it is smaller and has four buttons on the top side and one trigger on the right side.
The PCB contains four buttons, as well as an actuator part that assists the user in pressing the switch knob because the switch pcb is glued in place from the inside and the switch knob length is less than 2mm, which was insufficient for adding the switch directly to the body, so actuators were modelled.
The lid was also made, which closes the mouse from the bottom side.
If you want to print them, STEP files and 3D files are included. All files were created using PLA with a 0.6 mm nozzle and a layer height of 0.2 mm.
XIAO WIRING WITH BUTTONS
We begin by wiring the XIAO setup, which consists of five tactile switches, each connected to ground, and the other end of the switches are connected to the I/O Pins from D0 to D4 via connecting wires.
This setup was built on a perf board, which allowed us to connect it to the 3D enclosure.
ASSEMBLY PROCESS
- The assembly procedure was also fairly straightforward.
- We begin by installing the actuator in its place, followed by the switch pcb.
- Next, we added a small amount of hot glue to keep the PCB in place; the process was rough, but the hot glue did the trick.
- Next, we put the Xiao in place and secure the lid with three M2 screws.
- Assembly has now been completed.
CODE
Here's the code used in this build, and it's a simple one.
#include <Mouse.h>
// set pin numbers for the five buttons:
const int upButton = 0;
const int downButton = 1;
const int leftButton = 3;
const int rightButton = 2;
const int mouseButton = 4;
int range = 5; // output range of X or Y movement; affects movement speed
int responseDelay = 10; // response delay of the mouse, in ms
void setup() {
// initialize the buttons' inputs:
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(leftButton, INPUT_PULLUP);
pinMode(rightButton, INPUT_PULLUP);
pinMode(mouseButton, INPUT_PULLUP);
// initialize mouse control:
Mouse.begin();
}
void loop() {
// read the buttons:
int upState = digitalRead(upButton);
int downState = digitalRead(downButton);
int rightState = digitalRead(rightButton);
int leftState = digitalRead(leftButton);
int clickState = digitalRead(mouseButton);
// calculate the movement distance based on the button states:
int xDistance = (leftState - rightState) * range;
int yDistance = (upState - downState) * range;
// if X or Y is non-zero, move:
if ((xDistance != 0) || (yDistance != 0)) {
Mouse.move(xDistance, yDistance, 0);
}
// if the mouse button is pressed:
if (clickState == HIGH) {
// if the mouse is not pressed, press it:
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
// else the mouse button is not pressed:
else {
// if the mouse is pressed, release it:
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}
// a delay so the mouse doesn't move too fast:
delay(responseDelay);
}
sketch that reads the state of five buttons and controls the movement of a mouse cursor and the state of its left-click button based on the button states.
The setup() function is called once when the sketch starts and initializes the pins of the five buttons as inputs and sets the internal pull-up resistors to avoid floating states. It also initializes the mouse control using the Mouse.begin() function.
The loop() function is called repeatedly and reads the state of the five buttons using the digitalRead() function. The upState, downState, leftState, rightState, and clickState variables are set to the current state of the corresponding button.
The xDistance and yDistance variables are calculated based on the state of the left, right, up, and down buttons. If any of these buttons are pressed, the xDistance or yDistance variables will be set to a positive or negative value, which will be used to move the mouse cursor using the Mouse.move() function. The range variable determines the distance the mouse cursor will move for each button press.
The clickState variable is checked to see if the mouse button is pressed. If it is pressed, the Mouse.press() function is called to simulate a left-click. If it is not pressed, the Mouse.release() function is called to release the left-click button.
Finally, the responseDelay variable determines the amount of delay between each loop iteration to prevent the mouse from moving too fast.
RESULT
Here's the finished product: a working mouse that moves the mouse cursor in four directions. If we press two buttons at the same time, the mouse moves in a different angle that lies between two directions.
The trigger button is used as the mouse right button, and yes, we can easily add a left button by simply adding a new button with the XIAO's I/O Pin and editing the code slightly.
CONCLUSION
To spice things up, I fired up Halo CE and used this mouse as the primary gun control, which was a terrible idea.
Using a directional mouse in a 3D game was not a bad idea; however, the game was not playable at all.
Overall, this mouse worked, but it couldn't replace a proficient mouse or even a joystick mouse, The point was to demonstrate the HID capabilities of the XiAO M0 MCU.
This project can be built using an XIAO RP2040-based board or an RPI Pico; I've done similar projects in the past that you can see here-
https://www.instructables.com/Arduino-Game-Controller-for-NES-and-GBA/
That's all for today, folks.
Special thanks to Seeed Studio for providing XIAO for this project. Check them out if you need any electronic components at a lower cost.
That's it for now, and I'll be back soon with a brand-new project.
Peace out.