How Accelerometer Works
How Accelerometer Works
Ever wondered how your smartphone knows up from down! It’s one of the coolest
features of today’s smartphones. They all got a tiny device called Accelerometer built
into the circuitry which can sense when you tilt it from side to side. That’s how your
smartphone automatically figures out when to switch the screen layout from portrait to
landscape.
Accelerometers are widely used in cost sensitive, low power, motion and tilt sensing
applications like Mobile devices, Gaming systems, Disk drive protection, Image
stabilization and Sports & health devices.
Let’s take a closer look at what they are, what they do, and how they work.
What is Acceleration?
If you have a certain force (say, the power in your leg as you kick it outward) and you
apply it to a mass (a soccer ball), you’ll make the mass accelerate (the ball will shoot
off into the air).
Force = Mass x Acceleration
Suppose, the cube is in outer-space where everything is in weightless state, the ball
will simply float in the middle of the cube.
If we suddenly move the box to the left with acceleration 1g(A single G-force 1g is
equivalent to gravitational acceleration 9.8 m/s2), no doubt the ball will hit the wall X. If
we measure the force that the ball applies to the wall X, we can get an output value of
1g on the X axis.
Let’s see what happens if we put that cube on Earth. The ball will simply fall on the
wall Z and will apply a force of 1g, as shown in the picture below:
In this case the box isn’t moving but we still get a reading of 1g on the Z axis. This is
because the gravitational force is pulling the ball down with force 1g.
Due to deflection the capacitance between fixed plates and plates attached to the
suspended structure is changed. This change in capacitance is proportional to the
acceleration on that axis.
The sensor processes this change in capacitance and converts it into an analog
output voltage.
This breadboard friendly board breaks out every pin of the ADXL335 to a 6-pin, 0.1″
pitch header. This includes 3 analog outputs for X, Y and Z axis measurements, 2
supply pins and a self-test pin which allows you to check the functioning of the sensor
in the final application.
VCC pin provides power for the accelerometer which can be connected to 5V on the
Arduino.
X-Out pin outputs analog voltage proportional to acceleration exerted on X axis.
Y-Out pin outputs analog voltage proportional to acceleration exerted on Y axis.
Z-Out pin outputs analog voltage proportional to acceleration exerted on Z axis.
GND pin is connected to GND on Arduino
ST(Self-Test) pin controls the self-test feature. This feature is discussed in detail at the
end.
For accurate results, we need to change the analog reference(AREF) voltage of the
Arduino. This can be done by connecting the 3.3V pin on Arduino to the AREF pin.
When you’re done you should have something that looks similar to the illustration
shown below.
Wiring ADXL335
Accelerometer Module to Arduino UNO
So now that we’ve hooked up our accelerometer, it’s time to write some code and test
it out.
void setup()
{
analogReference(EXTERNAL);
Serial.begin(9600);
}
void loop()
{
//Read raw values
int xRaw = ReadAxis(xInput);
int yRaw = ReadAxis(yInput);
int zRaw = ReadAxis(zInput);
// re-scale to fractional Gs
float xAccel = xScaled / 1000.0;
float yAccel = yScaled / 1000.0;
float zAccel = zScaled / 1000.0;
Serial.print("X, Y, Z :: ");
Serial.print(xRaw);
Serial.print(", ");
Serial.print(yRaw);
Serial.print(", ");
Serial.print(zRaw);
Serial.print(" :: ");
Serial.print(xAccel,0);
Serial.print("G, ");
Serial.print(yAccel,0);
Serial.print("G, ");
Serial.print(zAccel,0);
Serial.println("G");
delay(200);
}
Next, we define the minimum and maximum values the Arduino is going provide. As
the Arduino board contains 10-bit analog to digital converter, it will map sensor’s
output voltages between 0 and 3.3 volts into integer values between 0 and 1023.
That’s why RawMin is set to 0 and RawMax is set to 1023.
You can read more about the function on Arduino’s official website. Except this, we
initialize serial communications with the PC.
Warning:
If you fail call analogReference(EXTERNAL), you will short together the active reference
voltage (internally generated) and the AREF pin, possibly damaging your Arduino.
analogReference(EXTERNAL);
Serial.begin(9600);
In setup function, we read analog outputs from the sensor every 200ms. Instead of
calling analogRead() function, we are calling ReadAxis() custom function. This function
merely takes 10 sample of ADC conversions and returns the average.
The IDE’s built-in map() function does the actual mapping. So, when we call map(xRaw,
RawMin, RawMax, -3000, 3000) , value of RawMin would get mapped to -3000, a value
of RawMax to 3000 and values in-between to values in-between.
The values -3000 and 3000 are not arbitrary. They actually represents the
gravitational acceleration (in milli-g which is 1/1000 of a g) measured by the sensor
i.e. ±3g (-3000 milli-g to 3000 milli-g).
For example,
When the sensor outputs 0 volts on x-axis i.e. xRaw=0, the map() function will
return -3000 representing -3g.
When the sensor outputs 3.3 volts on x-axis i.e. xRaw=1023, the map() function
will return 3000 representing +3g.
When the sensor outputs 1.65 volts on x-axis i.e. xRaw=511, the map() function
will return 0 representing 0g.
The term Ratiometric will make more sense now as the output voltage increases
linearly with acceleration over the range.
Finally, the sensor’s output is scaled down to fractional Gs by dividing it by 1000 and
displayed on the serial monitor.
// re-scale to fractional Gs
float xAccel = xScaled / 1000.0;
float yAccel = yScaled / 1000.0;
float zAccel = zScaled / 1000.0;
Serial.print("X, Y, Z :: ");
Serial.print(xRaw);
Serial.print(", ");
Serial.print(yRaw);
Serial.print(", ");
Serial.print(zRaw);
Serial.print(" :: ");
Serial.print(xAccel,0);
Serial.print("G, ");
Serial.print(yAccel,0);
Serial.print("G, ");
Serial.print(zAccel,0);
Serial.println("G");
Warning:
Exposing the ST pin to voltages greater than 3.6V may damage the accelerometer
permanently.