Air Mouse With an ESP32 Board
by RakaAmburo in Circuits > Microcontrollers
73 Views, 0 Favorites, 0 Comments
Air Mouse With an ESP32 Board
Hello everyone! 👋 In this project, I'll show you how to create a device that emulates a mouse using an ESP32. While most air mice rely on motion sensors, I decided to take a lazier approach. The goal? Control everything with minimal arm movement while comfortably enjoying movies or YouTube tutorials. So, let's dive in and see how to build it and make it work!
Supplies
In this project, we'll use an ESP32 microcontroller powered by a 9V battery. A 7-20V to 5V voltage regulator will optimize battery usage and protect the ESP32 from power fluctuations. To control the pointer movement, we'll use a joystick with an integrated button. Additionally, we'll add three more buttons to cover the essential mouse functions, though you can include more if needed. The battery will be placed in a holder for easy replacement. And, of course, you'll need plenty of cables and a good dose of patience! Finally, I added a terminal expansion board.
Rationale
As you might have guessed by now, the idea is to build a mouse that doesn't require a surface, allowing you to use it comfortably while lounging on your couch. Typically, these devices rely on a gyroscope to detect and translate motion into pointer movement. However, for this project, I wanted to take a different approach. Since I'm incredibly lazy, my goal is to control the pointer with the least amount of movement possible—that's why we'll be using a joystick.
Connections
Here's a diagram of the connections you'll need to set up, which will guide you through the next step. Of course, you can add more buttons if you'd like to include additional functionalities.
RMB = right mouse button
LMB = left mouse button
Wiring and Prototyping
I've tried to document the entire process of prototyping, wiring, testing, and debugging to make it easier for you to follow along. Here it goes:
1,2,3: Figuring out how to mount everything in the most compact way possible.
4: Testing the regulator.
5: Testing how to fit the joystick.
6: Testing with all connected (buttons and joystick).
7: More testing with all connected but in a single board.
8: Mounting the joystick and testing again :)
As you can see, I try to approach this step by step to maintain control and avoid missing anything. There were times when everything stopped working due to a faulty connection, so be patient.
I’ve tried to focus on keeping everything organized while considering the quick replacement of components. If something breaks, it should be easy to replace. For example, the expansion board ensures that if the ESP32 loses functionality, it can be easily swapped out. The same applies to the buttons and joystick. The only component difficult to replace would be the regulator; I couldn't find a better way to position it since I had to solder the cables directly. In hindsight, a better idea might have been to solder cables with a female pin connector or something similar, to the regulator. And put male pin connectors to the cables going to the regulator and coming from the expansion board.
You can find better pictures here.
The Sketch
You can check out the code from this repo. You'll see it's not overly complex. The code includes a library that combines mouse and keyboard features (you can also use separate libraries), definitions for pins and constants, and the setup for pins, keyboard, and mouse. In the loop, we handle the reading of the joystick's X and Y positions and detect button presses. Each section includes delay thresholds to prevent double clicks and false readings.
There's also a method called moveMouseGradually, designed to gradually increase the mouse speed as the joystick approaches its maximum vector. This helps smooth mouse movement across the screen. I achieved this by mapping the joystick's maximum range (0 to 127) to the mouse's range (1 to 10), allowing you to move from one corner of the screen to the opposite in no time.
Bare in mind you can rearrange the position of these values:
In case you decide to put the joystick in a different position or orientation.
Let me add that, as you will see in the code, I programmed the left mouse button to activate ctrl before the button is pressed. As you may know, this is for opening a window as a new tab. You can change this behavior to accommodate your preferences.
Bluetooth Connection
This step is quite simple, I dare say, across all operating systems. Basically, you go to the taskbar, click on the Bluetooth icon, and select 'Add Device.' Once the board is powered with the code uploaded, you'll see a device named 'ESP32 Keyboard/Mouse.
All Toghether With Style
Finally, I aimed to arrange everything neatly, grouping the cables, securing them with a zip tie, and reinforcing the jumpers. The result is a compact and durable final product.
A final comment about the prototype:
I found wooden sticks in which I made a hole to screw it in and use it as joystick pillars. I also added a wooden piece over the ESP32 to serve as support for the joystick's fourth pillar and to hold the Page Up and Page Down buttons. I screwed from both sides the front pillars. For the bottom-right pillar, I screwed it to the joystick and left it loose at the side of the expansion board because I didn't have enough space, but it still serves as support. The remaining pillar is attached to the button board, which is supported by the expansion board sockets. Maybe you can find a way to make it better.
To conclude, another wooden part, to place the third button, was added.
Last Hour Improvements
I made some last-minute additions. You'll notice in the code that I've added this function:
Basically, it is to verify if the signal is continuous or just a false reading from the joystick. This allows me to lower the threshold and achieve smoother movements.
When the signal is checked I do:
Also, I had to add this action:
This will restart the board every 8 minutes. It’s not the most elegant solution, but I implemented it because the board tends to hang after around 8 minutes. I’m not entirely sure why—maybe it’s due to the specific board I purchased or an issue with the library. I’ll need to investigate further. I’ll update the publication as soon as I find a better fix. For now, this ensures the board stays alive indefinitely :)
Testing
In the last minutes of the video linked above, you will find all the testing I made while building the prototype until the final result.
See you in the next one!