How to set MPU9250 full scale accelerometer

I have changed several vars in the attached MPU9250.h and the sensitivity does not change.
MPU9250-master
I think Im stuck in 16G mode. It does not look like there is an explicit command like "setAccelFS(2G)" command available in this library. So Im posting the library to see if someone can tell me the easiest way to adjust the accelerometer scale for my MPU9250 (with this library).

Ive changed this line:
template <typename WireType, AFS AFSSEL = AFS::A2G, GFS GFSSEL = GFS::G2000DPS, MFS MFSSEL = MFS::M16BITS>

from AFS::16G to AFS::A2G ... and nothing changed

I also changed :

c = c | (uint8_t)AFSSEL << 3; // Set full scale range for the accelerometer

to AFSSEL << 0 for 2g

What am I doing wrong ? I would like to have 2g full scale . At the moment the only large jolts change the readings.

see attached MPU9250.h lib

MPU9250.h (42.9 KB)

What am I doing wrong ?

You didn't read and follow the directions in the "How to use this forum" post, so we don't know.

Mention which Arduino you have, post the full code, using code tags, post a wiring diagram and include a link to the actual library you are using (there are several). Post the evidence for the problem.

Hint: the Earth's gravity is your friend. When held still, the accelerometer reports one g acceleration along any axis that is vertical.

Apologies for flagrantly flaunting the forum rules. No disrespect was intended. However:

I do not have a specific wiring diagram in mind and I do not have a specific sketch pertaining to this question.

My negligence to clearly ask the (rule breaking) question could be why I deserved your response.

My intention was for someone (knowledgeable as your self) to look at the attached MPU9250.h file and point out the function I can use to to adjust the accelerometer scale (to 2g), or tell me that this function can not support changing the accelerometer scale.

Ive seen the depth of your knowledge and I know this would take very little effort for you ( and others here) to do this but I know that rules are rules as well so if you choose not answer, I totally understand.

Thanks !

Why change anything in the library? Just call the appropriate routine with the proper argument, and you should be done.

Is it too much trouble to post a link to wherever you found that .h file?

Thank you for your response.

There is not a link , I realize I got the library from the Arduino IDE Library manager it says :
MPU9250 by hideakitai version 0.1.1

The readme file says :

MPU9250

Arduino library for MPU9250 Nine-Axis (Gyro + Accelerometer + Compass) MEMS MotionTrackingâ„¢ Device

This library is based on the great work by kriswiner, and re-writen for the simple usage.

And there is a simple example script:

#include "MPU9250.h"

MPU9250 mpu;

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

    Wire.begin();

    delay(2000);
    mpu.setup();
}

void loop()
{
    static uint32_t prev_ms = millis();
    if ((millis() - prev_ms) > 16)
    {
        mpu.update();
        mpu.print();

        Serial.print("roll  (x-forward (north)) : ");
        Serial.println(mpu.getRoll());
        Serial.print("pitch (y-right (east))    : ");
        Serial.println(mpu.getPitch());
        Serial.print("yaw   (z-down (down))     : ");
        Serial.println(mpu.getYaw());

        prev_ms = millis();
    }
}

And then this is at the end of the readme but I tried this without any results.

What actually happens when you upload the example sketch? "without any results" is meaningless.

Is the serial Baud rate correct?

For the MPU9250 there are numerous libraries written. I have done extensive use and testing with the Boulder Flight Systems Library Bolder Flight Systems MPU9250 - Arduino Libraries. I went on to write my own grown MPU9250 to take advantages of the ESP32 SPI API features using the Boulder Flight Systems MPU9050 library as a template. Note that the Boulder Flight Systems Library does a X/Y axis translation to coincide with the libraries intended use.

When it comes to the MPU9250 if the library does not work for you move to the next.

You may also want the information provided by this site: onehorse | Mbed.

Oh, the Boulder Flight Systems library can set the accelerometer scale.

Oh, if you do notice that there is one coding bug in the Boulder Flight Systems library, it is time to write your own.

My observations of using the MPU9250 is that they run for about 4 months continuous then fail. You will find a reference to eventual failures of the MPU9250 due to improper soldering techniques and the placing of the on-board components under continuous physical stresses. The common failure being that the gyros sticking to the inside of the casing.

This line indicates that the default accelerometer scale is 2g, specified when you instantiate the library.

template <typename WireType, AFS AFSSEL = AFS::A2G, GFS GFSSEL = GFS::G2000DPS, MFS MFSSEL = MFS::M16BITS>

