# Analog angle conversion from +70° to -70°

I am using below sensor (accelerometer) .My application is a solar tracker, where I wanted to convert the sensor output into +70° to -70° format.

My question is how can I convert the angle?

``````#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(9600);
}

void loop() {

Serial.print("voltage  of x");
Serial.println(value_x);
/*Serial.print("voltage  of y");
Serial.println(value_y);
Serial.print("voltage  of z");
Serial.println(value_z);
*/
//xv=(value_x/1024.0*2.5)/sensitivity_x;
xv=(0.0032226562499999998*value_x)/sensitivity_x;
angle_x =atan2(-yv,-zv)*57.2957795+90;

Serial.print ("x= ");
Serial.println (xv);
Serial.print("x_degree= ");
Serial.print(angle_x);
Serial.println(" deg");

/*Serial.print(" g ");

Serial.print ("y= ");
Serial.print (yv);
Serial.print(" g ");

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);
}
``````

I have inclinometer off the shelf its Vout is 0~5 V and angle measure from -70° to 70°, and I calculated as below.

``````void calcPos()
{
voltage = Sensor_Value * ARDUINO_ANALOG_SCALING;
tracker_actual_pos=(30*voltage)-75;
}
``````
• If you are using a multi-axis accelerometer such as you linked to measure angles, then you probably need to take an inverse tangent of the ratio between two axis. If that proves too painful to calculate, you can make a lookup table of tangents for each degree or so, and search it for the closest value. – Chris Stratton Jul 30 '14 at 14:44
• Wait... what is wrong with `calcPos()`? – Anonymous Penguin Aug 7 '14 at 13:48
• CORDIC may be a suitable method as a replacement for trigonometric functions. – CharlieHanson Mar 9 '16 at 10:49
• @CharlieHanson: Would you really use CORDIC on a CPU that does have a hardware multiplier? Have you ever tried? – Edgar Bonet Mar 9 '16 at 15:47
• With a question like this, first consider why you need an angle in degrees. If it's to transmit to some other system, it may be possible to transmit in measurement units, and have the other system take the arctangent. Or if it is for a display of limited resolution, a lookup table may be an option. Likely if you are doing little else, your Arduino can calculate the arctangent by any of several methods, but in a more burdened system thinking about the nature of the problem can lead to better solutions. – Chris Stratton Mar 3 '18 at 17:18

``````azimuth=90.0-atan2(y,x)*57.295779;
``````azimuth_constrained=constrain(azimuth,-70,70);