Mpu6050 from value to pecentage

Hello , i'm building an water level on my 6k L tank based on the angle that the
floater makes . Inside the floater will be an mpu6050 accelerometer/gyro board ,already cover by epoxy.
So the floater it's hoked to a rigid pipe/stick and makes semi circle while goes from level high or low or the oposite as picture.
lower level 180º mpu6050 reports -1 ,90º decres reports 0 and on top reports 1. i think it's in radians .

I want convert this to percentage, but math it's not my great advantage. Can someone help me with the formula?

so -1 its 0% ,0rads it's 50% and 1 it's 100% . to be more "fun" we have negative numbers ...

Is it an integer that's returned?

y = map(Radians, -1, 1, 0, 100);

I think that the number you are looking at is one of the acceleration vectors , x, y or z.
Lets say it's it z

Then:
P = (z+1) x 50%, where P is the percentage 0% to 100%

Example: When z= - 0.5 then P=(-0.5+1) x 50% = 25%

Thank you. Map() shoud solve. i never had used map() before ,but apear that will work for this. i post the result and the code if someone want to use it,because they use ultrasonic sensors that have only a short range and this mpu6050 it's presize for measure tank level...

like i told i never used map befor and i'm getting strange "behavior": this sketch for test the mpu6050 i get an output of 50%,ALWAYS! independent of the variable "incl" .why that happens?

Get scaled and calibrated output of MPU6050
 */

#include <basicMPU6050.h> 

// Create instance
basicMPU6050<> imu;
 float incl;

 
void setup() {
  // Set registers - Always required
  imu.setup();

  // Initial calibration of gyro
  imu.setBias();

  // Start console
  Serial.begin(38400);
}

void loop() { 
  // Update gyro calibration 
  imu.updateBias();
  
  incl= imu.ay();


  Serial.print( incl );
  Serial.print( " " );


  
 int val = map(incl,-1,1,0,100);
  Serial.print(val);
  Serial.print("%");
  Serial.print( " " );
   
  // Temp
  Serial.print( imu.temp() );
  Serial.println(); 
}

output: prints inclination(x axis of mpu6050 , percentage from map conversion , temperature)

0.29 50% 32.61
0.23 50% 32.60
0.19 50% 32.61
0.16 50% 32.61
0.12 50% 32.61
0.07 50% 32.61
0.03 50% 32.61
-0.01 50% 32.60
-0.03 50% 32.60
-0.06 50% 32.61
-0.08 50% 32.61
-0.10 50% 32.61
-0.12 50% 32.60
...

While this is a little late I'll toss it out anyway. Yes, you can use your MPU6050 to measure tank level. Personally I see it as a poor choice simply because first you need to isolate all your electronics as you have done with epoxy or similar like silicone and next you still have a moving arm to pivot so you may have mechanical issues.

Given the choices as to how to go about this I would have used "The Bubbler Method of Liquid Level Measurement" . Simple ans accurate and can not only afford tank level but how much water is in your tank in liters or gallons or whatever trips your trigger. No wetted parts and no moving parts in your tank.

Ron

Map does not work, use the formula I gave in post 3

@jim-p : It works indeed. i don't know what calculation i made the first time that it didn't match...thank you
@Ron_Blain : i belive that your solution is more acurate ,but can't beat the price of an mpu6050 and this is my house tank for irrigation,not a professional solution. Just for i have an metter inside home (by rfm69 and mysensors library) just in case i forgot someting on, wasting water during the night. if the water get too low there is an external pump from underground that fills the tank again. but i'm the generation that knows ,water must be preserved..

final scketch ,if it 's usefull for someone, for serial output values:

#include <basicMPU6050.h> 

// Create instance
basicMPU6050<> imu; //credits:https://github.com/RCmags/basicMPU6050
 float incl;

 
void setup() {
  // Set registers - Always required
  imu.setup();

  // Initial calibration of gyro
  imu.setBias();

  // Start console
  Serial.begin(38400);
}

void loop() { 
  // Update gyro calibration 
  imu.updateBias();
  
  incl= imu.ay();

  Serial.print( incl );
  Serial.print( " " );

  int val = ((incl+1)*50);
  Serial.print(val);
  Serial.print("%");
  Serial.print( " " );
   
  // Temp
  Serial.print( imu.temp() );
  Serial.println(); 
}

One more question. It is possible move pin D2 (int) that is an interrupt to pin D3?
D2 is used for the rfm69(and mysensors library) for the same propose. i used basicMPU6050 library here.

Yes but you may have more options if you are using a Mega2560 or nano every

i'm using pro mini 3.3v 8mhz because the rfm69 . how can i assign new pin for INT?

How are you assigning D2

Post your code

it's just that one that i put above for now and for test the mpu6050 . shoud be coded on the library itself that it's to use D2
library: GitHub - RCmags/basicMPU6050: Basic arduino library for the MPU6050 6-axis gyro.

You can change the RFM69 IRQ to pin 3 with this
#define DEFAULT_RFM69_IRQ_PIN (3)

I don't think you can change the MPU6050 IRQ

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.