Introduction
This experiment shows how to detect the tilt angle of an object using an accelerometer with Arduino.
Components Needed
- Accelerometer (e.g., ADXL345)
- Arduino
- Jumper Wires
Circuit Setup
- Connect the VCC and GND of the accelerometer to the 5V and GND pins on the Arduino.
- 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.
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
- If the sensor values are erratic, check the wiring and calibration.
- Ensure that the accelerometer is oriented correctly to measure the tilt angle properly.