Raspberry Pi Pico As HID Mouse
by Arnov Sharma in Circuits > Raspberry Pi
17942 Views, 20 Favorites, 0 Comments
Raspberry Pi Pico As HID Mouse
Hello Everyone.
So here's a fun little project that tackles a major issue with the Raspberry Pi Pico Microcontroller.
The Raspberry Pi Pico's pinout information is located on the bottom side of the board, which makes prototyping difficult. When we mount the device on a breadboard, we have to utilize a reference guide to determine which pins are whose, which is a messy procedure.
All the pinout details are on the top side of the expansion board I made. To showcase the Pico's HID capabilities, we connect it to an extension board and utilize it to build a simple mouse.
This Expansion board have extra 4 pins for each GPIO pins and have few extra pins on both sides which are not connected to anything, these are for placement of XYZ components for making quick prototypes.
Now that we know how to use this expansion board, let's get started with the article's discussion of the entire process.
Supplies
Following are the materials used in this built-
- RPi Pico
- Custom PCB (which was provided by PCBWAY)
- Push Buttons
- Connection wires
- PCB Standoffs
Expansion Board Design
The expansion board was designed by keeping a few things in mind which were the extra GPIO Pins for each Pin and a few extra con pins for adding stuff, another thing was that GPIO Number Details on the Topside were nice and clear.
The project's schematic was incredibly straightforward; I simply duplicated a Pico design in my CAD software and connected a CON4 pin to each of the Pico's GPIO pins.
I created a PCB design from the PCB schematic and included a hybrid pad for Pico's placement that contains a hole for header pins and a pad for mounting Pico as an SMD module on the PCB.
Getting PCBs From PCBWAY
I sent the finished PCB to PCBWAY for samples and receive them after a week.
White Soldermask with Black Silkscreen was chosen for the order since it generally makes PCBs seem beautiful.
In terms of overall quality, it was outstanding. Each of the 10 boards I bought was flawless.
I've been using their service for a while, and I must say that the PCBs I received was excellent, just as I had hoped.
Check out PCBWAY for getting great PCB service for an economic price and high quality!
Board Assembly
- Board Assembly Process begins by first adding solder paste to each component pad one by one.
- Next, using a tweezer, we pick RPi Pico and place it in the designated position, we do have to align the board according to the pads.
- Following that, we carefully lifted the entire circuit board and set it down on the Mini SMT Hotplate, which heats the PCB from below up to the solder paste melting temperature. As soon as the PCB reaches that temperature, the solder paste melts, and all the components are connected to their pads.
Due to the board's slightly larger size compared to the PCB Hotplate's pads, I had to reflow the board twice by shifting the PCB.
Pico As HID
Fun fact: The Pico offers Human Interface Device (HID) capability.
Mouse, Keyboard, Controllers, and other HID devices,
The Atmega382PU or AU used in general-purpose boards like the Arduino Nano or UNO is fantastic for prototyping for any project, but it does not support HID, so we cannot build a game console project with one of these boards. (There is another, more complicated way to use an Uno or Nano as a game controller.)
We use the Atmega32U-powered Arduino Micro or Leonardo for projects involving HID.
With better peripherals and a processor that is on par with ESP32 but without WiFi, the Raspberry Pi Pico is a direct replacement for the Micro or Leonardo.
Perhaps Pico is now superior to ESP32 because it is also offered in a WiFi version? That is debatable.
Wiring Diagram
This is the wiring connection we'll be using in the next step.
Mouse Assembly
- In order to build an HID mouse, we first take another expansion PCB, add push buttons to it, and solder the buttons into position.
- We attach one pin of each Push button to GND and then attach other pins to GPIO0, GPIO1, GPIO2, GPIO3, and GPIO4. (see the attached Wiring Diagram)
- By using Four PCB standoffs, we attach two PCBs together using four Mounting holes.
Code
For the code, I'm using HID Mouse Sketch, which is available in the Example> USB>Mouse>mousebuttons menu.
#include "Mouse.h"
// set pin numbers for the five buttons:
const int upButton = 0;
const int downButton = 1;
const int leftButton = 2;
const int rightButton = 3;
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);
}
We only need to modify the pin number and the INPUT to INPUT PULLUP so that each GPIO we use will have an internal pull-up resistor. Other than those two modifications, nothing else has to be changed.
- We must first press and hold the Bootsel Button while connecting the USB to the Pico Board in order to upload the sketch.
- After that, we just upload the sketch after choosing the board from the board manager. (Pico does not require the selection of a COM Port; leave it unchecked or blank.)
Result
Here's the result of this build: a functional HID mouse with one button for selection and four directional buttons for movement.
This configuration cannot take the place of a standard mouse. This is merely a concept to demonstrate the RP2040's capabilities and how it can take the place of Arduino-powered HID-capable microcontrollers.
I'm going to be prototyping with the RPi Pico board and utilizing this expansion PCB going forward.
I'm going to be prototyping with the RPi Pico board and utilizing this expansion PCB going forward.
I hope this article was useful, if yes, then leave a comment.
Special thanks to PCBWAY for supporting this project; do check them out for great PCB service at a lower cost.
Thanks again, and I will be back with a new project soon.