# Arduino accelerometer programming

Hello guys. I recently bought an ADXL 335 accelerometer and an Arduino nano V3 board and I want to make some measurements.

My configuration pin is:
Vcc pin from accelerometer to 3.3V Arduino Board
GND to GND
X,Y,X pins from sensor to A1,A2,A3 pins from nano board
3.3V from Arduino board to AREF pin nano board

My program is:

const int xPin= A1;
const int yPin= A2;
const int zPin= A3;

void setup()
{
Serial.begin(9600);
}

void loop()
{
analogReference(EXTERNAL); //3.3V to AREF

Serial.print("\t");

Serial.print("\t");

Serial.print("\t");
Serial.println();

delay(100);
}

And the results from Serial monitor are:
375 374 475

But, this is not what I expected. I want to see +- datas in G forces on each axis.
So what I need to do for achieveing my goal?
Thank you!

Hi, welcome to the forum.

The analogRead() returns a value from the ADC (the part in the chip that converts the analog input to a digital value) and can be 0 to 1023.

So all you can see is a value of 0 to 1023.

This is a tutorial : Overview | Adafruit Analog Accelerometer Breakouts | Adafruit Learning System
That tutorial uses an accelerometer module from Adafruit that has a onboard voltage regulator for 3.3V. That is why that module can be powered with 5V. You might have an accelerometer module without voltage regulator, and you may only use 3.3V to power it.
There is an example sketch that uses the 'map' function to calculate the g-forces.
Read that tutorial from begin to end, you will learn a lot.

CIPA:
So what I need to do for achieveing my goal?

MATH

Ok, thank you guys for answers. After I'll have all the parts i'll try to make the configuration and see what I can do. But I wonder what is the value of that resistor? I think is 2.2Kohm. Am I right? Sorry, I'm new in this world so... be patient with me please. But I want to learn.

Delta G, what math I can do? I now that i can decompose one vector on three axes. And that vector is the square from x^2+y^2+z^2. And if i know the value of the vector, and my vector is positioned at an angle alpha from one axis,let say from X axis, I can find the value of that angle : [X^2/(square from x^2+y^2+z^2)].

Thanks!

The resistor is for the button. The Adafruit tutorial uses the button to calibrate to zero. Any value of 220 ohm to 100k will do for a button, but between 1k and 10k is better :))

The formula for the vector is correct, for the axis angle I don't know.

Before you do that math, you'll have to do some math to turn the voltages you are getting into values that represent acceleration on some axis. There should be some indication of how that is done in the datasheet for the accelerometer.

Ok peter, so I'll make the configuration but with a resistor value between 1 and 10k.

Delta G. I don't know what voltage I have on each axis at a specified time. All I can see in Serial Monitor is values between 0 and 1023 becouse of ADC from chip. In my case with accelerometer sensor flat on the table X=375 Y=374 Z=475. So, I don't know what voltage I have on X axis when Serial monitor shows me 375 or 293 or 430 or I don t know, any values. I can find this? In ADXL 335 datasheet says this:

,,The ADXL335 output is ratiometric, therefore, the output
sensitivity (or scale factor) varies proportionally to the
supply voltage. At VS = 3.6 V, the output sensitivity is typically
360 mV/g. At VS = 2 V, the output sensitivity is typically
195 mV/g.
The zero g bias output is also ratiometric, thus the zero g
output is nominally equal to VS/2 at all supply voltages. "

I hope will not be so hard to transform some numbers in G values :)))
I forgot to mentioned. I can power the sensor with a voltage between 3.3 and 5V and I usualy use the 3.3V.
Thanks.

CIPA:
Delta G. I don't know what voltage I have on each axis at a specified time. All I can see in Serial Monitor is values between 0 and 1023 becouse of ADC from chip.

And those numbers correspond to voltages right? The ADC on the Arduino reads an analog voltage and gives you a number from 0 to 1023 that represents that voltage on a scale where 0 is 0 and 1023 is the AREF voltage.

Can you figure the math to turn that number into a voltage. Here's a hint: It's a linear relationship.

So, my datas on each axes are:

X: 375 that means 1.2V
Y: 374 that means 1.2V
Z: 475 that means 1.5V

This are the numbers for accelerometer positioned on the table( flat).
For conversion, I have used this formula:
1023/3.3 = 375/x (375 number is for X axes)
So, what I need to do next?
Thanks!

CIPA:
,,The ADXL335 output is ratiometric, therefore, the output
sensitivity (or scale factor) varies proportionally to the
supply voltage. At VS = 3.6 V, the output sensitivity is typically
360 mV/g. At VS = 2 V, the output sensitivity is typically
195 mV/g.
The zero g bias output is also ratiometric, thus the zero g
output is nominally equal to VS/2 at all supply voltages. "

Let's look at what the datasheet says.

That last line is interesting.

The zero g bias output is also ratiometric, thus the zero g
output is nominally equal to VS/2 at all supply voltages. "

