Accident Detecting System Help Needed

Hey guys actually iam making an accident detector with mpu 6050 ,neo 6m gps moule ,sim 800L gsm and arduino mega.i went to check in the serial monitor if this code works.but it showed mpu initialization failed.

the wiring is

VCC to 5V
GND TO GND
SDA TO PIN 20 of mega
SCL TO PIN 21 of mega

#include <Wire.h>
#include <TinyGPS++.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_FONA.h>
#include <SoftwareSerial.h>

// Define MPU6050 object
Adafruit_MPU6050 mpu;

// Define GPS object
TinyGPSPlus gps; // Initialize TinyGPS++ object

// Define GSM object
SoftwareSerial gsmSerial(16, 17); // RX, TX (not used on Mega)
Adafruit_FONA fona = Adafruit_FONA(6); // Use the appropriate pin number for your SIM800L module

// Global variables
bool accidentDetected = false;
float lastAccelMag = 0.0;
char phone_number[] = "Your_Phone_Number"; // Replace with your phone number
char message[160]; // Buffer for SMS
float thresholdValue = 10.0; // Adjust this threshold as needed

// Function prototypes
void initializeMPU();
void initializeGPS();
void initializeGSM();
void sendSMS(const char* phoneNumber, const char* message);

void setup() {
  Serial.begin(9600); // This is the Serial monitor

  // Initialize sensors and modules
  initializeMPU();
  initializeGPS();
  initializeGSM();
}

void loop() {
  // Read accelerometer data
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  float accelX = a.acceleration.x;
  float accelY = a.acceleration.y;
  float accelZ = a.acceleration.z;

  float accelMag = sqrt(accelX * accelX +
                        accelY * accelY +
                        accelZ * accelZ);

  // Check for accident
  if (accelMag > thresholdValue && accelMag > lastAccelMag) {
    accidentDetected = true;
  }
  lastAccelMag = accelMag;

  // GPS data
  while (Serial1.available()) { // Read from Serial1 (GPS)
    gps.encode(Serial1.read());
  }

  if (gps.location.isValid() && accidentDetected) {
    // Get latitude and longitude
    float latitude = gps.location.lat();
    float longitude = gps.location.lng();

    // Create Google Maps link
    String mapLink = "https://maps.google.com/?q=" + String(latitude, 6) + "," + String(longitude, 6);

    // Compose SMS message
    sprintf(message, "Accident detected! Location: %s", mapLink.c_str());

    // Send SMS
    sendSMS(phone_number, message);
    accidentDetected = false;
  }
}

void initializeMPU() {
  if (!mpu.begin()) {
    Serial.println("MPU6050 initialization failed");
    while (1);
  }
}

void initializeGPS() {
  Serial1.begin(9600);  // Set the GPS module's baud rate to match your module
}

void initializeGSM() {
  gsmSerial.begin(9600);
  delay(1000);
  
  // Ensure SIM card is accessible
  if (!fona.begin(gsmSerial)) {
    Serial.println("Couldn't find SIM800L module");
    while (1);
  }
  
  // Unlock SIM card (replace with your SIM card PIN)
  if (!fona.unlockSIM("YOUR_SIM_PIN")) {
    Serial.println("SIM card unlock failed");
    while (1);
  }
  
  // Set SMS mod
}

void sendSMS(const char* phoneNumber, const char* message) {
  if (fona.sendSMS(phoneNumber, message)) {
    Serial.println("SMS sent successfully");
  } else {
    Serial.println("Failed to send SMS");
  }
}

So I Am Getting The Error And I am Having A Deadline Of 3 days. Kindly Tell What to do. Iam using all the components Breakout Board and then soldered them.
Please please Help Me with This

2 posts were merged into an existing topic: I am getting errors