Speedometer using mpu6050


#include "Wire.h"

#include "I2Cdev.h"

#include "MPU6050.h"

#include <TimerOne.h>

 
 

MPU6050 mpu ;
 

int16_t ax, ay, az;

int16_t gx, gy, gz;



int currentValue; //defaults to zero

int previousValue; //defaults to zero

boolean firstReading = true;



int RPM = 0;

double trip = 0;

double kkbanspd = 0.00223;

float lnTime = 0;

long time;

int  count = 0;

long endTime;

long startTime;

long lastDebounceTime = 0;

long debounceDelay = 5; 



int val;

int prevVal;

 

int valax; 

int valay;

int valaz;

 

void setup()

{

    Wire.begin();

    Serial.begin(9600);

    Serial.println("Initialize MPU");

    mpu.initialize();

    Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");

   endTime = 0;

  Timer1.initialize(1000000);  // Set the timer to 60 rpm, 1,000,000 microseconds (1 second)

  Timer1.attachInterrupt(timerIsr);  // Attach the service routine here






 

}

 

void loop()

{

 

mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);


valax = map(ax, -17000, 17000, 0, 255);



Serial.println("ax: ");

Serial.println(valax);



time = millis();



if ((millis() - lastDebounceTime) > debounceDelay) {



if (firstReading)

  {

    previousValue = valax;

    currentValue = previousValue / 360;

    firstReading = false;

  }


firstReading = true;


if(previousValue == currentValue){

count++;

calculateRPM(); // Real RPM from sensor



}

}

}

void calculateRPM() {
  

  startTime = lastDebounceTime;

  lnTime = startTime - endTime;

  RPM = 60000 / (startTime - endTime);

  endTime = startTime;

  trip++;

}

void timerIsr()

{
  // Print RPM every second
  // RPM based on timer

  Serial.println("---------------");

  time = millis() / 1000;

  Serial.print(time);

  Serial.print(" RPM: ");

  Serial.println(RPM);

  Serial.print(trip);

  

  Serial.print("Spd:");

  Serial.print((RPM * kkbanspd) * 60);

  Serial.print("Km/h");

  Serial.print("Trp:");


  Serial.print("Km");



  delay(500);

  RPM = 0;

}

hi , how can i get RPM using mpu6050 please let me know what's the problem in my code

  • RPM is increasing even if the sensor is not moving .

please help me .

You shouldn’t use print in a ISR, kkbanspd should be a float .

That’s my starter for 10

thanks for your kind advice , don't know much about arduino therefore can u help me

The mpu6050 is an accelerometer sensor. It measures acceleration- not speed. The output is the same when it is at rest or when it is moving at a steady rate. There is no way the mpu6050 can give you speed or RPM data.

    if (firstReading)
    {
      previousValue = valax;
      currentValue = previousValue / 360;
      firstReading = false;
    }

    firstReading = true;

This looks wrong. If 'firstReading' is true, set it to false and immediately set it back to true.

In theory a sensor for acceleration could be used to calculate speed.
Measuring the acceleration tenthousand times per second in all three axles X,Y,Z and integrate the accerleration over time.
The practical result is unprecise as long as you don't use highspeed and very accurate sensors and a super-high-speed microcontroller that is able to do all data-aquisioning and the calculations fast enough.

So if you change to the $10000 dollar-range it might be possible.
But I guess you won't do that.
Post a description of what you want to do in the end.

  • Searching for another application for mpu6050 sensors?
  • measuring speed of a RC-car / RC-plane?
  • a two wheel active self-balancing bot?

Post an overview about your project. WIth this information alternative suggestions can be made
how to achieve your final purpose

just an overlooked log of how to slow yourself down
04.06.21 13:03 correcting setting firstread true/false
05.06.21 05:00 small post saying nothing
05.06.21 06:02 dropped one detail "sensor is rotating"
05.06.21 12:00 short answer "it rotates"

best regards Stefan

to compare first reading of sensor to another reading taken after few minutes

wow , u seems to know the sensor very well , perhaps u could suggest me . thank u for your help

I'm curious what values you will measure
best regards Stefan

i just want to measure how fast is the sensor is rotating

take an optical encoder for measuring rotational speed = rpm

Hi,
Can you please tell us what your program is for?
What is your project?
What is the application?

I'm afraid without this sort of information we cannot provide much in the way of help.

Thanks... Tom... :smiley: :+1: :coffee: :australia:

then i have to stick a led on one side and that reciever on other side then they both will need different batteries . is it accurate enough

hi , i want to detect RPM of a car using mpu6050 . is it possible to do

depends on the car

this IS definitly wrong.
just delete the line which sets the variable== true

ok i will do it

Is the sensor rotating?

To measure rotation. It is best to use some form of rpm sensor.

If you measure rpm for 1000 counts.
And measure how long it took. You can calculate rpm.

s the sensor is rotating .

and the my idea is also same but someone told me that mpu6050 cannot take readings this much faster and arduino nano can process it faster .

does the microcontroller rotate together with the sensor?