Lesson 18 ADXL345

Share for us


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


– 1 * SunFounder Uno board

– 1 * Breadboard

– 1 * ADXL345 module

– 1 * USB cable

– Jumper wires



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.


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.

Relationship between output and gravity direction

Pin Function of ADXL345 Module

VSSupply Voltage
CSChip SelectI2C mode is enabled if the CS pin is tied high to VDD I/O (VDD I/O = 1.8V), so the I2C mode is set as default for the module.
SD0Serial Data Out, Alternate I2C Address Select
INT1Interrupt 1 Output
INT2Interrupt 2 Output
SDASerial Data (I2C), Serial Data In (SPI 4-Wire), Serial Data In/Out (SPI 3-Wire)
SCLSerial Communications Clock

See the following figure for the schematic diagram. 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.

Experimental Procedures

Step 1: Build the circuit

SCLA5 Uno/ pin 21 Mega2560
SDAA4 Uno/pin 20 Mega2560

Step 2: Open the code file

Step 3: Select correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

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.

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.


/******************************************************* Name: ADXL345 Description: Print the X, Y, and Z values on Serial Monitor Website: www.sunfoundder.coom Email: serice@sunfounder.com Date: 14/12/2016 Author: Daisy ********************************************************/ #include <wire.h> //Call the I2C library built in Arduino //Set the address of the register #define Register_ID 0 #define Register_2D 0x2D //standby,measurement,sleep,wake_up mode set #define Register_X0 0x32 //DATAX0, store the value of X0 #define Register_X1 0x33 //DATAX1, store the value of X1 #define Register_Y0 0x34 //DATAY0, store the value of Y0 #define Register_Y1 0x35 //DATAY1, store the value of Y1 #define Register_Z0 0x36 //DATAZ0, store the value of Z0 #define Register_Z1 0x37 //DATAZ1, store the value of Z1 int ADXAddress = 0x53; //I2C address int reading = 0; int val = 0; int X0, X1, X_out; int Y0, Y1, Y_out; int Z1, Z0, Z_out; double Xg, Yg, Zg; void setup() { Serial.begin(9600);//Set the baud rate of serial monitor as 9600bps delay(100); Wire.begin(); //Initialize I2C delay(100); Wire.beginTransmission(ADXAddress); //transmit to device ADXAddress 0x53 Wire.write(Register_2D); // Wire.write(8); //measuring enable Wire.endTransmission(); //end transmitting Serial.println(“Accelerometer Test “); } void loop() { Wire.beginTransmission(ADXAddress); //transmit to device ADXAddress 0x53 Wire.write(Register_X0); //request the X0 value Wire.write(Register_X1); //request the X1 value Wire.endTransmission(); //stop transmitting Wire.requestFrom(ADXAddress, 2); //request 2 bytes from device 0x53 if (Wire.available()