ESP-32 Based BLE Mouse With Magnetic Mouse Pad

by Neutrino-1 in Circuits > Electronics

5664 Views, 8 Favorites, 0 Comments

ESP-32 Based BLE Mouse With Magnetic Mouse Pad

IMG_20210920_224350.jpg
vlcsnap-2021-09-19-15h24m24s022.png
vlcsnap-2021-09-19-22h49m01s242.png
esp32 cam ble mouse.png
IMG_20210920_234317.jpg
IMG_20210920_225425.jpg
esp32 cam ble mouse back.png
I made the Worst Bluetooth Mouse with Magnets! [ESP32/Arduino]

This instructable is about building an esp32 based BLE mouse, unlike using the traditional method to build a mouse using optical sensors, this mouse works based on magnets embedded in a custom 3d printed mouse pad.

Make sure to read until the end of the article to know how this mouse is built from scratch and also learn how to navigate the magnetic surface using just a single magnetic pole and with a bunch of hall effect sensors!

Supplies

These are the list of products that can help you do this project with ease

(Affiliate Link)


3d printer: https://amzn.to/3trVWKw

printer upgrades: https://amzn.to/3hbK3Ga

Orange filament: https://amzn.to/3DbhpxG

Esp32: https://amzn.to/2XwQBIf

hall effect sensor: https://amzn.to/3hHz0UL

Magnets: https://amzn.to/3nMO47l

Perfboard:https://amzn.to/3EyIV8M

18650 Cell holder:https://amzn.to/3lGFsMH

18650 Cell:https://amzn.to/2Zb9Vv0

Push buttons: https://amzn.to/3EvvC9g

SMD capacitor and resistor kit:https://amzn.to/3CqQmNz

Buttons : https://amzn.to/3lDTij1

cp2102 : https://amzn.to/3hPc4CX

Tp4056 : https://amzn.to/3nHskK6

How an Optical Mouse Work?

IMG_20210917_205238.jpg
vlcsnap-2021-09-17-18h55m34s682.png

First, let’s talk about how the traditional mouse works.

If you take a look at the bottom, you may have noticed a flashy red light blasting from the mouse. This is the light that the optical sensor inside the mouse use determines the motion. But how does it work?

Initially, when I thought about it, I thought it would be something like the IR sensor used in the line follower bot or maybe a little bit more complicated like a LED and array of and Light sensors within an IC and finally maybe some fancy algorithm to detect the motion.

But when I heard the working for the first time, I was mind blown by the technology that is used. So, Let me spoil it if you haven’t already heard about it.

more info

Inside an Optical Mouse

vlcsnap-2021-09-20-10h31m08s994.png
IMG_20210919_234754.jpg
IMG_20210919_234822.jpg

If you break open the mouse you can see an IC with a small notch on the bottom and this is the casing for the camera, When you compare it to the actual camera this capture way too little detail. Because this was not meant to take pictures of people and cats, it was made to take pictures of patterns and tiny grooves on the surface where the mouse is used. Once it takes the picture it applies an algorithm called optical flow, which determines the direction of the mouse by comparing the frames of images it has taken in a continuous motion.

This really amazed me, because for the optical flow algorithm to detect which way the camera is moving the 2 consecutive frames has to be very close by. If they are far away the algorithm just doesn’t work that well. Because of this reason, the mouse optical sensor has to capture almost 2000 to 6000 frames per second. Which is far faster than the smartphone camera. So, From now on you can consider yourself a high-speed camera operator when you are a mouse.

Replacement for Optical Sensor - Failure

vlcsnap-2021-09-17-18h57m07s491.png
vlcsnap-2021-09-19-21h42m24s424.png

So, to start with the project. My initial idea was never to use magnets.

I just wanted to build a BLE Bluetooth mouse, But while designing the mouse, I soon ran into the problem of finding the Optical sensor. It was not cheap and the most familiar one to the Arduino community is now obsolete.

So my genius brain thought I could build an optical sensor using the ESP32 CAM. I would have been really happy even if I could at least get some decent frame rate, so I could change the lens of the ESP32 CAM and make a very slow/less responsive mouse. But after playing with ESP32 CAM for a while, I decided it was a very bad idea to start with ESP32 CAM.

The major reasons why ditched ESP32 CAM:

  • ESP32 CAM was unreliable in capturing high frame rate photos, the max I could push was 24 with very low resolution.
  • The second reason was the optical flow algorithm wouldn’t quite work well and crash the esp32 multiple times.
  • Finally, most esp32 cam comes with the lens locked to the sensor with super glue.

Magnets!!!

vlcsnap-2021-09-17-19h18m09s847.png
vlcsnap-2021-09-17-19h17m56s147.png

Since I'm out of options for an optical sensor, it put me in a desperate situation to find a way to detect the motion of the mouse, because reset of the electronics and the coding is way simple. It’s just about adding some buttons and integration of the BLE.

I could have definitely gone away with any one of the old methods to build the mouse. But anyway the project is not gonna be highly usable like I wanted in the beginning. So I decided to build a mouse that uses some unique way to detect motion.

