This document provides a detailed overview of a real-time accident detection system. The system uses an ESP32 microcontroller, a GSM800L module, a GPS NEO 6M module, an Adafruit ADXL345 accelerometer, a buzzer, and power sources. The system detects accidents using the accelerometer, triggers a buzzer, and sends an alert message with the GPS location via the GSM module.
ESP32
GPS NEO 6M
Adafruit ADXL345
SIM800L
Battery 18650
Buzzer
5V Battery
// Real-time Accident Detection System using ESP32, GSM800L, GPS, and Buzzer
// This code detects accidents using an accelerometer, triggers a buzzer, and
// sends an alert message with GPS location via GSM module.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h> // Accelerometer library
#include <TinyGPS++.h> // GPS parsing library
#include <WiFi.h> // Wi-Fi library
#include <SoftwareSerial.h> // SoftwareSerial for SIM800L GSM module
// Pin definitions
#define BUZZER_PIN 25
#define GSM_RX_PIN 27
#define GSM_TX_PIN 26
#define GPS_RX_PIN 16
#define GPS_TX_PIN 17
#define ACCEL_SDA_PIN 21
#define ACCEL_SCL_PIN 22
// GSM setup (SIM800L GSM module)
SoftwareSerial gsmSerial(GSM_RX_PIN, GSM_TX_PIN); // RX, TX pins for GSM module
// GPS setup
TinyGPSPlus gps;
HardwareSerial gpsSerial(2); // Use hardware serial 2 for GPS
// Accelerometer setup
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
// Global variables
bool accident_detected = false;
String latitude = "0.0";
String longitude = "0.0";
// Threshold for accident detection
#define THRESHOLD 5.0
void setup() {
Serial.begin(115200); // For debugging
gsmSerial.begin(9600); // GSM baud rate
gpsSerial.begin(9600, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN); // GPS baud rate
pinMode(BUZZER_PIN, OUTPUT); // Buzzer output pin
// Accelerometer initialization
if (!accel.begin()) {
Serial.println("Accelerometer not detected! Check connections.");
while (1);
}
accel.setRange(ADXL345_RANGE_16_G);
// Wi-Fi connection setup (though we're not using it here, kept in case for other use)
WiFi.begin("Your_SSID", "Your_PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Wi-Fi connected.");
}
void loop() {
if (detectAccident()) {
triggerBuzzer();
String location = getLocation();
sendAlert(location);
delay(30000); // Prevent duplicate alerts within 30 seconds
}
}
bool detectAccident() {
sensors_event_t event;
accel.getEvent(&event);
// Check if any axis acceleration exceeds the threshold
if (abs(event.acceleration.x) > THRESHOLD ||
abs(event.acceleration.y) > THRESHOLD ||
abs(event.acceleration.z) > THRESHOLD) {
accident_detected = true;
Serial.println("Accident detected!");
}
return accident_detected;
}
void triggerBuzzer() {
digitalWrite(BUZZER_PIN, HIGH); // Turn on buzzer
delay(10000); // Keep buzzer on for 10 seconds
digitalWrite(BUZZER_PIN, LOW); // Turn off buzzer
Serial.println("Buzzer triggered!");
}
String getLocation() {
String loc = "Lat: 0.0, Lon: 0.0";
while (gpsSerial.available() > 0) {
if (gps.encode(gpsSerial.read())) {
if (gps.location.isUpdated()) {
latitude = String(gps.location.lat(), 6);
longitude = String(gps.location.lng(), 6);
loc = "Lat: " + latitude + ", Lon: " + longitude;
Serial.println("Location fetched: " + loc);
break;
}
}
}
return loc;
}
void sendAlert(String location) {
gsmSerial.println("AT+CMGF=1"); // Set SMS text mode
delay(1000);
gsmSerial.println("AT+CMGS=\"9087654321\""); // Set recipient number
delay(1000);
gsmSerial.print("Accident detected! Location: ");
gsmSerial.print(location);
gsmSerial.write(26); // End SMS with CTRL+Z
delay(1000);
Serial.println("Alert sent via GSM!");
}
void makeCall() {
gsmSerial.println("ATD+9087654321;"); // Make a call
delay(1000);
Serial.println("Call initiated!");
}
This code initializes the ESP32, GSM800L, GPS NEO 6M, and Adafruit ADXL345 accelerometer. It continuously monitors the accelerometer for any sudden changes in acceleration that exceed a predefined threshold, indicating a potential accident. If an accident is detected, the buzzer is triggered, and an alert message with the GPS location is