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() {    

void loop() {    
  value_x = analogRead(A0);

//  value_y = analogRead(A1);
  //value_z = analogRead(A2);

  Serial.print("voltage  of x");
  /*Serial.print("voltage  of y");
   Serial.print("voltage  of z");
/*  zv=(value_z/1024.0*ADC_ref-zero_z)/sensitivity_z;
  angle_x =atan2(-yv,-zv)*57.2957795+90;

  Serial.print ("x= ");
  Serial.println (xv);
  Serial.print("x_degree= ");
  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("Rotation ");

   Serial.print("x= ");

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

   Serial.print(" deg");
   Serial.print(" ");

   Serial.print("y= ");

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

   Serial.print(" deg");
   Serial.print(" ");

   Serial.print("z= ");

   angle_z =atan2(-yv,-xv)*57.2957795+180;

   Serial.print(" deg");

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;
  • 1
    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. Jul 30 '14 at 14:44
  • 1
    Wait... what is wrong with calcPos()? Aug 7 '14 at 13:48
  • CORDIC may be a suitable method as a replacement for trigonometric functions. 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? 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. Mar 3 '18 at 17:18

So atan2(y,x) gives the angle CCW from the x axis in radians, limited to [-pi,pi].

If you want it converted to degrees CW from the Y axis, you would do


If you need to constrain it to -70,70 then:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.