Personally, experimenting with magnets was my favorite and it’s quite satisfying. Also, I have experience using magnets with electronics in my previous projects.

Making Magnets Useful

vlcsnap-2021-09-19-15h26m29s502.png
vlcsnap-2021-09-19-15h26m40s201.png
vlcsnap-2021-09-19-15h22m32s258.png
vlcsnap-2021-09-20-16h53m40s277.png

To make this magnet useful I have to convert the magnetic field into an electrical signal, for that we need to use a hall effect sensor. To be exact I will be using a 49E linear hall effect sensor.

This sensor will convert the strength of the magnetic field to an analog voltage which later we can read using a microcontroller.

After few experiments and playing with magnets and hall effect sensors, I found an appropriate distance between 2 magnets where the hall effect sensor value varies continuously when I move across them. This was very important because without this change in value detecting the motion would be quite impossible.

In my case, the distance was 3mm, it might be different for you because the value of the hall-effect sensor is based on the diameter, thickness, and material of the magnets.

Building the Magnetic Mouse Pad

vlcsnap-2021-09-19-20h51m39s177.png
vlcsnap-2021-09-19-20h51m55s188.png
vlcsnap-2021-09-17-19h17m44s842.png
vlcsnap-2021-09-17-19h17m51s006.png
vlcsnap-2021-09-19-15h25m18s606.png
vlcsnap-2021-09-19-15h24m24s022.png

I designed a pad in Fusion 360 where I can embed 49 magnets, which gives us 7 rows and 7 columns and the distance between each of the magnets is 3mm. 49 is just an arbitrary number, you can increase the number of magnets to increase the size of the mouse pad.

The next step was to 3d print. And placing the magnets in place. Just make sure to place the magnets in such a way where either all the north or south pole facing u, this will make sure we have a single-pole surface which would make it easier for us to detect the motion.

Note: For me placing the magnets in the right place was a daunting task as the magnets would collapse with each other because of the strong magnetic field. So to make it less tedious I had to use some tape to hold them in place temporarily.

Encoder Analogy

vlcsnap-2021-09-19-15h25m53s687.png
vlcsnap-2021-09-19-15h25m39s893.png
clockwise direction.png
anti-clock wise direction.png

With this current setup, we can see the logic for navigating on the magnetic monopole surface

This idea was highly inspired by the rotatory encoder mechanism. If you have worked with the rotatory encoder mechanism before, you would already know that they work on the principle of phase difference. Where either one of the signals from the encoder will be lagging or leading and by using this phase difference we can determine whether we are turning the encoder in a clockwise or anticlockwise direction. I put a link in the description where you can learn more about the rotatory encoder.

Now, similarly, we are going to use 4 single with different phases where two are used to detect left, right and the other two are used to detect up, and down.

Note : how does an encoder work

Find the Motion and Direction of the Hall Effect Sensor

vlcsnap-2021-09-20-16h53m50s755.png
magnetic field.png
vlcsnap-2021-09-19-15h23m08s152.png
vlcsnap-2021-09-19-15h24m09s757.png

At first, it’s hard to notice or a little bit hard to imagine how we can implement the encoder analogy using the magnets. But it was quite interesting to see how they work.

First, map out the magnetic field of all the magnets from the pad. This should give us this approximately a sine wave as we sweep the sensor across the grid, where the top peak of the sine wave is the maximum magnetic field read by the hall effect sensor and the bottom trough is a minimum magnetic field read by the hall effect sensor.

Now with this hall effect sensor value, we can find the motion and direction of the Hall effect sensor by comparing the past and the present reading.

Issue With Using Single Sensor

vlcsnap-2021-09-19-15h25m08s319.png
vlcsnap-2021-09-19-15h24m48s808.png
vlcsnap-2021-09-20-16h53m29s346.png

I just said we can find the direction and the motion of the Hall effect sensor just with a single sensor. So, it would be pretty easy to assume we can find all the four directions just by using 2 sensors and why brother using encoder mechanism.

But this is where things get tricky.

If I fix both sensors in the same plane and start to move the plane you can see both the hall effect sensor value changes, either if you go left to right or up to down. This makes it very complicated to determine, what direction exactly the mouse is moving.

To fix this I will add another set of Hall effect sensors. Where we’ll use two of them to detect left, right motion and the rest two for up, down motion.

Adding More Pairs of Hall Effect Sensor

vlcsnap-2021-09-19-15h36m04s137.png
vlcsnap-2021-09-19-15h36m18s465.png
vlcsnap-2021-09-19-15h24m42s703.png
vlcsnap-2021-09-19-20h52m48s967.png
vlcsnap-2021-09-19-22h49m01s242.png
vlcsnap-2021-09-19-20h53m11s913.png
vlcsnap-2021-09-20-16h54m11s970.png

This extra set of sensors will create an offset in the reading, if you see the left, right sensor reading together, either one of them will lead or lag as I move my mouse left and right. Exactly this is applicable for up and down as well. Which we could do with a single sensor as well.