This comment makes no sense to me:

Ive changed this line:
template <typename WireType, AFS AFSSEL = AFS::A2G, GFS GFSSEL = GFS::G2000DPS, MFS MFSSEL = MFS::M16BITS>

from AFS::16G to AFS::A2G ... and nothing changed

Yes Idaho walker you are correct , this is my 5th library... I got enamored with this one because of the Calibraation and eeprom features. My 4th was of course the Boulder Systems Library that has exactly what I need in the mpu.setAccelRange(0) function. But Im being a bit dense in trying to make this one work (at my demise)

Im just curious as it seems like it has the options of 2G,4G,8G and 16G. I just cant figure out how to set them . The lib has a selftest as well but it says "bad context" when I use "mpu.SelfTest(); which should spew out at least the settings it thinks its running at.

Jrem.. Right now I made a sketch to satisfy the form req.:

Can you be more specific of what exactly do you mean by "This comment makes no sense to me" are you saying my modification should set the device to 2G full scale or something else?

The output of the sketch below is :


MPU9250 WHO AM I = 71
MPU9250 is online...
AK8963 WHO AM I = 48
Calibration values:
X-Axis sensitivity adjustment value 1.20
Y-Axis sensitivity adjustment value 1.20
Z-Axis sensitivity adjustment value 1.16
X-Axis sensitivity offset value 0.00
Y-Axis sensitivity offset value 0.00
Z-Axis sensitivity offset value 0.00
calstartp
0
calibrated? : NO
load default values
MPU9250 bias
x y z
-66 1 18 mg
0.2 0.3 0.3 o/s
< calibration parameters >
accel bias [g]:
-66.65, 1.34, 18.01
gyro bias [deg/s]:
0.20, 0.34, 0.33
mag bias [mG]:
98.99, 93.79, -9.34
mag scale []:
0.72, 0.75, 2.47
caldone
Setup Complete
266 -0.45 0.02 8.12
266 -0.45 0.02 8.11
266 -0.45 0.00 8.10
266 -0.48 0.01 8.11
266 -0.45 0.01 8.11
265 -0.44 0.03 8.13
265 -0.45 0.02 8.12
....

Here is the sketch:

#include "eeprom_utils.h"
#include "MPU9250.h"


MPU9250 mpu;
int heading;
void setup() 
{
Serial.begin(57600);
  Serial.setTimeout(50);
  Wire.begin();
  delay(200);
  mpu.setup();
  delay(100);
  //mpu.SelfTest();
  Serial.println("calstartp"); 
  // calibrate anytime you want to
  loadCalibration();
  mpu.calibrateAccelGyro();
      //mpu.calibrateMag();
    delay(1000);
  mpu.printCalibration();
  //mpu.saveCalibration();
  delay(100);
  Serial.println("caldone"); 
  mpu.setMagneticDeclination(13.3);
  // setting the accelerometer full scale range to +/-8G 
// mpu.setAccelRange(0);

 Serial.println("Setup Complete ");


}
  
void loop() {
     String mpu_str=get_heading();

     Serial.print(mpu_str); // robot docked, avoid OFF

  Serial.println(" ");

  delay(100);
} //loop


String get_heading()
{
        heading = get_angle();
float  oldValue = heading;
        
        String mpu_string = String(heading)+' '+String (mpu.getAcc(0))+' '+String (mpu.getAcc(1))+' '+String(mpu.getAcc(2)) ;  
        //Serial.println(mpu_string);
        return mpu_string;
}

int get_angle()
{
        mpu.update();
      heading=int(mpu.getYaw());
        if (heading < 0 )
           heading = heading+360;
           
        heading = (heading+80);
        heading = (heading%360);   
        
        return heading;
}

I think I have the desired results:

  1. I set the template setting to 16G
    template <typename WireType, AFS AFSSEL = AFS::A16G, GFS GFSSEL = GFS::G2000DPS, MFS MFSSEL = MFS::M16BITS>

  2. I set the Acell setting to 0
    c = c | (uint8_t)AFSSEL << 0; // Set full scale range for the accelerometer

My goal was to get more sensitivity and the above settings get what I want. I guess Im not sure what exactly fix things but I was under the assumption that 2g was more sensitive than 16G maybe not .

Im not sure at this point ... any one with some clarity?

"2G" is the most sensitive setting, and is used for navigation.
"16G" is for measuring accelerations during crashes.