ESP32-Based Real-Time Accident Detection System with GSM, GPS, and Buzzer
View and Edit Project In Cirkit Designer
Real-time Accident Detection System Documentation
Summary
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.
Component List
ESP32
- Description: A powerful microcontroller with Wi-Fi and Bluetooth capabilities.
- Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, VIN, 3V3, D15, D2, D4, RX2, TX2, D5, D18, D19, D21, RX0, TX0, D22, D23, BOOT
GPS NEO 6M
- Description: A GPS module used for obtaining location data.
- Pins: VCC, RX, TX, GND
Adafruit ADXL345
- Description: A 3-axis accelerometer used for detecting motion and acceleration.
- Pins: VIN, 3.3V, GND, CS, INT1, INT2, SDO/ADDR, SDA/SDIO, SCL
SIM800L
- Description: A GSM module used for sending SMS and making calls.
- Pins: NET, VCC, RST, RXD, TXD, GND, RING, DTR, MIC+, MIC-, SPK+, SPK-
Battery 18650
- Description: A rechargeable lithium-ion battery.
- Pins: +, -
Buzzer
- Description: An audio signaling device.
- Pins: +, GND
5V Battery
- Description: A power source providing 5V.
- Pins: +, -
Wiring Details
ESP32
- D25: Connected to Buzzer (+)
- D26: Connected to SIM800L (TXD)
- D27: Connected to SIM800L (RXD)
- GND: Connected to Battery 18650 (-), 5V Battery (-), GPS NEO 6M (GND), Buzzer (GND), Adafruit ADXL345 (GND), SIM800L (GND), SIM800L (RST)
- RX2: Connected to GPS NEO 6M (TX)
- TX2: Connected to GPS NEO 6M (RX)
- D21: Connected to Adafruit ADXL345 (SDA/SDIO)
- D22: Connected to Adafruit ADXL345 (SCL)
GPS NEO 6M
- VCC: Connected to Battery 18650 (+)
- RX: Connected to ESP32 (TX2)
- TX: Connected to ESP32 (RX2)
- GND: Connected to ESP32 (GND)
Adafruit ADXL345
- VIN: Connected to Battery 18650 (+)
- GND: Connected to ESP32 (GND)
- SDA/SDIO: Connected to ESP32 (D21)
- SCL: Connected to ESP32 (D22)
SIM800L
- VCC: Connected to 5V Battery (+)
- GND: Connected to ESP32 (GND)
- RST: Connected to ESP32 (GND)
- RXD: Connected to ESP32 (D27)
- TXD: Connected to ESP32 (D26)
Battery 18650
- +: Connected to GPS NEO 6M (VCC), Adafruit ADXL345 (VIN)
- -: Connected to ESP32 (GND)
Buzzer
- +: Connected to ESP32 (D25)
- GND: Connected to ESP32 (GND)
5V Battery
- +: Connected to SIM800L (VCC)
- -: Connected to ESP32 (GND)
Code Documentation
// 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