Overview of Accelerometer
ADXL335 Accelerometer Module
An accelerometer is an electromechanical device that measures acceleration due to gravity in units of g.
It is commonly used in applications that require tilt sensing.
The ADXL335 accelerometer measures acceleration along the X, Y, and Z axes and provides an analog voltage output proportional to the acceleration on each of these axes.
Microcontrollers can process these voltage signals by converting them into digital signals using an ADC.
For more information on the ADXL335 accelerometer and how to use it, refer to the ADXL335 Accelerometer Module topic in the sensors and modules section.
Connection Diagram of Accelerometer ADXL335 with Arduino
Interfacing ADXL335 Accelerometer Module With Arduino UNO
Read Accelerometer ADXL335 Using Arduino Uno
To find the roll and pitch of a device using the analog voltages from the accelerometer module and display the results on the Arduino’s serial monitor:
The accelerometer’s analog outputs are used to calculate the roll and pitch angles based on the acceleration values along the X, Y, and Z axes. These values are then processed by the Arduino and displayed on the serial monitor.
Accelerometer ADXL335 Code With Arduino Uno
#include <math.h>
const int x_out = A1; /* connect x_out of module to A1 of UNO board */
const int y_out = A2; /* connect y_out of module to A2 of UNO board */
const int z_out = A3; /* connect z_out of module to A3 of UNO board */
void setup() {
Serial.begin(9600);
}
void loop() {
int x_adc_value, y_adc_value, z_adc_value;
double x_g_value, y_g_value, z_g_value;
double roll, pitch, yaw;
x_adc_value = analogRead(x_out); /* Digital value of voltage on x_out pin */
y_adc_value = analogRead(y_out); /* Digital value of voltage on y_out pin */
z_adc_value = analogRead(z_out); /* Digital value of voltage on z_out pin */
Serial.print("x = ");
Serial.print(x_adc_value);
Serial.print("\t\t");
Serial.print("y = ");
Serial.print(y_adc_value);
Serial.print("\t\t");
Serial.print("z = ");
Serial.print(z_adc_value);
Serial.print("\t\t");
//delay(100);
x_g_value = ( ( ( (double)(x_adc_value * 5)/1024) - 1.65 ) / 0.330 ); /* Acceleration in x-direction in g units */
y_g_value = ( ( ( (double)(y_adc_value * 5)/1024) - 1.65 ) / 0.330 ); /* Acceleration in y-direction in g units */
z_g_value = ( ( ( (double)(z_adc_value * 5)/1024) - 1.80 ) / 0.330 ); /* Acceleration in z-direction in g units */
roll = ( ( (atan2(y_g_value,z_g_value) * 180) / 3.14 ) + 180 ); /* Formula for roll */
pitch = ( ( (atan2(z_g_value,x_g_value) * 180) / 3.14 ) + 180 ); /* Formula for pitch */
//yaw = ( ( (atan2(x_g_value,y_g_value) * 180) / 3.14 ) + 180 ); /* Formula for yaw */
/* Not possible to measure yaw using accelerometer. Gyroscope must be used if yaw is also required */
Serial.print("Roll = ");
Serial.print(roll);
Serial.print("\t");
Serial.print("Pitch = ");
Serial.print(pitch);
Serial.print("\n\n");
delay(1000);
}