Help with a project with MPU6050/ Nano

Hi!
New to this. Only basic knowledge.
Working on a spirit level to check precise level on video cameras.
Based on a project I found on the web
Nano/ MPU6050 lighting LED to indicate inclination degree

I know I can determine sensivity on this line

else if(angle_pitch_output >= 10 && angle_pitch_output <= 15)
  {
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
  }

Several code lines from level to máximum inclination to determine the eleven LED behavior

But this project needs:

  1. To choose from two sensivity options at the touch of a button
  2. To reset the "zero" position even if the 6050 is off level at the touch of a button

Could this be possible?

Thanks!!!

//Viral Science www.youtube.com/c/viralscience
//Arduino LED Spirit level using MPU6065

#include <Wire.h>
int gyro_x, gyro_y, gyro_z;
long gyro_x_cal, gyro_y_cal, gyro_z_cal;
boolean set_gyro_angles;

long acc_x, acc_y, acc_z, acc_total_vector;
float angle_roll_acc, angle_pitch_acc;

float angle_pitch, angle_roll;
int angle_pitch_buffer, angle_roll_buffer;
float angle_pitch_output, angle_roll_output;

long loop_timer;
int temp;

void setup() {
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);

  Wire.begin();

  setup_mpu_6050_registers();

  for (int cal_int = 0; cal_int < 1000; cal_int++) {
    read_mpu_6050_data();
    gyro_x_cal += gyro_x;
    gyro_y_cal += gyro_y;
    gyro_z_cal += gyro_z;
    delay(3);
  }

  gyro_x_cal /= 1000;
  gyro_y_cal /= 1000;
  gyro_z_cal /= 1000;
  Serial.begin(115200);
  loop_timer = micros();
}


void loop() {
  read_mpu_6050_data();
  gyro_x -= gyro_x_cal;
  gyro_y -= gyro_y_cal;
  gyro_z -= gyro_z_cal;
  angle_pitch += gyro_x * 0.0000611;
  angle_roll += gyro_y * 0.0000611;
  angle_pitch += angle_roll * sin(gyro_z * 0.000001066);
  angle_roll -= angle_pitch * sin(gyro_z * 0.000001066);
  acc_total_vector = sqrt((acc_x * acc_x) + (acc_y * acc_y) + (acc_z * acc_z));
  angle_pitch_acc = asin((float)acc_y / acc_total_vector) * 57.296;
  angle_roll_acc = asin((float)acc_x / acc_total_vector) * -57.296;
  angle_pitch_acc -= 0.0;
  angle_roll_acc -= 0.0;

  if (set_gyro_angles) {
    angle_pitch = angle_pitch * 0.9996 + angle_pitch_acc * 0.0004;
    angle_roll = angle_roll * 0.9996 + angle_roll_acc * 0.0004;
  } else {
    angle_pitch = angle_pitch_acc;
    angle_roll = angle_roll_acc;
    set_gyro_angles = true;
  }

  angle_pitch_output = angle_pitch_output * 0.9 + angle_pitch * 0.1;
  angle_roll_output = angle_roll_output * 0.9 + angle_roll * 0.1;

  Serial.print(" | Angle  = ");
  Serial.println(angle_pitch_output);
  if (angle_pitch_output > 0 && angle_pitch_output < 1) {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  } else if (angle_pitch_output >= 1 && angle_pitch_output <= 5) {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  } else if (angle_pitch_output >= 5 && angle_pitch_output <= 10) {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  } else if (angle_pitch_output >= 10 && angle_pitch_output <= 15) {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  } else if (angle_pitch_output >= 50 && angle_pitch_output <= 65) {
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  } else if (angle_pitch_output >= 65 && angle_pitch_output <= 90) {
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  } else if (angle_pitch_output <= 0 && angle_pitch_output >= -5) {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  } else if (angle_pitch_output <= -5 && angle_pitch_output >= -20) {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  } else if (angle_pitch_output <= -20 && angle_pitch_output >= -35) {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  } else if (angle_pitch_output <= -35 && angle_pitch_output >= -50) {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  } else if (angle_pitch_output <= -50 && angle_pitch_output >= -65) {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
    digitalWrite(13, LOW);
  } else if (angle_pitch_output <= -65 && angle_pitch_output >= -90) {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
    digitalWrite(13, HIGH);
  }

  while (micros() - loop_timer < 4000)
    ;
  loop_timer = micros();
}





void setup_mpu_6050_registers() {
  //Activate the MPU-6050
  Wire.beginTransmission(0x68);
  Wire.write(0x6B);
  Wire.write(0x00);
  Wire.endTransmission();
  Wire.beginTransmission(0x68);
  Wire.write(0x1C);
  Wire.write(0x10);
  Wire.endTransmission();
  Wire.beginTransmission(0x68);
  Wire.write(0x1B);
  Wire.write(0x08);
  Wire.endTransmission();
}


