ADXL345 Using Arduino Uno R3

About: PrimeRobotics is a E-Commerce site, which focus on supplying right products to Electronics Hobbyists, Enthusiast & Students.

In this lesson, we will learn how to use the acceleration sensor ADXL345.

Step 1: ​Components

Step 2: ​Principle

An accelerometer is used to measure the force generated during the acceleration. The most fundamental is the commonly-known acceleration of gravity which is 1g.

By measuring the acceleration caused by gravity, you can calculate the tilt angle of the device to the level surface. Through analyzing the dynamic acceleration, you can tell the way how the device is moving. For example, self-balancing board or hoverboard applies the acceleration sensor and gyroscope for Kalman filter and posture correction.

ADXL345

The ADXL345 is a small, thin, low power, 3-axis accelerometer with high resolution (13-bit) measurement at up to ±16 g. Digital output data is formatted as 16-bit two’s complement and is accessible through either an SPI (3- or 4-wire) or I2C digital interface. In this experiment, the I2C digital interface is used.

It is well suited to measure the static acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration resulting from motion or shock. Its high resolution (4 mg/LSB) enables the inclination change measurement by less than 1.0°. And the excellent sensitivity (3.9mg/LSB @2g) provides a high-precision output of up to ±16g.

How ADXL345 works

The ADXL345 detects the acceleration with the sensing component at the front, and then the electric signal sensing component changes it into electric signal, which is analog. Next, the AD adapter integrated on the module will convert the analog signal into digital one.

The X_OUT, Y_OUT and Z_OUT are the values at the X, Y, and Z axis respectively. Place the module face up: Z_OUT can reach +1g at most, the minimum of X_OUT is -1g toward the Ax direction, and the minimum of Y_OUT is -1g toward the Ay direction. On the other hand, turn the module upside down: the minimum of Z_OUT is -1g, the maximum of X_OUT is +1g toward the Ax direction, and the maximum of Y_OUT is +1g toward the Ay direction. , as shown below. Rotate the ADXL345 module and you'll see the change of three values.

when channel A changes from high level to low level, if channel B is high level, it indicates the rotary encoder spins clockwise (CW); if at that moment channel B is low level, it means spins counterclockwise (CCW). So if we read the value of channel B when channel A is low level, we can know in which direction the rotary encoder rotates.

Principle: See the schematic diagram of the Rotary Encoder module below. From it we can see that pin 3 of the rotary encoder, namely CLK on the module, is channel B. Pin 5, which is DT, is channel A. To know the rotational direction of the recorder, just read the value of CLK and DT.

There is a 3.3v voltage regulator chip in the circuit, so you can power
the module with 5V or 3.3V.

Since SDO has been connected to GND, the I2C address of the ADXL345 is 0x53, 0xA6 for write, 0xA7 for read

Pin Function of ADXL345 Module.

Step 3: Procedures

Step 1. Build the circuit.

Step 2:

Download the code from https://github.com/primerobotics/Arduino

Step 3:

Upload the sketch to the Arduino Uno board

Click the Upload icon to upload the code to the control board.

If "Done uploading" appears at
the bottom of the window, it means the sketch has been successfully uploaded.

After uploading, open Serial Monitor, where you can see the data detected. When the acceleration of the module changes, the figure will change accordingly on the window.

Step 4: Code

//ADXL335

/********************************

ADXL335

note:vcc-->5v ,but ADXL335 Vs is 3.3V

The circuit:

5V: VCC

analog 0: x-axis

analog 1: y-axis

analog 2: z-axis

After burning the program, open the serial monitor debugging window, where you can see the data detected being displayed. When the acceleration varies, the figure will vary accordingly.

*********************************

/Email: info@primerobotics.in

//Website:www.primerobotics.in

const int xpin = A0; // x-axis of the accelerometer

const int ypin = A1; // y-axis

const int zpin = A2; // z-axis (only on 3-axis models)

void setup()

{

// initialize the serial communications:

Serial.begin(9600);

}

void loop()

{

int x = analogRead(xpin); //read from xpin

delay(1); //

int y = analogRead(ypin); //read from ypin

delay(1);

int z = analogRead(zpin); //read from zpin

float zero_G = 338.0; //ADXL335 power supply by Vs 3.3V:3.3V/5V*1024=676/2=338

//Serial.print(x);

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

//Serial.print(y);

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

//Serial.print(z);

//Serial.print("\n");

float zero_Gx=331.5;//the zero_G output of x axis:(x_max + x_min)/2

float zero_Gy=329.5;//the zero_G outgput of y axis:(y_max + y_min)/2

float zero_Gz=340.0;//the zero_G output of z axis:(z_max + z_min)/2

float scale = 67.6;//power supply by Vs 3.3V:3.3v /5v *1024/3.3v *330mv/g =67.6g

float scale_x = 65;//the scale of x axis: x_max/3.3v*330mv/g

float scale_y = 68.5;//the scale of y axis: y_max/3.3v*330mv/g

float scale_z = 68;//the scale of z axis: z_max/3.3v*330mv/g

Serial.print(((float)x - zero_Gx)/scale_x); //print x value on serial monitor

Serial.print("\t");

Serial.print(((float)y - zero_Gy)/scale_y); //print y value on serial monitor

Serial.print("\t");

Serial.print(((float)z - zero_Gz)/scale_z); //print z value on serial monitor

Serial.print("\n");

delay(1000); //wait for 1 second

}

Step 5: ​Code Analysis

The code for ADXL345 experiment includes 3 parts: initialize each port and device, acquire and store data sent from the sensors, and convert the data.

Share

    Recommendations

    • Paper Contest

      Paper Contest
    • Organization Contest

      Organization Contest
    • Build a Tool Contest

      Build a Tool Contest

    Discussions