0% found this document useful (0 votes)
129 views4 pages

Arduino Tilt Angle Calculation Code

This Arduino program uses an ADXL335 accelerometer to calculate tilt angles. It takes analog readings from the accelerometer's three channels and converts them to x, y, z values in g's. It then uses the atan2 function to calculate the tilt angles in degrees for each axis based on the relative lengths of the x, y, z vectors. The results are printed to the serial monitor every second.

Uploaded by

udaynandhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views4 pages

Arduino Tilt Angle Calculation Code

This Arduino program uses an ADXL335 accelerometer to calculate tilt angles. It takes analog readings from the accelerometer's three channels and converts them to x, y, z values in g's. It then uses the atan2 function to calculate the tilt angles in degrees for each axis based on the relative lengths of the x, y, z vectors. The results are printed to the serial monitor every second.

Uploaded by

udaynandhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Arduino Program to Calculate Tilt angle using ADXL335

#define ADC_ref 2.56


#define zero_x 1.569
#define zero_y 1.569
#define zero_z 1.569
#define sensitivity_x 0.3
#define sensitivity_y 0.3
#define sensitivity_z 0.3
ADC reference voltage is 2.56V. Default “on board 5V” is not selected
Because it is slightly lower than 5.00V and can be unstable. 0g voltage
After calibration is set to 1.569V. The sensitivity for calculations
used is default 300mV/g.
void setup() {
analogReference(INTERNAL2V56);
Serial.begin(256000);
Setting the ADC reference to 2.56V and starting serial communication
Void loop() {
value_x = analogRead(0);
value_y = analogRead(1);
value_z = analogRead(2);
At the loop A/D conversions are performed for each of the 3 channels.
xv=(value_x/1024.0*ADC_ref-zero_x)/sensitivity_x;
angle_z =atan2(-yv,-xv)*57.2957795+180;
The rotation (for Z axis)is calculated using atan2 function. It
calculates angle from length of X,Y vectors. *57.2957795 – is
conversion of radian to degree. +180 is for offset.
// Simple angle meter using ADXL335 accelerometer
#define ADC_ref 2.56
#define zero_x 1.569

#define zero_y 1.569

#define zero_z 1.569

#define sensitivity_x 0.3

#define sensitivity_y 0.3

#define sensitivity_z 0.3

unsigned int value_x;

unsigned int value_y;

unsigned int value_z;

float xv;

float yv;

float zv;

float angle_x;

float angle_y;

float angle_z;

void setup() {

analogReference(INTERNAL2V56);

Serial.begin(256000);

void loop() {

value_x = analogRead(0);

value_y = analogRead(1);

value_z = analogRead(2);

xv=(value_x/1024.0*ADC_ref-zero_x)/sensitivity_x;
Serial.print ("x= ");

Serial.print (xv);

Serial.print(" g ");

yv=(value_y/1024.0*ADC_ref-zero_y)/sensitivity_y;

Serial.print ("y= ");

Serial.print (yv);

Serial.print(" g ");

zv=(value_z/1024.0*ADC_ref-zero_z)/sensitivity_z;

Serial.print ("z= ");

Serial.print (zv);

Serial.print(" g ");

Serial.print("\n");

Serial.print("Rotation ");

Serial.print("x= ");

angle_x =atan2(-yv,-zv)*57.2957795+180;

Serial.print(angle_x);

Serial.print(" deg");

Serial.print(" ");

Serial.print("y= ");

angle_y =atan2(-xv,-zv)*57.2957795+180;

Serial.print(angle_y);

Serial.print(" deg");

Serial.print(" ");

Serial.print("z= ");
angle_z =atan2(-yv,-xv)*57.2957795+180;

Serial.print(angle_z);

Serial.print(" deg");

Serial.print("\n");

delay(1000);

delay(1000);

You might also like