void read_mpu_6050_data() {
  Wire.beginTransmission(0x68);
  Wire.write(0x3B);
  Wire.endTransmission();
  Wire.requestFrom(0x68, 14);
  while (Wire.available() < 14)
    ;

  acc_x = Wire.read() << 8 | Wire.read();
  acc_y = Wire.read() << 8 | Wire.read();
  acc_z = Wire.read() << 8 | Wire.read();
  temp = Wire.read() << 8 | Wire.read();
  gyro_x = Wire.read() << 8 | Wire.read();
  gyro_y = Wire.read() << 8 | Wire.read();
  gyro_z = Wire.read() << 8 | Wire.read();
}

That is not sensitivity, that is range (meaning "between 10 and 15").

Define these.

Do you mean "two range options" in the same line as my above statement?

Perhaps you could enlighten us on this magical device?

Hi!!!
Thanks for the answer. I´m sorry. Yes, range.
In this case this would mean sensitivity: if the range is too broad, it would mean the sensitivity is low in this specific application. That´s what I meant. Thanks.

Hi again
Yes. The possibility of using two different set of ranges at the touch of a button
Could this be possible?
Thanks!

Hi
What I´m trying to achieve is a spirit level that indicates the degree of inclination with a set of LED. I could basically achieve that.
But I need it to be very senstive (using a short range) so from level to maximun indication shows more or less 5 degrees. I can adjust that modifyng the code I could get, but:
In certain situations I would need to use a broader range. I´m asking help to switch two sets of ranges at the touch of a button. I don´t know if it´s possible.
And I would need to re-adjust the indication of zero (center, level position) even if the MPU6050 is off-level at the touch of a button. Wondering how could achieve this
Thanks!!

You asked the same questions, twice.

Yes, you can use a button to change constant multipliers for angle_pitch and angle_roll.

You could store the current position/angle as the "center" position/angle (using "center" to avoid confusion of "center" zero versus "0" zero).

The "constant multipliers" that I mention are in these lines with "hard coded" numbers (their meaning is explained within the link, below)...

  angle_pitch += gyro_x * 0.0000611;
  angle_roll += gyro_y * 0.0000611;
  angle_pitch += angle_roll * sin(gyro_z * 0.000001066);
  angle_roll -= angle_pitch * sin(gyro_z * 0.000001066);
.
.
  angle_pitch_acc = asin((float)acc_y / acc_total_vector) * 57.296;
  angle_roll_acc = asin((float)acc_x / acc_total_vector) * -57.296;
.
.
    angle_pitch = angle_pitch * 0.9996 + angle_pitch_acc * 0.0004;
    angle_roll = angle_roll * 0.9996 + angle_roll_acc * 0.0004;

... from which, the entire code is a direct rip-off from Ralph Bacon's git repository the "youtube author" failed to acknowledge. MPU6050_Accelerometer_Gyro/GryoFunctions.h at master · RalphBacon/MPU6050_Accelerometer_Gyro · GitHub

The "youtube author" either had "perfect" accelerometers, or failed when copy-pasting these lines from Ralph Bacon...

  angle_pitch_acc -= 0.0;
  angle_roll_acc -= 0.0;

... where Ralph's factory setting are...

	angle_pitch_acc -= -0.62;                                         //Accelerometer calibration value for pitch
	angle_roll_acc -= -1.77;                                          //Accelerometer calibration value for roll

