I'm looking at the little code snippet from this (many other projects also use similar code):

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

 ax = map(ax, -17000, 17000, -1500, 1500);

If my reading is correct, it's assuming the value of ax to be between -17000 and 17000. But isn't the value supposed to be 16-bit? If so, shouldn't the range be wider than -17000, 17000? Is the value capped?

Any thought appreciated.


The ADC for the accelerometer is 16 bit, yes. Also the data being sent out is in 16 bit format.

But what you are confusing is resolution and range.

The accelerometer can be programmed to have a sensitivity of 2g, 4g, 8g or 16g. With it set to the least sensitive, that is 2g, the full range of -32768 to +32767 would equate to ±2g.

That's fine when you're measuring G-forces of up to 2g, but when all you are sensing is tilt then there is only 1g - the 1g the Earth exerts on it.

That 1g is half the 2g range, which is -16384 to +16363. So round that up and give a little extra headroom to your calculations, and you get -17000 to +17000. It gives enough range in your calculations to give the full ±1g range "plus a little bit".

  • Very good point! So, suppose I'd like to detect when a rod rotating about the x axis is being accelerated too much (eg 1m/s^2), should I be using the accelerometer or gyro values? I'm guessing it might be gyro since accelerometer measures linear acceleration and I might want instantaneous angular accelerations?
    – John M.
    Jun 4 '16 at 9:25
  • Actually, isn't 2g the most rather than the least sensitive? At 2g, it could pick up more refined changes in acceleration than at 16g.
    – John M.
    Jun 4 '16 at 12:29
  • That's what I meant, yeah. Most sensitive, or least attenuated.
    – Majenko
    Jun 4 '16 at 12:31

No, the map() function does not cap it's input or output, you would need constrain() for that. With map() alone, there is no assumption about the range of the values, other than the calculation not overflowing 32-bit integers. Thus,

ax = map(ax, -17000, 17000, -1500, 1500);

simply multiplies x by the ratio 1500/17000. It is mathematically equivalent to

ax = map(ax, 0, 34, 0, 3);

or to

ax = ax * 3L / 34;

C.f. the documentation of map().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.