TinyCircuits Forum

TinyCircuits Products => Wirelings => Topic started by: Leeyt4541 on March 13, 2023, 05:18:50 PM

Title: Accelerometer: transfer the raw data to the real acceleration in m/^2
Post by: Leeyt4541 on March 13, 2023, 05:18:50 PM
Hey Sir/Madam,

I have a simple question: what is the unit of acceleration in three axes and how can I transfer this value to the actual acceleration in m/s^2? I tried to figure it out but failed. I think transferring the raw data to the actual acceleration needs a scaling factor (like accelerometer ADXL345) but I don't know this specific factor for BMA250. Hope someone can help me figure it out.

Thanks!
Title: Re: Accelerometer: transfer the raw data to the real acceleration in m/^2
Post by: Jason on March 14, 2023, 11:12:03 AM
You should be able to hold the sensor flat and still, record the number on the axis with the largest magnitude, and then divide each axis with that number after getting the readings. Now you'll have all the readings normalized to gravity, so just multiply the readings by 9.8 after dividing them.

The value I recorded was -261.0 on the z-axis, so I'll divide each axis by its magnitude and then multiply by 9.8
Code: [Select]
#include <Wire.h>
#include "BMA250.h"


BMA250 accel_sensor;
int x, y, z;


#if defined(ARDUINO_ARCH_SAMD)
 #define SerialMonitorInterface SerialUSB
#else
 #define SerialMonitorInterface Serial
#endif


void setup() {
  SerialMonitorInterface.begin(115200);
  Wire.begin();

  SerialMonitorInterface.print("Initializing BMA...");
  // Set up the BMA250 acccelerometer sensor
  accel_sensor.begin(BMA250_range_2g, BMA250_update_time_64ms);
}


void loop() {
  accel_sensor.read();//This function gets new data from the acccelerometer

  // Get the acceleration values from the sensor and store them into global variables
  // (Makes reading the rest of the program easier)
  x = accel_sensor.X;
  y = accel_sensor.Y;
  z = accel_sensor.Z;

  if (x == -1 && y == -1 && z == -1) {
    SerialMonitorInterface.print("ERROR! NO BMA250 DETECTED!");
  }
 
  else {
    showSerial();
  }

  delay(250);
}


void showSerial() {
  SerialMonitorInterface.print("X = ");
  SerialMonitorInterface.print((x / 261.0f) * 9.8f);
 
  SerialMonitorInterface.print("  Y = ");
  SerialMonitorInterface.print((y / 261.0f) * 9.8f);
 
  SerialMonitorInterface.print("  Z = ");
  SerialMonitorInterface.println((z / 261.0f) * 9.8f);
}

You could simplify the conversion math by combining the constant values: 9.8/261.0 =  0.0375. Now I can convert each axis this way:
Code: [Select]
void showSerial() {
  SerialMonitorInterface.print("X = ");
  SerialMonitorInterface.print(x * 0.0375f);
 
  SerialMonitorInterface.print("  Y = ");
  SerialMonitorInterface.print(y * 0.0375f);
 
  SerialMonitorInterface.print("  Z = ");
  SerialMonitorInterface.println(z * 0.0375f);
}