Maybe... do not use buttons, but try just using angle_pitch_output as your "sensitivity" adjust... In your code, you use a float variable , but only compare its integer (not using the decimal part of the float)...

  if (angle_pitch_output > 0 && angle_pitch_output < 1) {

If angle_pitch_output is less than 1 (and greater than -1), you could use the decimal portion of the value along the full-scale of your LEDs... during this "-1< value < 1" you could light the FIRST and LAST LED to show "super sensitive scale" and use a single LED to show the decimals 0.1 to 0.9.

Maybe something like this... values between -1 and 1 show as INVERSE on the scale... [EDITED: corrected decimal scale]

byte ledPin[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // in showscale();
.
.

void setup() {
.
.
}

void loop() {
.
.

  if (angle_pitch_output == 0)
      bits = 0b00000100000;

  if (angle_pitch_output >= -1 && angle_pitch_output < 0) {
    if (angle_pitch_output > -1.0 && angle_pitch_output <= -0.8)
      bits = 0b01111011111;
    if (angle_pitch_output > -0.8 && angle_pitch_output <= -0.6)
      bits = 0b10111011111;
    if (angle_pitch_output > -0.6 && angle_pitch_output <= -0.4)
      bits = 0b11011011111;
    if (angle_pitch_output > -0.4 && angle_pitch_output <= -0.2)
      bits = 0b11101011111;
    if (angle_pitch_output > -0.2 && angle_pitch_output <= -0.0)
      bits = 0b11110011111;
  }

  if (angle_pitch_output > 0 && angle_pitch_output < 1) {
    if (angle_pitch_output < 1.0 && angle_pitch_output >= 0.8)
      bits = 0b11111011110;
    if (angle_pitch_output < 0.8 && angle_pitch_output >= 0.6)
      bits = 0b11111011101;
    if (angle_pitch_output < 0.6 && angle_pitch_output >= 0.4)
      bits = 0b11111011011;
    if (angle_pitch_output < 0.4 && angle_pitch_output >= 0.2)
      bits = 0b11111010111;
    if (angle_pitch_output < 0.2 && angle_pitch_output >= 0.0)
      bits = 0b11111001111;
  }


  if (angle_pitch_output >= 1 && angle_pitch_output < 5)
    bits = 0b00001000000;
  if (angle_pitch_output >= 5 && angle_pitch_output < 15)
    bits = 0b00011000000;
  if (angle_pitch_output >= 15 && angle_pitch_output < 35)
    bits = 0b00111000000;
  if (angle_pitch_output >= 35 && angle_pitch_output < 65)
    bits = 0b01111000000;
  if (angle_pitch_output >= 65 && angle_pitch_output < 90)
    bits = 0b11111000000;

  if (angle_pitch_output <= 1 && angle_pitch_output > -5)
    bits = 0b00000010000;
  if (angle_pitch_output <= -5 && angle_pitch_output > -10)
    bits = 0b00000011000;
  if (angle_pitch_output <= -15 && angle_pitch_output > -35)
    bits = 0b00000011100;
  if (angle_pitch_output <= -35 && angle_pitch_output > -65)
    bits = 0b00000011110;
  if (angle_pitch_output <= -65 && angle_pitch_output > -90)
    bits = 0b00000011111;

  showscale(bits);
}

void showscale(int leds) {
  for (byte i = 0; i < 11; i++)
    digitalWrite(ledPin[i], bitRead(leds, i));
}

I use this...

  if (angle_pitch_output >= 1 && angle_pitch_output < 5)
    bits = 0b00001000000;

to represent this in your code...

  if (angle_pitch_output > 1 && angle_pitch_output < 5) {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);

Thank you everyone
That´s was a lot of help
I will work it out from that
Thanks again

I made some coding errors in Post #10 (edited and corrected)... here is the complete, correct code with corrections to Post #10...

// https://forum.arduino.cc/t/help-with-a-project-with-mpu6050-nano/1421336
// https://www.youtube.com/watch?v=wEXvfCl8LiY
// https://www.viralsciencecreativity.com/post/arduino-mpu6050-digital-spirit-level-measuring-device

//Viral Science www.youtube.com/c/viralscience
//Arduino LED Spirit level using MPU6065

#include <Wire.h>

boolean set_gyro_angles;
int gyro_x, gyro_y, gyro_z;
long gyro_x_cal, gyro_y_cal, gyro_z_cal;

long acc_x, acc_y, acc_z, acc_total_vector;
float angle_pitch, angle_roll, angle_roll_acc, angle_pitch_acc, angle_pitch_output, angle_roll_output;;

long loop_timer;
int temp;

byte ledPin[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // in showscale();
int bits;

void setup() {
  Serial.begin(115200);
  for (byte i = 3; i < 14; i++)
    pinMode(i, OUTPUT);
  Wire.begin();

  setup_mpu_6050_registers();
  for (int cal_int = 0; cal_int < 1000; cal_int++) {
    read_mpu_6050_data();
    gyro_x_cal += gyro_x;
    gyro_y_cal += gyro_y;
    gyro_z_cal += gyro_z;
    delay(3);
  }
  gyro_x_cal /= 1000;
  gyro_y_cal /= 1000;
  gyro_z_cal /= 1000;

  loop_timer = micros();
}

void loop() {
  read_mpu_6050_data();

  gyro_x -= gyro_x_cal;
  gyro_y -= gyro_y_cal;
  gyro_z -= gyro_z_cal;

  angle_pitch += gyro_x * 0.0000611;
  angle_roll += gyro_y * 0.0000611;
  angle_pitch += angle_roll * sin(gyro_z * 0.000001066);
  angle_roll -= angle_pitch * sin(gyro_z * 0.000001066);

  acc_total_vector = sqrt((acc_x * acc_x) + (acc_y * acc_y) + (acc_z * acc_z));
  angle_pitch_acc = asin((float)acc_y / acc_total_vector) * 57.296;
  angle_roll_acc = asin((float)acc_x / acc_total_vector) * -57.296;
  angle_pitch_acc -= 0.0;
  angle_roll_acc -= 0.0;

  if (set_gyro_angles) {
    angle_pitch = angle_pitch * 0.9996 + angle_pitch_acc * 0.0004;
    angle_roll = angle_roll * 0.9996 + angle_roll_acc * 0.0004;
  } else {
    angle_pitch = angle_pitch_acc;
    angle_roll = angle_roll_acc;
    set_gyro_angles = true;
  }

  angle_pitch_output = angle_pitch_output * 0.9 + angle_pitch * 0.1;
  angle_roll_output = angle_roll_output * 0.9 + angle_roll * 0.1;
  Serial.print(" | angle_pitch_output  = ");
  Serial.println(angle_pitch_output);

  if (angle_pitch_output == 0)
      bits = 0b00000100000;

  if (angle_pitch_output >= -1 && angle_pitch_output < 0) {
    if (angle_pitch_output > -1.0 && angle_pitch_output <= -0.8)
      bits = 0b01111011111;
    if (angle_pitch_output > -0.8 && angle_pitch_output <= -0.6)
      bits = 0b10111011111;
    if (angle_pitch_output > -0.6 && angle_pitch_output <= -0.4)
      bits = 0b11011011111;
    if (angle_pitch_output > -0.4 && angle_pitch_output <= -0.2)
      bits = 0b11101011111;
    if (angle_pitch_output > -0.2 && angle_pitch_output <= -0.0)
      bits = 0b11110011111;
  }

  if (angle_pitch_output > 0 && angle_pitch_output < 1) {
    if (angle_pitch_output < 1.0 && angle_pitch_output >= 0.8)
      bits = 0b11111011110;
    if (angle_pitch_output < 0.8 && angle_pitch_output >= 0.6)
      bits = 0b11111011101;
    if (angle_pitch_output < 0.6 && angle_pitch_output >= 0.4)
      bits = 0b11111011011;
    if (angle_pitch_output < 0.4 && angle_pitch_output >= 0.2)
      bits = 0b11111010111;
    if (angle_pitch_output < 0.2 && angle_pitch_output >= 0.0)
      bits = 0b11111001111;
  }

  if (angle_pitch_output >= 1 && angle_pitch_output < 5)
    bits = 0b00001000000;
  if (angle_pitch_output >= 5 && angle_pitch_output < 15)
    bits = 0b00011000000;
  if (angle_pitch_output >= 15 && angle_pitch_output < 35)
    bits = 0b00111000000;
  if (angle_pitch_output >= 35 && angle_pitch_output < 65)
    bits = 0b01111000000;
  if (angle_pitch_output >= 65 && angle_pitch_output < 90)
    bits = 0b11111000000;

  if (angle_pitch_output <= -1 && angle_pitch_output > -5)
    bits = 0b00000010000;
  if (angle_pitch_output <= -5 && angle_pitch_output > -10)
    bits = 0b00000011000;
  if (angle_pitch_output <= -15 && angle_pitch_output > -35)
    bits = 0b00000011100;
  if (angle_pitch_output <= -35 && angle_pitch_output > -65)
    bits = 0b00000011110;
  if (angle_pitch_output <= -65 && angle_pitch_output > -90)
    bits = 0b00000011111;

  showscale(bits);

  while (micros() - loop_timer < 4000);
  loop_timer = micros();
}

void setup_mpu_6050_registers() {
  //Activate the MPU-6050
  Wire.beginTransmission(0x68);
  Wire.write(0x6B);
  Wire.write(0x00);
  Wire.endTransmission();
  Wire.beginTransmission(0x68);
  Wire.write(0x1C);
  Wire.write(0x10);
  Wire.endTransmission();
  Wire.beginTransmission(0x68);
  Wire.write(0x1B);
  Wire.write(0x08);
  Wire.endTransmission();
}

void read_mpu_6050_data() {
  Wire.beginTransmission(0x68);
  Wire.write(0x3B);
  Wire.endTransmission();
  Wire.requestFrom(0x68, 14);
  while (Wire.available() < 14);
  acc_x = Wire.read() << 8 | Wire.read();
  acc_y = Wire.read() << 8 | Wire.read();
  acc_z = Wire.read() << 8 | Wire.read();
  temp = Wire.read() << 8 | Wire.read();
  gyro_x = Wire.read() << 8 | Wire.read();
  gyro_y = Wire.read() << 8 | Wire.read();
  gyro_z = Wire.read() << 8 | Wire.read();
}

void showscale(int leds) {
  for (byte i = 0; i < 11; i++)
    digitalWrite(ledPin[i], bitRead(leds, i));
}

Thank you! I will test it right now
Thanks again