Share for us

Introduction

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

Components

– 1 * SunFounder Uno board

– 1 * USB cable

– Jumper wires

Principle

Accelerometer

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.

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

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

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.

Code