Bluetooth Air Mouse With ESP32 & Madgwick Filter

by chandan30 in Circuits > Electronics

33 Views, 1 Favorites, 0 Comments

Bluetooth Air Mouse With ESP32 & Madgwick Filter

WhatsApp Image 2026-03-13 at 2.28.48 PM (2).jpeg

DIY 2026 Air Mouse: Gesture Control with ESP32 & Madgwick Filter

In this project, we are building a "buttonless" Air Mouse. Unlike cheap versions that drift and shake, this build uses the Madgwick Filter—a high-end sensor fusion algorithm used in drones and robotics—to ensure silky-smooth cursor movement.

🚀 Features:

  1. No-Touch Clicks: Uses capacitive touch sensors for a silent, futuristic feel.
  2. Pro Smoothing: Madgwick filter eliminates hand tremors.
  3. Bluetooth LE: No dongles needed; works with Windows, Mac, and Android.


Supplies

WhatsApp Image 2026-03-13 at 2.28.48 PM.jpeg

Step 1: Components Needed

For this build, we are using standard components that pack a punch.

  1. Microcontroller: ESP32 WROOM 32
  2. Motion Sensor: MPU6050 (6-axis IMU)
  3. Buttons: 2x TTP223 Red Capacitive Touch Modules
  4. Breadboard & Jumper Wires
  5. Battery (Optional): 3.7V Li-Po for a truly wireless experience.


Wiring

Step 2: The Wiring (Pin Mapping)

The ESP32 communicates with the MPU6050 via the I2C Protocol.

Component

Pin

ESP32 Pin

MPU6050

VCC

3V3

MPU6050

GND

GND

MPU6050

SDA

GPIO 21

MPU6050

SCL

GPIO 22

Left Touch Sensor

SIG

GPIO 4

Right Touch Sensor

SIG

GPIO 2

Libraries Required

Step 3: Setting Up the Software

To handle the "Smart" side of this project, you need to install three libraries in the Arduino IDE:

  1. ESP32-BLE-Mouse: Turns the ESP32 into a Bluetooth HID device.
  2. Adafruit MPU6050: Handles the raw data from the sensor.
  3. MadgwickAHRS: The mathematical engine for sensor fusion.


The 2026 'smart' Code

This code fuses the accelerometer and gyroscope data to keep your cursor stable.

#include <BleMouse.h>

#include <Adafruit_MPU6050.h>

#include <MadgwickAHRS.h>


Adafruit_MPU6050 mpu;

BleMouse bleMouse("2026 Pro AirMouse", "Maker-X", 100);

Madgwick filter;


// Tuning

float sensitivity = 4.0;

int deadzone = 6;

const int leftTouchPin = 4;

const int rightTouchPin = 2;


void setup() {

pinMode(leftTouchPin, INPUT);

pinMode(rightTouchPin, INPUT);

bleMouse.begin();

mpu.begin();

filter.begin(100); // 100Hz

}


void loop() {

if (bleMouse.isConnected()) {

sensors_event_t a, g, temp;

mpu.getEvent(&a, &g, &temp);


// Madgwick Sensor Fusion

filter.updateIMU(g.gyro.x * 57.29, g.gyro.y * 57.29, g.gyro.z * 57.29,

a.acceleration.x, a.acceleration.y, a.acceleration.z);


int moveX = (int)(filter.getRoll() * sensitivity);

int moveY = (int)(filter.getPitch() * sensitivity);


if (abs(moveX) < deadzone) moveX = 0;

if (abs(moveY) < deadzone) moveY = 0;


bleMouse.move(moveX, -moveY);


// Touch Logic

if (digitalRead(leftTouchPin) == HIGH) bleMouse.press(MOUSE_LEFT);

else bleMouse.release(MOUSE_LEFT);

if (digitalRead(rightTouchPin) == HIGH) bleMouse.press(MOUSE_RIGHT);

else bleMouse.release(MOUSE_RIGHT);

}

delay(10);

}

How It Works (The Science)

Step 5: How it Works (The Science)

The "Air Mouse" doesn't just read raw tilt. It uses a Madgwick Filter which calculates the Quaternion orientation of your hand.

  1. Gyroscope tracks how fast you rotate.
  2. Accelerometer uses gravity to know which way is "Down."
  3. The Filter combines them to cancel out "drift"—the annoying problem where the cursor moves even when your hand is still.


Testing & Calibration

Step 6: Testing & Calibration

  1. Upload the code and open your Laptop's Bluetooth settings.
  2. Pair with "2026 Pro AirMouse".
  3. If the cursor is too fast, decrease the sensitivity variable in the code.
  4. If it jitters, increase the deadzone.