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.
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.
Download the code from https://github.com/primerobotics/Arduino
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
note:vcc-->5v ,but ADXL335 Vs is 3.3V
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.
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)
// initialize the serial communications:
int x = analogRead(xpin); //read from xpin
int y = analogRead(ypin); //read from ypin
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
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(((float)y - zero_Gy)/scale_y); //print y value on serial monitor
Serial.print(((float)z - zero_Gz)/scale_z); //print z value on serial monitor
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.