keyestudio
Share your creative ideas with us here
By RAN | 10 May 2024 | 0 Comments

HOW to Detect Acceleration with ESP32

Overview

In this kit, there is a DIY electronic building block ADXL345 acceleration sensor module, which uses the ADXL345BCCZ chip. The chip is a small, thin, low-power 3-axis accelerometer with a high resolution (13 bits) and a measurement range of ±16g that can measure both dynamic acceleration due to motion or impact as well as stationary acceleration such as gravitational acceleration, making the device usable as a tilt sensor.

Working Principle

The ADXL345 is a complete 3-axis acceleration measurement system with a selection of measurement ranges of ±2 g, ±4 g, ±8 g or ±16 g. Its digital output data is in 16-bit binary complement format and can be accessed through an SPI (3-wire or 4-wire) or I2C digital interface.

The sensor can measure static acceleration due to gravity in tilt detection applications, as well as dynamic acceleration due to motion or impact. Its high resolution (3.9mg/LSB) enables measurement of tilt Angle changes of less than 1.0°.

Components Required

image-20230506103058599

Connection Diagram

Test Code

//**********************************************************************************
/*  
 * Description : Read the X/Y/Z value of ADXL345
 * Auther      : http//www.keyestudio.com
*/
#include "adxl345_io.h"
//The port is sda-->21,scl-->22
adxl345 adxl345(21, 22);

float out_X, out_Y, out_Z;

void setup() {
  Serial.begin(57600);//Start serial port monitoring and set baud rate to 57600
  adxl345.Init();
}

void loop() {
  adxl345.readXYZ(&out_X, &out_Y, &out_Z);
  Serial.print(out_X);
  Serial.print("g   ");
  Serial.print(out_Y);
  Serial.print("g   ");
  Serial.print(out_Z);
  Serial.println("g");
  delay(100);
}
//**********************************************************************************

Code Explanation

Set 3 decimal variables out_X out_Y out_Z, and assign the measured result to out_X out_Y out_Z. The serial monitor displays the value of out_X out_Y out_Z, and the baud rate needs to be set before displaying (our default setting is 57600, which can be changed).

Adxl345.Init; Initialize the ADXX345 accelerometer.

adxl345.readXYZ(&out_X, &out_Y, &out_Z); Get the acceleration value of the X axis and return it to the variables out_X, out_Y, out_Z.

Test Result

Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully, we will use a USB cable to power on. Open the serial monitor and set baud rate to 57600.

The serial monitor displays the value corresponding to the sensor, the unit is mg, as shown in the figure below.

Leave a Reply

Your email address will not be published.Required fields are marked. *
Name
E-mail
Content
Verification code