So at 0g (ie laying on the table) the output should be about half the supply voltage (VS is supply voltage). So what's half of 3.3? Is that what you got back? Or real close to it?

Now look at the other part. It says:

,The ADXL335 output is ratiometric, therefore, the output
sensitivity (or scale factor) varies proportionally to the
supply voltage. At VS = 3.6 V, the output sensitivity is typically
360 mV/g. At VS = 2 V, the output sensitivity is typically
195 mV/g.

So at 3.6V supply, you would have 360mV/g. resting would be at 1.8 (VS / 2) so pulling 1g would put you about 1.8 + 0.36 = 2.16V.

But you're running 3.3V and not 3.6V. So look at that other word, ratiometric. So just interpolate with the two points they've given you and figure out what it would be at 3.3V.

Judging by what I just wrote, your values make sense. You got about 1.2 for the 2 axis at 0g and 1.5 for the Z axis at 1g. Surely when you extrapolate that out it is going to be something close to 330mV/g. So that looks just about right.

I hope that this forum could help me as well. I have upload a code for the acelerometer using a arduino lily pad. When I move the acelerometer the x, y, z numbers do not change. What should I do to get this to work. How can I organize the data in a data table to be able to see?

I put in the code for accelerometer and it was successfully uploaded, but I don't know how to get the numbers to change when moving the x, y, z accelerometer. Does any one know how to get the numbers to show in a table as well for data collection?

sherrip077:
I put in the code for accelerometer and it was successfully uploaded, but I don't know how to get the numbers to change when moving the x, y, z accelerometer. Does any one know how to get the numbers to show in a table as well for data collection?

You should start your own thread for your own problem. When you do, be sure you include the code you're having trouble with. It's really really hard for anyone to help troubleshoot code that they can't see.

Mapping the values to actual acceleration should be dead easy: no need to worry abut all that "voltage" business. The zero point of the x and y values occur when the z value is at it's maximum or minimum. With luck, the zero point for each axis is at the centre of the max/min. The scale is that the maximum and minimum corresponds to an acceleration of ±9.8 m/s/s (unless you are writing from the moon).

For your first project, why not write a "calibrate" sketch? When you hold a button down, samples are taken at 100ms intervals. From those samples, the sketch makes its best guess at what the midpoint and scale is for each axis.

Think of it as a 3-d scatter graph. When you hold the unit still at some orientation, the acellerometer reading will be somewhere on the surface of a sphere (well, and ellipsoid, but hopefully it's pretty round). The center of that sphere is the zero point, the radius of that sphere corresponds to an acceleration of 9.8m/s/s. So given N readings, what's the best fit for the center and scale?

To test out your accelerometer, to need to accelerate it at a known rate. One way would be to attach it to a wheel that's rotating at a known rate. Another would be to put it on a nice, heavy pendulum of a known length - but the math for that is not simple (pendulums do not swing at a constant frequency - the frequency is nearly constant when the arc of the swing is very small).

Ok guys, I'm back. Sorry for that long period of absence. I had made the circuit like here Calibration and Programming | Adafruit Analog Accelerometer Breakouts | Adafruit Learning System from adafruit and I uploaded that program in my arduino nano. But when I start serial monitor something weird happends. It shows me continuously ,,Calibrate" and if I touch the resistor from breadboard circuit it appear some datas: (accelerometer have a flat position on table).

Raw Ranges: X: 321-512, Y: 351-512, Z: 464-512
372, 374, 475 :: -0.47G, -0.71G, -0.54G

But ONLY if I touch the resistor, not any wire or something else. And to be more strange, the datas appear only if I touch the resistor directly with my hand (I tried with a pen or something else and did not work)and after I take my hand from resistor shows me continuously ,,Calibrate".

Another problem is when I press the button it does not happend anything. In Serial Monitor shows me continuosly ,,Calibrate".

Two questions:

1. Why do you think dates appear just when I touch the resistor with my hand (I took a look for imperfect contacts or something else but everything is fine)?
2. Why on Z axe I have -0.54G? The accelerometer is flat on the table so it should be something like -1G, right?

Thanks!

I had rebuild the circuit and it was my bad, one wire was not in the right place. Now, after i calibrate my sensor I have this datas:

Raw Ranges: X: 300-512, Y: 301-512, Z: 326-512
372, 375, 326 :: -0.32G, -0.30G, -1.00G
Raw Ranges: X: 300-512, Y: 301-512, Z: 326-512
373, 375, 326 :: -0.31G, -0.30G, -1.00G
Raw Ranges: X: 300-512, Y: 301-512, Z: 326-512
373, 375, 326 :: -0.31G, -0.30G, -1.00G
Raw Ranges: X: 300-512, Y: 301-512, Z: 326-512
373, 375, 326 :: -0.31G, -0.30G, -1.00G

How I can remove from axes the - sign and how to make X and Y axes to be ~ 0 G. I don't understand why they are ~ 0.30 G.

Thank's!