Example Code for Arduino-Tilt Angle Measurement

Last revision 2026/01/07

This article guides you through measuring tilt angles using Arduino with a Triple Axis Accelerometer MMA7361. It covers necessary hardware, software preparation using Arduino IDE, includes a wiring diagram, and provides sample code for angle measurement, ensuring you can accurately obtain tilt values for your project.

Hardware Preparation

You will need some extra hardware to convert this analog signal to a usable digital one. The Arduino is really good option for it. This break board is especially designed for Arduino which has 3 JST connector that can be easily plug into our IO/Sensor expansion board.

Software Preparation

  • Development tools: Arduino IDE
  • Download link: Arduino IDE

Wiring Diagram

Triple Axis Accelerometer MMA7361 diagram

Sample Code


// # Editor    :Holiday from DFRobot
// # Data      09.10.2013

// # Product name:Triple Axis Accelerometer MMA7361
// # Product SKU:SEN0143
// # Version :  2.0

// # Description:
// # This sample shows how to measure the angle value using two axis value


#include<math.h>
#include<stdio.h>

#define A_X  5
#define A_Y  4
#define A_Z  3

int val_x,val_y,val_z;
double b;
void setup()
{
  pinMode(A_X,INPUT);
  pinMode(A_Y,INPUT);
  pinMode(A_Z,INPUT);
  Serial.begin(9600);
}
void loop()
{
  float a;
   for (int i=0;i<10;i++)
  {
    val_x+=analogRead(A_X);delay(2);
    val_y+=analogRead(A_Y);delay(2);
    val_z+=analogRead(A_Z);delay(2);
  }
   val_x=val_x/10;
   val_y=val_y/10;
   val_z=val_z/10;
   delay(300);
   Serial.print(" X_Axis: ");
   Serial.print(val_x);
   Serial.print(" Z_Axis: ");
   Serial.print(val_z);
   Serial.print("      ");
   b=(double) (abs(val_x-320))/(abs(val_z-320));
   Serial.print(" B: ");
   Serial.print(b);
   Serial.print("      ");
   a=atan(b);
   Serial.print(" A: ");
   Serial.println(a/3.14*180);              //the value of Angle
   val_x=0;
   val_y=0;
   val_z=0;

}

Was this article helpful?

TOP