The interesting part about this is, the phase shift occurs only on one set of sensors reading have the phase shift when the plane is moved in a single direction (either up/down or left/right), and for the remaining set of sensors, no phase shift occurs [ideally].

So, by arranging the sensor in this we way, we can detect all 4 direction which we use to move the mouse on the screen in all direction.

If you felt difficult to understand I suggest you, download the CAD model and try it out by yourself.

Electronics for the BLE Mouse

esp32 cam ble mouse.png
esp32 cam ble mouse back.png
esp32 cam ble mouse side.png
vlcsnap-2021-09-20-10h33m52s584.png
vlcsnap-2021-09-20-10h33m46s020.png

Anyway since the movement of the mouse was figured out, the rest of the circuit was pretty easy!

Just add some buttons, a battery, and a charging circuit for the esp32. You can take a look at the circuit diagram for a better understanding (most of these circuits are used in my previous projects).

PCB Manufacturing With PCBway

vlcsnap-2021-09-17-18h57m54s707.png
vlcsnap-2021-09-19-21h40m18s395.png
vlcsnap-2021-09-19-21h40m27s918.png
vlcsnap-2021-09-19-21h40m57s764.png

Once the circuit and the PCB layout were ready. I sent the Gerber file for manufacturing with PCBway, who generously sponsored this project.

So, If you need custom PCBs for your project, they offer 10 custom PCBs for as low as 5$ and a variety of customization that you can choose for your PCB, like the color of the solder mask, silk layer, and surface finish. And on your first order, you get a coupon of 5$! So, basically, you will be just paying for your shipping during your first order. So check out the link in the description to visit their website.

Just after a week, I received my PCBs. Also along with the PCB, I received a shiny aluminum stencil for easy soldering.

Assembly and Soldering

vlcsnap-2021-09-17-18h58m14s108.png
vlcsnap-2021-09-17-19h22m09s278.png
vlcsnap-2021-09-17-18h59m55s202.png
vlcsnap-2021-09-17-19h00m36s164.png
vlcsnap-2021-09-20-10h37m22s199.png
IMG_20210920_225444.jpg

Now, it’s time for sourcing the components, For some reason, this project had a lot of issues with getting the components, a few went missing during shipping and a few were stuck in custom for too long. Anyway, I wanted to complete this project soon, So I’m just going to go without those remaining components.

Note: I haven't included the Gerber file for this project because of the same reason. But anyhow I updated the circuit diagram with more readily/easily available components. So, I will soon try to rebuild the PCB as well.

Need to Develop This Project Into a PCB?

PCB-Cupid-logo-Design_Black_070123.png

Getting a electronics project into production would be nightmare. To ease you into the production world we have developed a platform (PCB CUPID) for PCB enthusiasts and hobbyists to ask and answer questions related to PCB design, fabrication, and assembly.

In addition to the Q&A feature, this website also has a wealth of blog posts and useful resources to help you learn about developing and manufacturing printed circuit boards. Whether you're a beginner looking for a crash course on PCB basics, or an experienced designer looking for tips and tricks, you'll find something of value on the site

So head on over and check it out, and don't forget to participate in the Q&A community to get help and share your own knowledge. Thanks!

Programming the ESP32 BLE Mouse

vlcsnap-2021-09-20-23h08m12s160.png
vlcsnap-2021-09-20-16h53m20s356.png
vlcsnap-2021-09-20-22h20m16s866.png

For the code, The first step was to clean up the signal.

Here you can use different types of filters and digital signal processing methods to clean up the noise. But to keep it simple and fast, I'm going to just smooth out the signal by averaging it. Then I took the rotary encoder example code and modified it a bit to detect the motion by using all four sensors. To be honest this is one of the worst codes I have written in a while because I lost my patient after dragging this project for way too long.

Anyway after adding some esp32 BLE library and additional code for the button we are done!

Just upload the code and it should be good to go.

Downloads

Testing the BLE Mouse

vlcsnap-2021-09-20-23h12m56s644.png
vlcsnap-2021-09-20-23h10m10s671.png
vlcsnap-2021-09-20-23h09m26s592.png
vlcsnap-2021-09-20-23h10m18s991.png
vlcsnap-2021-09-20-23h08m37s425.png

Well, I definitely thought the motion detection will be quite better. But at least it works!! And other features like the left click, right-click scroll click work very well. [I will update it with a video soon]

Final Thoughts

IMG_20210920_224358.jpg
vlcsnap-2021-09-20-23h10m18s991.png
I made the Worst Bluetooth Mouse with Magnets! [ESP32/Arduino]

This project is not perfect by any means. I would say it's just 50% working/accurate also, I’m not sure if a smooth movement like an actual mouse is possible yet, but there are a couple of things I can do. First I can go back and brush up my knowledge on filters and digital signal processing and make the signal from the sensor better to make the mouse movement smoother. Or secondly, I could experiment with other kinds of sensors, I'm currently looking at alternatives like IR sensors, lasers, etc.

Let me know in the comment section what you think about it.

I hope at least this article gave you few new ideas and a different approach to build a mouse. Make sure to read more upcoming Instructables and also make sure to check out my Youtube channel for more similar content.