MPU6050 Calibration code for detecting and measuring peak ground acceleration

Hi Guys,

This code below is the MPU6050 Calibration code for detecting and measuring peak ground acceleration. I hope that this code will help you. Thanks.

======================================================

#include <Wire.h>
#include <SoftwareSerial.h>

// MPU6050 Slave Device Address
const uint8_t MPU6050SlaveAddress = 0x68;

// Select SDA and SCL pins for I2C communication
const uint8_t scl = D6;
const uint8_t sda = D7;

//ACCELERATION DATA CORRECTION
int AcXcal = -950;
int AcYcal = -300;
int AcZcal = 0;

// sensitivity scale factor respective to full scale setting provided in datasheet
const uint16_t AccelScaleFactor = 16384;
//const uint16_t AccelScaleFactor = 32768;
const uint16_t GyroScaleFactor = 131;

// MPU6050 few configuration register addresses
const uint8_t MPU6050_REGISTER_SMPLRT_DIV = 0x19;
const uint8_t MPU6050_REGISTER_USER_CTRL = 0x6A;
const uint8_t MPU6050_REGISTER_PWR_MGMT_1 = 0x6B;
const uint8_t MPU6050_REGISTER_PWR_MGMT_2 = 0x6C;
const uint8_t MPU6050_REGISTER_CONFIG = 0x1A;
const uint8_t MPU6050_REGISTER_GYRO_CONFIG = 0x1B;
const uint8_t MPU6050_REGISTER_ACCEL_CONFIG = 0x1C;
const uint8_t MPU6050_REGISTER_FIFO_EN = 0x23;
const uint8_t MPU6050_REGISTER_INT_ENABLE = 0x38;
const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H = 0x3B;
const uint8_t MPU6050_REGISTER_SIGNAL_PATH_RESET = 0x68;

int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ;
double Gx, Gy, Gz, Ax, Ay, Az, Wxx, Wyy, Wzz, IntVal;
double x_mval, y_mval, z_mval, x_tval, y_tval, z_tval, x_xval, y_yval, z_zval, x_grav, y_grav, z_grav;
String Int;

void setup() {
Serial.begin(9600);

//INITIALIZE THE LCD TO 20x4 CHARACTER FORMAT
Wire.begin(sda, scl);
MPU6050_Init();
}

void loop() {
// put your main code here, to run repeatedly:

Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H);

///DIVIDE EACH WITH THEIR FULL SENSITIVITY SCALE FACTOR
Ax = ((double)AccelX + AcXcal)/AccelScaleFactor; //getting Gx value
Ay = ((double)AccelY + AcYcal)/AccelScaleFactor; //getting Gy value
Az = ((double)AccelZ + AcXcal)/AccelScaleFactor; //getting Gz value

x_mval = x_tval;
x_tval = x_xval;
x_xval = Ax;

y_mval = y_tval;
y_tval = y_yval;
y_yval = Ay;

z_mval = z_tval;
z_tval = z_zval;
z_zval = Az;

//NON-LINEAR LEAST-SQUARE FORMULA
x_grav = (x_xval - x_mval)/(1/x_tval);
y_grav = (y_yval - y_mval)/(1/y_tval);
z_grav = (z_zval - z_mval)/(1/z_tval);

Wxx = Gx;
Gx = x_grav;
Wyy = Gy;
Gy = y_grav;
Wzz = Gz;
Gz = z_grav;

//TOTAL RESULTANT VECTOR ACCELERATION
IntVal = sqrt(sq(x_grav - Wxx) + sq(x_grav - Wxx) + sq(x_grav - Wxx));

Serial.print(Ax, 5);
Serial.print(" | “);
Serial.print(Ay, 5);
Serial.print(” | “);
Serial.print(Az, 5);
Serial.print(” | “);
Serial.print(x_grav, 5);
Serial.print(” | “);
Serial.print(y_grav, 5);
Serial.print(” | “);
Serial.print(z_grav, 5);
Serial.print(” | ");
Serial.println(IntVal, 7);

delay(100);
}

void AutoCalibrate(int xRaw, int yRaw, int zRaw){
//if (xRaw
}
void I2C_Write(uint8_t deviceAddress, uint8_t regAddress, uint8_t data){
Wire.beginTransmission(deviceAddress);
Wire.write(regAddress);
Wire.write(data);
Wire.endTransmission();
}

// read all 14 register
void Read_RawValue(uint8_t deviceAddress, uint8_t regAddress){
Wire.beginTransmission(deviceAddress);
Wire.write(regAddress);
Wire.endTransmission();
Wire.requestFrom(deviceAddress, (uint8_t)14);
AccelX = (((int16_t)Wire.read()<<8) | Wire.read());
AccelY = (((int16_t)Wire.read()<<8) | Wire.read());
AccelZ = (((int16_t)Wire.read()<<8) | Wire.read());
Temperature = (((int16_t)Wire.read()<<8) | Wire.read());
GyroX = (((int16_t)Wire.read()<<8) | Wire.read());
GyroY = (((int16_t)Wire.read()<<8) | Wire.read());
GyroZ = (((int16_t)Wire.read()<<8) | Wire.read());
}

//configure MPU6050
void MPU6050_Init(){
delay(150);
I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SMPLRT_DIV, 0x07);
I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_1, 0x01);
I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_2, 0x00);
I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_CONFIG, 0x00);
I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_GYRO_CONFIG, 0x00);//set +/-250 degree/second full scale
I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_CONFIG, 0x00);// set +/- 2g full scale
I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_FIFO_EN, 0x00);
I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_INT_ENABLE, 0x01);
I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00);
I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_USER_CTRL, 0x00);
}

Please edit your post to add code tags, as described in How to use this forum.

Duplicate post on Sensors section deleted

@darkprogrammer37 - do not post duplicate questions in different forum sections