I want to code my MPU6050 to measure hand tremors, and control vibration motors to vibrate based on the readings. How do I do that?
This is a code I got from Chatgpt.
Please fix the code tags properly in your post above. Where it says "type or paste code here" is where you should have pasted your code.
Did you test it?
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
const int motorPin = 9;
const float tremorThreshold = 0.2; // Adjust based on sensitivity requirements
const int pwmMax = 255; // Maximum PWM value for full intensity
void setup() {
Serial.begin(9600);
Wire.begin();
mpu.initialize();
// Check if the MPU6050 is connected
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed");
while (1);
}
Serial.println("MPU6050 connection successful");
pinMode(motorPin, OUTPUT);
}
void loop() {
int16_t ax, ay, az;
int16_t gx, gy, gz;
// Read accelerometer and gyroscope values
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Convert raw values to acceleration in g
float ax_g = ax / 16384.0;
float ay_g = ay / 16384.0;
float az_g = az / 16384.0;
// Calculate the overall acceleration magnitude
float accelMagnitude = sqrt(ax_g * ax_g + ay_g * ay_g + az_g * az_g);
// Print the values to the Serial Monitor for debugging
Serial.print("aX: "); Serial.print(ax_g); Serial.print(" g");
Serial.print("\taY: "); Serial.print(ay_g); Serial.print(" g");
Serial.print("\taZ: "); Serial.print(az_g); Serial.print(" g");
Serial.print("\tMagnitude: "); Serial.println(accelMagnitude);
// Check if the tremor threshold is exceeded
if (accelMagnitude > tremorThreshold) {
// Map the magnitude to a PWM value
int pwmValue = map(accelMagnitude * 100, tremorThreshold * 100, 300, 0, pwmMax); // Scale the magnitude
pwmValue = constrain(pwmValue, 0, pwmMax); // Ensure the PWM value is within bounds
analogWrite(motorPin, pwmValue); // Set the motor speed
} else {
analogWrite(motorPin, 0); // Turn off the vibration motor
}
delay(100);
}
I've tested it, and it can only cause the motors to vibrate when you move your hand up and down.
Thanks for posting the code correctly. Please delete post #2 now.
Did you adjust this value?
This is the original value. I did adjust it, but it either is too sensitive or motor becomes too weak.
Please post some sample output from serial monitor (using code tags) when the sensor is stationary and when sensing normal and severe tremors.
You need to ask yourself "Under what tremor conditions you want the vibration motors run?" Of course you need to know under what conditions to turn to motors off. Do you have any research suggesting that amplitude is a useful trigger point? What type of tremor are you trying to attenuate?
Using the accelerator to measure tremor is problematic. You cannot remove the gravity component from the measurement. x,y,z all will have a gravity component in their measurement.
From my own personal research into Essential Tremor and a smaller reading of Parkinson's many researchers use tremor frequency to trigger vibration and/or electrical stimulation. YMMV AMA
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.