MPU6050 Slouch Detecting Project Code

Hello!

I am trying to create the proper code for an Arduino nano and mpu6050 to detect if someone is slouching. The location of the mpu6050 will be located on their neck through a 3d printed case. However, the math to detect if they are slouching is never right nor works. I think it's the math. Would anyone be willing to look over it? Here it is:

#include <Wire.h>
#include <MPU6050.h>
#include <math.h>
#include <SPI.h>
#define buzzer 12

// an MPU6050 object
MPU6050 IMU;
int status;
int LED1 = 3; // Red
int LED2 = 4; // Green
int LED3 = 5; // White
int buzzstate = 0;

void setup() {
Serial.begin(9600);
Wire.begin();
// Initialize MPU6050
IMU.initialize();
if (!IMU.testConnection()) {
Serial.println("IMU initialization unsuccessful");
Serial.println("Check IMU wiring or try cycling power");
while(1) {}
}

// Setting the accelerometer sensitivity (plus or minus 8g)
IMU.setFullScaleAccelRange(MPU6050_ACCEL_FS_8);
// Setting the gyroscope sensitivity (plus or minus 500 degrees per second)
IMU.setFullScaleGyroRange(MPU6050_GYRO_FS_500);

pinMode(LED1, OUTPUT); // Red
pinMode(LED2, OUTPUT); // Green
pinMode(LED3, OUTPUT); // Yellow

digitalWrite(LED1, LOW); // Red
digitalWrite(LED2, LOW); // Green
digitalWrite(LED3, LOW); // Yellow

pinMode(buzzer, OUTPUT); // Buzzer
digitalWrite(buzzer, 0);
}

void loop() {
char data;
if (Serial.available() > 0) {
data = Serial.read();
while (data != '0') {
IMU.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // Update sensor data
float accelZ = az / 16384.0; // Convert raw accel data to g's

  if (accelZ >= 0 && accelZ < 3.5) {
    Serial.print("Good posture\n");
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, HIGH);
    digitalWrite(buzzer, 0);
    buzzstate = 0;
  } else if (accelZ >= 3.5 && accelZ < 6.0) {
    Serial.print("Okay Posture\n");
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(buzzer, 0);
    buzzstate = 0;
  } else if (accelZ >= 6.0 || accelZ <= -2) {
    Serial.print("Bad posture\n");
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
    if (!buzzstate) {
      delay(5000);
    }
    digitalWrite(buzzer, 1);
    buzzstate = 1;
  }
  delay(1000);

  if (Serial.available() > 0) {
    data = Serial.read();
  }
}

if (data == '0') {
  Serial.println("Device switched off.\n");
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(buzzer, 0);
}

}
}

Thank you ahead of time :slight_smile:

You can edit your code like this:

#include <Wire.h>
#include <MPU6050.h>
#include <math.h>
#include <SPI.h>
#define buzzer 12

MPU6050 IMU;
int LED1 = 3; // Red
int LED2 = 4; // Green
int LED3 = 5; // Yellow

void setup() {
  Serial.begin(9600);
  Wire.begin();
  
  // Initialize MPU6050
  IMU.initialize();
  if (!IMU.testConnection()) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    while(1) {}
  }

  // Setting the accelerometer sensitivity (plus or minus 8g)
  IMU.setFullScaleAccelRange(MPU6050_ACCEL_FS_8);
  // Setting the gyroscope sensitivity (plus or minus 500 degrees per second)
  IMU.setFullScaleGyroRange(MPU6050_GYRO_FS_500);

  pinMode(LED1, OUTPUT); // Red
  pinMode(LED2, OUTPUT); // Green
  pinMode(LED3, OUTPUT); // Yellow

  digitalWrite(LED1, LOW); // Red
  digitalWrite(LED2, LOW); // Green
  digitalWrite(LED3, LOW); // Yellow

  pinMode(buzzer, OUTPUT); // Buzzer
  digitalWrite(buzzer, LOW);
}

void loop() {
  int16_t ax, ay, az, gx, gy, gz;
  IMU.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  // Calculate pitch angle from accelerometer data
  float accelAngle = atan2(ay, az) * 180 / PI;

  Serial.print("Accel Angle: ");
  Serial.println(accelAngle);

  if (accelAngle > -10 && accelAngle < 10) {
    Serial.println("Good posture");
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, HIGH);
    digitalWrite(buzzer, LOW);
  } else if (accelAngle >= 10 && accelAngle < 30) {
    Serial.println("Okay posture");
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(buzzer, LOW);
  } else {
    Serial.println("Bad posture");
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
    digitalWrite(buzzer, HIGH);
  }

  delay(1000);
}

Please use code tags when posting code.

Consider measuring pitch and roll tilt angles instead of acceleration values, as described here.

Example:

// minimal MPU-6050 pitch and roll (sjr). Works with MPU-9250 too.
// works with GY-521, pitch and roll signs agree with arrows on sensor module 7/2019
//
// Tested with 3.3V eBay Pro Mini with no external pullups on I2C bus (worked with internal pullups)
// Add 4.7K pullup resistors to 3.3V if required. A4 = SDA, A5 = SCL

#include<Wire.h>
const int MPU_addr1 = 0x68;
float xa, ya, za, roll, pitch;

void setup() {

  Wire.begin();                                      //begin the wire communication
  Wire.beginTransmission(MPU_addr1);                 //begin, send the slave adress (in this case 68)
  Wire.write(0x6B);                                  //make the reset (place a 0 into the 6B register)
  Wire.write(0);
  Wire.endTransmission(true);                        //end the transmission
  Serial.begin(9600);
}

void loop() {

  Wire.beginTransmission(MPU_addr1);
  Wire.write(0x3B);  //send starting register address, accelerometer high byte
  Wire.endTransmission(false); //restart for read
  Wire.requestFrom(MPU_addr1, 6); //get six bytes accelerometer data
  int t = Wire.read();
  xa = (t << 8) | Wire.read();
  t = Wire.read();
  ya = (t << 8) | Wire.read();
  t = Wire.read();
  za = (t << 8) | Wire.read();
// formula from https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing
  roll = atan2(ya , za) * 180.0 / PI;
  pitch = atan2(-xa , sqrt(ya * ya + za * za)) * 180.0 / PI; //account for roll already applied

  Serial.print("roll = ");
  Serial.print(roll,1);
  Serial.print(", pitch = ");
  Serial.println(pitch,1);
  delay(400);
}

Sorry, my first time using the forum! How do the pitch and roll angles work exactly? I don't use MPU6050 often, it's my first time.

Described in the link I posted above.