# How to get values in terms of G in STM32F3 Discovery board

I am working on STM32 F3 Discovery Board. I am accessing accelerometer of the board. While doing so I am getting values for X,Y and Z axes. But these values are some random numbers. I want the output in terms of G (9.8). The sensitivity of sensor is set to 2g. The function I wrote for getting these values is as follows: I am showing code for one axis only because it is same for all three axes.

USART_SendData(USART1, 'Y');
//  tab=Yval;
tab= (int8_t)Buffer[1];
if(tab<0)
{
USART_SendData(USART1, '-');
tab*=-1;
}
rem=tab/100;
tab=tab-rem*100;
s=rem+48;
USART_SendData(USART1,s);
Delay(5);
rem=tab/10;
tab=tab-rem*10;
s=rem+48;
USART_SendData(USART1,s);
Delay(5);
rem=tab;
//tab=tab-rem*100;
s=rem+48;
USART_SendData(USART1,s);
Delay(5);
//  send(tab);
USART_SendData(USART1, '\n');

Delay(5);


I have also read in the data sheet of acceleromter used on the board "LIS302DL" that these values are stored in registers Outx, Outy and Outz but I am not able to access these registers. Please help me to get the values in terms of G.

You can work out the scaling of your sensor by repeating your measurement with the board flipped over. The difference between the two measurements will be 2g. So, record your x1, y1 and z1 values. Flip the board over and record second x2, y2 and z2 values. Your zero points are then just the average of each pair of readings.

X0 = (x1+x2)/2. (Same for y0 and z0)

Scaling is the distance between the two measurements divided by 2g

Counts per g = Sqrt ((z1-z2)^2+(y1-y2)^2+(x1-x2)^2)/2

If only one measurement changes significantly (say x), while the other two have nearly the same value, then this simplifies to:

Counts per g = (X1-x2)/2

• This will certainly work, but from a "having a grip on your embedded system" point of view, I prefer to confirm that the device is behaving exactly as I expect it to. Jun 19, 2015 at 15:19
• I'd do BOTH these (Scott and Harrys approaches) - Id want to know how theoritical calibration works and then Id want to see that it is applied as expected in real world. +1 each! Jun 22, 2015 at 7:45

The STM32F3 Discovery has the LSM303DLHC on board. The data sheet is at http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00027543.pdf

According to Page 10 of that doc, the sensitivity is 1, 2, 4 or 12 milli-G per LSB, depending on whether the FS bits are set at 00, 01, 10, or 11 respectively (corresponding to a range of +/- 2, 4, 8, or 16 G).

Assuming the full resolution number is being read in by your code, then you would multiply by the appropriate sensitivity and divide by 1000 to get your reading in G. You'll have to figure out by yourself (or maybe someone can peek at your code and add it) how FS bits are being set in your special case.

the weird numbers you are getting aren't that weird, they are digital signal so if you got a 8 bits device than the output of that device is between 0 and 255.

If you want the value in physical meaning value, you need to find the resolution of your accelerometer than multiply that value by the digital value.

Of course, you can't access the register, they are on the device. You need talk to the device either in SPI or I2C. If the device is already on your development you should read the datasheet of your board to know if the accelerometer is connected in SPI or I2C.

Regards MathieuL.

• I am looking for that factor by that I can get the output in terms of G (9.8). Jun 19, 2015 at 12:16
• Hi, you will need to find the resolution, than just write 1 or 2 lines of code to calculate the physical value. Jun 19, 2015 at 12:18