## Monday, January 7, 2013

### Accelerometer calibration helper, for AVR Atmega, Arduino and other micro

An accelerometer is a device that measures proper acceleration.
Almost every accelerometer chip has to be calibrated, because every device brings a manufacturing error.

In case of accelerometer the correction parameters we need are gain and offset.

Once you obtain those values you can get calibrated data by applying this formula:
x_calibrated = (x_raw-offsetx) / gainx
y_calibrated = (y_raw-offsety) / gainy
z_calibrated = (z_raw-offsetz) / gainz

This Python script implements a simple Accelerometer calibration routine to estimate offset and gain calibration values.

To calibrate the accelerometer user have to place it in 6 different position (see sheet provided). The collected values are then computed to correct gain and offset.
offsetx = (x_raw_at_+1g + x_raw_at_-1g) / 2
offsety = (y_raw_at_+1g + y_raw_at_-1g) / 2
offsetz = (z_raw_at_+1g + z_raw_at_-1g) / 2
gainx = (x_raw_at_+1g - x_raw_at_-1g) / 2
gainy = (y_raw_at_+1g - y_raw_at_-1g) / 2
gainz = (z_raw_at_+1g - z_raw_at_-1g) / 2

If you run your sensor in a big range of temperature you should also consider a calibration dependent to temperature too.
To calibrate your sensor you should collect values in a stable position, collecting temperature also.
You should find that the temperature against raw values is more or less linear, so you can estimate a temp_coeficient values, and your formula could became like this:
z_calibrated = (v_raw - offsetv - temp * temp_coeficient) / gainv;

To obtain values, run this script and follow the instructions, a calibration sheet is provived to help you directing the sensor.

On the microcontroller side you have to setup a function that print out to UART raw values read from your chip.
Given 2 bytes (int16_t) variables for every axis, output the LSB and then MSB byte ((uint8_t)(int16_t>>0) + (uint8_t)(int16_t>>8)), follow by a '\n' char.

Snippets are provided for AVR Atmega and Arduino, but it can be setup for other micro too.

Code

Notes