Super Cheap Accelerometer with Arduino!

Measure g-forces on your car with Arduino!

  • Idea
Lets say you want to measure the g-Forces your car/bike experiences while you accelerate, take a corner, do drag race or go through a speed bump. For this you have to use a VBOX, DRAGGY or some other device for getting the acceleration(g-force) values. But the problem with these devices is that they're too expensive, like $300 and upwards. If you are not into pro kind of stuff & just want to know g-force values without spending too much, this can be useful for you. Although this is currently a bit crude, it can be improved a lot if you want to. Nonetheless, this is a fairly easy and straightforward project for its purpose.

  • Demonstration
Before getting started with the details of project, have a look at this demonstration video. Because this is the final result of this project. You will be obtaining 5 values on your laptop (yes you have to carry your laptop/MacBook with you, g-values would be displayed in it only)- instantaneous acceleration in x, y, z axes separately and maximum acceleration in x & y axes. 

                                       
Here I tested this project on my car and it did performed exactly as it should've. 
  • Components required
  1. For measuring acceleration, an MPU6050 module is required. This is actually a triple axis accelerometer + triple axis gyroscope + temperature sensor, all in one! 
  2. For processing data, an Arduino Uno board.
  3. Few jumper wires for connection and some double sided tape or glue for fixing the accelerometer to the car.
Only this much is required! I'm sure these can be obtained for less than $30. Cheap, isn't it?

  • circuit connections
  1. Firstly, VCC of mpu6050 is connected to 5V pin of Arduino.
  2. GND of mpu6050 is connected to GND of Arduino.
  3. The SCL pin of mpu6050 goes with A5 of Arduino.
  4. The SDA pin of mpu6050 goes with A4 of Arduino.
  5. Finally, the ADO is grounded i.e connected to ground.             died

Simply put, just connect everything as is shown in the given circuit diagram. It can't get any simpler than this😁

  • code to get it work
  1. If you don't have Arduino IDE, make sure to install it. Its available for free on Arduino's official website. Here it is Arduino IDE
  2. Once installed, just open it and paste the following code 
  3.  Link to this code is this . You can copy it either from the link or from here.

//module MPU6050 has gyroscope too, but i'm currently reading only accln
//you can make changes for measuring maximum/minimum acceleration in "processAccelData"
//demonstration of finally finidhed circuit is here: https://youtu.be/YG_fWjylvV4
//you may publish this code anywhere you want, BUT give credits to me
//_______________________________________________________________________________________________
#include<Wire.h>
long accelX, accelY, accelZ;    //variables for measuring acceleration
float gForceX, gForceY, gForceZ, maxX = 0.0, maxY = 0.0, maxZ = 0.0;  //variables for storing g-forces 
long gyroX, gyroY, gyroZ;   //variables for measuring angle rotations
float rotX, rotY, rotZ;
void setup(){
  Serial.begin(9600);
  Wire.begin();
  setupMPU();
}
//function for setting up the module's communication
void setupMPU(){
  Wire.beginTransmission(0b1101000);
  Wire.write(0x6B);
  Wire.write(0b00000000);
  Wire.endTransmission();
  Wire.beginTransmission(0b1101000);
  Wire.write(0x1B);
  Wire.write(0x00000000);
  Wire.endTransmission();
  Wire.beginTransmission(0x1C);
  Wire.write(0b00000000);
  Wire.endTransmission();
}
//function for reading acceleration data
void recordAccelRegisters(){
  Wire.beginTransmission(0b1101000);
  Wire.write(0x3B);
  Wire.endTransmission();
  Wire.requestFrom(0b1101000, 6);
  while(Wire.available()<6);
  accelX = Wire.read()<<8|Wire.read();
  accelY = Wire.read()<<8|Wire.read();
  accelZ = Wire.read()<<8|Wire.read();
  processAccelData();
}
//function for processing acceleration data;
void processAccelData(){
  gForceX = accelX/16384.0;
  gForceY = accelY/16384.0;
  gForceZ = accelZ/16384.0;
  if(gForceX>maxX){       //registering maximum x-acceleration
    maxX = gForceX;
  }
  if(gForceY>maxY){       //registering maximum y-acceleration
    maxY = gForceY;
  }
}
void loop(){
  recordAccelRegisters();
  printData();
  delay(100);
}
//function for printing the acceleration data
void printData(){
  Serial.print("accel X =");
  Serial.print(gForceX);
  Serial.print("g ");
  Serial.print(" accel Y =");
  Serial.print(gForceY);
  Serial.print("g ");
  Serial.print(" accel Z =");
  Serial.print(gForceZ);
  Serial.print("g ");
  Serial.print("acclnX max= ");
  Serial.print(maxX);
  Serial.print("g ");
  Serial.print("acclnY max= ");
  Serial.print(maxY);
  Serial.println("g ");
}

Once this code is entered in IDE, upload it and open serial monitor for obtaining g-values. You'd see the values appearing on the monitor giving
     x-acceleration, y-acceleration, z-acceleration, maximum acceleration in            x and maximum acceleration in y. Now you should spend like a minute or two and tilt/ move the sensor in different directions and see what sort of values are you obtaining- positive, negative or no acceleration and get a sense of its orientation too that is which way are its x, y, z axes.
    If you understand which axis is pointing where, then the next part of this post would be a cakewalk for you. So please try to play with the sensor for a few minutes and understand orientation of its axes.
  • A few pointers regarding the code and orientation of accelerometer
  1. Using the provided code, you would be able to measure the maximum positive acceleration in X & Y direction(the ones in which the car moves)
  2. Acceleration is a directional thing. Suppose you start moving forward and you obtain positive acceleration in a particular direction, then you'd obtain negative acceleration if you move in opposite direction.
  3. This directionality should be kept in mind while fixing accelerometer in your car. If you intend to ,measure g-force while car launches or takes off, make sure the accelerometer is aligned in that manner only. Just move your mpu6050 in the direction in which you have to measure acceleration, if positive values are obtained, fix it in that orientation else turn the sensor exactly opposite and fix it.
  4. Similarly If you want to measure braking force, move the sensor in direction opposite to car, if positive values are obtained then fix it else reverse it by 180degrees and fix it.
  5. Another important point to keep in mind is that sensor should be aligned perfectly in line with the car's direction. Simply put, if your car is moving dead straight, then you should be obtaining 0(or really small value like 0.03g/0.04g) acceleration in the direction perpendicular to motion. If you're obtaining some significant acceleration in perpendicular direction also while car is moving just forward, then the sensor isn't aligned in line with car.
  6. While cornering, the axis which is perpendicular to car's forward direction is your lateral axis. Its g-values are the cornering g-forces.
Long story short, make sure sensor is aligned perfectly in line with the car. You should also be aware of the axes of sensor and their alignment wrt to car. If you get these correct, you're ready to measure g-force values accurately!

And that's pretty much it! Your system is ready to record and display maximum acceleration & current acceleration. Once again, just get the sensor aligned correctly, and you're ready to go! 

        If you have any questions or comments, feel free to leave them below. 

Comments

Popular posts from this blog

Biasing of PN Junction Diodes

Basics of Diodes (Structure & Operation)

Rectifiers (Half & bridge)