ADXL320 Acceleration Measurement using Arduino

In this article, we will show you how to measure acceleration using the ADXL320 acceleration sensor and Arduino Uno R3. We will explain what the ADXL320 sensor does and how it can be connected to the Arduino Uno R3. Furthermore, we will guide you on writing code to accurately measure acceleration in two directions.

When it comes to acceleration measurement sensors, there are several high-quality options available in the market. Here are some notable ones:

  1. ADXL335: This is a three-axis acceleration sensor that can measure acceleration in various directions. It is widely used in robotics and motion-sensing applications.
  2. MPU-6050: Known as a 6-DOF (degree of freedom) sensor, the MPU-6050 combines a three-axis accelerometer and a three-axis gyroscope. It provides precise motion tracking capabilities.
  3. LIS3DH: This accelerometer offers high-resolution measurements and operates in a low-power mode, making it suitable for battery-operated devices. It supports both motion and free-fall detection.

By exploring the use of the ADXL320 sensor with Arduino Uno R3, you will gain valuable insights into measuring and analyzing acceleration data for your own projects.

  • ADXL203CE (SFSEN-00844),
  • ADXL320 (SF SEN 00847),
  • MMA7260Q (SF SEN00252)

ADXL320 Accelemoter

Acceleration sensors detect movement of objects from the starting point to the endpoint. They can only work in two directions, specifically detecting bi-directional movements in the X and Y axes. Furthermore, acceleration sensors are utilized to determine the orientation of objects based on the X and Y axes.

Analog sensors are employed for acceleration sensing. This implies that when interfacing them with microcontrollers or Arduino, you must utilize the Analog to Digital Converter (ADC) channel of Arduino. To connect the ADXL320 sensor to Arduino Uno R3, you should connect the two analog outputs of the sensor to two ADC channels of Arduino.

ADXL320 Sensor Pinouts

The ADXL320 acceleration measurement sensor has 6 pins. Four of the pins are used to connect with Arduino. The description of each pin is given below:

  • X analog pin is analog output along the x-axis.
  • Y analog pin is analog output along the y-axis.
  • VDD 5-volt power supply input pin.
  • GND ground pin.

ADXL320 Interfacing with Arduino

The circuit diagram represents an acceleration measurement module.

Overall, this circuit diagram illustrates a basic setup for an acceleration measurement module, where an accelerometer measures acceleration and Arduino Uno processes the ADXL320 accelerometer sensor data.

acceleration meaurement using Arduino and ADXL320
acceleration measurement using Arduino and ADXL320

Code

The Arduino board reads acceleration data from an ADXL345 sensor using this code. It includes the necessary libraries for I2C communication and sensor handling. The code initializes the ADXL345 sensor and checks if it’s connected. In the main loop, it repeatedly reads the acceleration values along the X, Y, and Z axes from the sensor and prints them to the serial monitor. The delay(1000) pauses the loop for 1 second between readings, resulting in a steady stream of acceleration data being displayed on the serial monitor.

// Code for acceleration measurement sensor

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void setup() {
  Serial.begin(9600);
  if(!accel.begin())
  {
    Serial.println("Could not find a valid ADXL345 sensor, check wiring!");
    while(1);
  }
}

void loop() {
  sensors_event_t event;
  accel.getEvent(&event);
  
  float x = event.acceleration.x;
  float y = event.acceleration.y;
  float z = event.acceleration.z;
  
  Serial.print("X: "); Serial.print(x); 
  Serial.print(" | Y: "); Serial.print(y); 
  Serial.print(" | Z: "); Serial.println(z);
  
  delay(1000);
}

How Does the Code Work?

Here is a detailed explanation of each line of code:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>

These lines include the necessary libraries for I2C communication and sensor handling. The Wire library is used for I2C communication, while the Adafruit_Sensor and Adafruit_ADXL345_U libraries are specific to the ADXL345 sensor.

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

This line creates an instance of the Adafruit_ADXL345_Unified class called accel. The parameter 12345 in the constructor is just a unique identifier for the sensor (can be any value).

void setup() {
  Serial.begin(9600);

  if (!accel.begin()) {
    Serial.println("Could not find a valid ADXL345 sensor, check wiring!");
    while (1);
  }
}

The setup() function is called once when the Arduino board is powered on or reset. This function initializes the serial communication at a baud rate of 9600. It then checks if the ADXL345 sensor is connected and functioning properly. If the sensor is not found, the program will output an error message and enter an infinite loop.

void loop() {
  sensors_event_t event;
  accel.getEvent(&event);

  float x = event.acceleration.x;
  float y = event.acceleration.y;
  float z = event.acceleration.z;

  Serial.print("X: ");
  Serial.print(x);
  Serial.print(" | Y: ");
  Serial.print(y);
  Serial.print(" | Z: ");
  Serial.println(z);

  delay(1000);
}

The loop() function is the main part of the program that runs repeatedly. Within this function, it declares a variable event of type sensors_event_t to store the sensor data.

The line accel.getEvent(&event) reads the acceleration values along the X, Y, and Z axes from the ADXL345 sensor and stores them in the event variable.

The next three lines assign the acceleration values to individual variables (x, y, z) for easier manipulation and printing.

The subsequent lines use the Serial.print() and Serial.println() functions to output the X, Y, and Z acceleration values to the serial monitor. The values are separated by the pipe character (|) for clarity. Serial.print() is used for the first two values, and Serial.println() is used for the last value to add a newline character at the end.

Lastly, delay(1000) pauses the program for 1 second between each reading of acceleration data to create a steady stream of values on the serial monitor.

Demonstration

To use this code, copy and paste it into your Arduino IDE. Make sure you have the required libraries installed, and connect the ADXL345 sensor as shown in the circuit diagram. After uploading the code to your Arduino board, you can view the X and Y direction values in the serial monitor of your computer.

Conclusion

In conclusion, this article discussed the ADXL320 acceleration measurement sensor and its application with the Arduino. We explored the functionality of the ADXL320 sensor, learned about its connection to the Arduino Uno R3, and provided guidance on writing code to measure acceleration in two directions.

We also provided a list of other notable acceleration measurement sensors, such as the ADXL335, MPU-6050, and LIS3DH, each with their own unique features and applications.

The article included a detailed circuit diagram showing the connection between the ADXL320 sensor and Arduino Uno R3, as well as a code example for reading acceleration data from an ADXL345 sensor using Arduino. The code explained the purpose of each line and provided an overview of how it works.

You may also like to read:

Leave a Comment