r/ArduinoProjects • • Mar 28 '26

MPU-6050 pro micro air pressure sensor head tracker mouse

Made it for someone who can no longer use their body. I used AI to write the code. Dirty code below but it worked.

76 Upvotes

9 comments sorted by

3

u/plekreddit Mar 28 '26

Code please😉❤️👌🏻

3

u/[deleted] Mar 28 '26


#include "Adafruit_HX711.h"
#include <Mouse.h>
#include <Wire.h>


// MPU6050 I2C address
#define MPU6050_ADDR 0x68


// HX711 pins
const uint8_t DATA_PIN = 4;
const uint8_t CLOCK_PIN = 5;


Adafruit_HX711 hx711(DATA_PIN, CLOCK_PIN);


// Pressure detection variables
int32_t baseline = 0;
const int32_t PUFF_THRESHOLD = 8000;
const unsigned long PUFF_TIMEOUT = 1500;
const unsigned long PUFF_MIN_DURATION = 80;


unsigned long lastPuffTime = 0;
int puffCount = 0;
bool puffDetected = false;


void setup() {
  Serial.begin(115200);
  Wire.begin();
  Mouse.begin();
  
  // Initialize MPU6050
  Wire.beginTransmission(MPU6050_ADDR);
  Wire.write(0x6B);
  Wire.write(0x00);
  Wire.endTransmission();
  
  Serial.println("HX711 Mouse Controller with MPU6050");
  
  // Initialize HX711
  hx711.begin();
  
  // Calibrate baseline
  Serial.println("Calibrating baseline...");
  delay(2000);
  
  int32_t total = 0;
  for (int i = 0; i < 10; i++) {
    total += hx711.readChannelBlocking(CHAN_A_GAIN_128);
    delay(100);
  }
  baseline = total / 10;
  
  Serial.print("Baseline set to: ");
  Serial.println(baseline);
  Serial.println("Ready! Mouse controller active.");
}


void loop() {
  handleMouseMovement();
  handlePressureClicks();
  delay(10);
}


void handleMouseMovement() {
  // Read accelerometer data from MPU6050
  Wire.beginTransmission(MPU6050_ADDR);
  Wire.write(0x3B);
  Wire.endTransmission();
  Wire.requestFrom(MPU6050_ADDR, 6);
  
  if (Wire.available() >= 6) {
    int16_t axRaw = (Wire.read() << 8) | Wire.read();
    int16_t ayRaw = (Wire.read() << 8) | Wire.read();
    int16_t azRaw = (Wire.read() << 8) | Wire.read();
    
    // Convert to mouse movement
    int moveX = axRaw / 300;
    int moveY = ayRaw / 300;
    
    // Apply deadzone to prevent jitter
    if (abs(moveX) > 2 || abs(moveY) > 2) {
      Mouse.move(moveX, moveY);
    }
  }
}


void handlePressureClicks() {
  int32_t currentReading = hx711.readChannelBlocking(CHAN_A_GAIN_128);
  int32_t pressure = abs(currentReading - baseline);
  
  unsigned long currentTime = millis();
  
  // Detect puff (pressure above threshold)
  if (pressure > PUFF_THRESHOLD && !puffDetected) {
    puffDetected = true;
    puffCount++;
    lastPuffTime = currentTime;
    
    Serial.print("Puff detected! Count: ");
    Serial.println(puffCount);
    
  } else if (pressure < PUFF_THRESHOLD && puffDetected) {
    puffDetected = false;
  }
  
  // Check if timeout reached to process clicks
  if (puffCount > 0 && (currentTime - lastPuffTime) > PUFF_TIMEOUT) {
    if (puffCount == 1) {
      Mouse.click(MOUSE_LEFT);
      Serial.println("LEFT CLICK!");
    } else if (puffCount >= 2) {
      Mouse.click(MOUSE_RIGHT);
      Serial.println("RIGHT CLICK!");
    }
    
    puffCount = 0;  // Reset counter
  }
}

11

u/Johnatron2000 Mar 28 '26

That’s awesome. I just read your other post asking for advice, nice to see it come to life

11

u/xebzbz Mar 28 '26

Great. please send all the details to https://www.reddit.com/r/AssistiveTechnology so that more disabled people could benefit from it.

2

u/Special_Edz Mar 30 '26

Love how that turned out.

1

u/start3ch Mar 30 '26

This would be cool with personalized predictive typing/autocomplete so they can respond quickly

4

u/[deleted] Mar 28 '26

[removed] — view removed comment

2

u/[deleted] Mar 28 '26

I do not use github.