Tilt Angle Detection

Introduction

This experiment shows how to detect the tilt angle of an object using an accelerometer with Arduino.

Tilt Angle Sensor

Components Needed

Circuit Setup

  1. Connect the VCC and GND of the accelerometer to the 5V and GND pins on the Arduino.
  2. Connect the SDA and SCL pins of the accelerometer to the Arduino's SDA and SCL pins.

The accelerometer measures the acceleration in the X, Y, and Z axes to determine the tilt angle of the object.

Tilt Angle Circuit Setup

Code for Tilt Angle Detection

Upload the following code to your Arduino to detect the tilt angle:


#include 
#include 
#include 

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void setup() {
  Serial.begin(9600);
  if (!accel.begin()) {
    Serial.println("Failed to initialize accelerometer");
    while (1);
  }
}

void loop() {
  sensors_event_t event;
  accel.getEvent(&event);

  float tiltAngle = atan2(event.acceleration.y, event.acceleration.z) * 180.0 / PI;
  Serial.print("Tilt Angle: ");
  Serial.println(tiltAngle);
  delay(500);
}
            

Explanation

The accelerometer detects the gravitational pull along the X, Y, and Z axes, and the Arduino calculates the tilt angle based on these values.

Troubleshooting