How To Connect An Accelerometer To Arduino

Table of contents:

How To Connect An Accelerometer To Arduino
How To Connect An Accelerometer To Arduino

Video: How To Connect An Accelerometer To Arduino

Video: How To Connect An Accelerometer To Arduino
Video: How To Track Orientation with Arduino | ADXL345 Accelerometer Tutorial 2024, December
Anonim

This time we will deal with connecting the ADXL335 analog triaxial accelerometer to the Arduino.

Analog Accelerometer ADXL335
Analog Accelerometer ADXL335

Necessary

  • - Arduino;
  • - accelerometer ADXL335;
  • - a personal computer with the Arduino IDE development environment.

Instructions

Step 1

Accelerometers are used to determine the acceleration vector. The ADXL335 accelerometer has three axes, and thanks to this, it can determine the acceleration vector in three-dimensional space. Due to the fact that the force of gravity is also a vector, the accelerometer can determine its own orientation in three-dimensional space relative to the center of the Earth.

The illustration shows pictures from the passport (https://www.analog.com/static/imported-files/data_sheets/ADXL335.pdf) for the ADXL335 accelerometer. Shown here are the coordinate axes of the accelerometer's sensitivity in relation to the geometric placement of the device body in space, as well as a table of voltage values from 3 accelerometer channels depending on its orientation in space. The data in the table are given for a sensor at rest.

Let's take a closer look at what the accelerometer shows us. Let the sensor lie horizontally, for example, on a table. Then the projection of the acceleration vector will be equal to 1g along the Z axis, or Zout = 1g. The other two axes will have zeros: Xout = 0 and Yout = 0. When the sensor is turned "on its back", it will be directed in the opposite direction relative to the gravity vector, ie. Zout = -1g. Similarly, measurements are taken on all three axes. It is clear that the accelerometer can be positioned as desired in space, so we will take readings other than zero from all three channels.

If the probe is shaken hard along the vertical Z-axis, the Zout value will be greater than "1g". The maximum measurable acceleration is "3g" in each of the axes in any direction (ie both with "plus" and "minus").

Accelerometer sensitivity axes and table of output values
Accelerometer sensitivity axes and table of output values

Step 2

I think we figured out the principle of operation of the accelerometer. Now let's look at the connection diagram.

The ADXL335 analog accelerometer chip is rather small and housed in a BGA package, and it is difficult to mount it on a board at home. Therefore, I will use a ready-made GY-61 module with an ADXL335 accelerometer. Such modules in Chinese online stores cost almost a penny.

To power the accelerometer, it is necessary to supply voltage +3, 3 V to the VCC pin of the module. The sensor measuring channels are connected to the analog pins of the Arduino, for example, "A0", "A1" and "A2". This is the whole circuit:)

Wiring diagram of ADXL335 accelerometer to Arduno
Wiring diagram of ADXL335 accelerometer to Arduno

Step 3

Let's load this sketch into Arduino memory. We will read readings from the analog inputs on three channels, convert them to voltage and output them to the serial port.

The Arduino has a 10-bit ADC, and the maximum voltage allowed on the pin is 5 volts. The measured voltages are encoded with bits that can only take 2 values - 0 or 1. This means that the entire measurement range will be divided by (1 + 1) to the 10th power, i.e. on 1024 equal segments.

In order to convert the readings to volts, you need to divide each value measured at the analog input by 1024 (segments), and then multiply by 5 (volts).

Let's see what really comes from the accelerometer using the Z-axis as an example (the last column). When the sensor is positioned horizontally and looks up, the numbers come (2.03 +/- 0.01). So this should correspond to the acceleration "+ 1g" along the Z axis and an angle of 0 degrees. Flip the sensor. The numbers come (1, 69 +/- 0, 01), which should correspond to "-1g" and an angle of 180 degrees.

Accelerometer reading sketch
Accelerometer reading sketch

Step 4

Let's take the values from the accelerometer at angles of 90 and 270 degrees and enter them into the table. The table shows the angles of rotation of the accelerometer (column "A") and their corresponding Zout values in volts (column "B").

For clarity, a plot of voltages at the Zout output versus the rotation angle is shown. The blue field is the range at rest (at 1g acceleration). The pink box on the graph is a margin so that we can measure acceleration up to + 3g and up to -3g.

At 90 degrees rotation, the Z-axis has zero acceleration. Those. a value of 1.67 volts is a conditional zero Zo for the Z axis. Then you can find the acceleration like this:

g = Zout - Zo / sensitivity_z, here Zout is the measured value in millivolts, Zo is the value at zero acceleration in millivolts, sensitivity_z is the sensitivity of the sensor along the Z axis. calibrate the accelerometer and calculate the sensitivity value specifically for your sensor using the formula:

sensitivity_z = [Z (0 degrees) - Z (90 degrees)] * 1000. In this case, the sensitivity of the accelerometer along the Z axis = (2, 03 - 1, 68) * 1000 = 350 mV. Similarly, the sensitivity will need to be calculated for the X and Y axes.

Column "C" of the table shows the acceleration calculated for five angles at a sensitivity of 350. As you can see, they practically coincide with those shown in Figure 1.

Correspondence of the angles of rotation of the accelerometer to the readings
Correspondence of the angles of rotation of the accelerometer to the readings

Step 5

Remembering the basic geometry course, we get the formula for calculating the angles of rotation of the accelerometer:

angle_X = arctg [sqrt (Gz ^ 2 + Gy ^ 2) / Gx].

Values are in radians. To convert them to degrees, divide by Pi and multiply by 180.

As a result, a complete sketch calculating the acceleration and rotation angles of the accelerometer along all axes is shown in the illustration. The comments provide explanations for the program code.

When outputting to the "Serial.print ()" port, the "\ t" character denotes a tab character so that the columns are even and the values are located one under the other. "+" means concatenation (concatenation) of strings. Moreover, the "String ()" operator explicitly tells the compiler that the numerical value must be converted to a string. The round () operator rounds the corner to the nearest 1 degree.

A sketch that calculates the acceleration and angles of the accelerometer
A sketch that calculates the acceleration and angles of the accelerometer

Step 6

So, we learned how to take and process data from the ADXL335 analog accelerometer using the Arduino. Now we can use the accelerometer in our designs.

Recommended: