Arduino Projects
Vehicle Accident Alert System Using ADXL-335 Sensor
Vehicle Accident Alert System Using ADXL-335 Sensor | GSM | GPS
Introduction
In this article, we demonstrate a Vehicle Accident Alert System that utilizes the ADXL-335 accelerometer sensor, GSM module, and GPS technology to enhance road safety.
The ADXL-335 sensor detects sudden changes in acceleration, indicating potential accidents. When a collision is detected, the system immediately sends an alert message with the vehicle’s GPS location to emergency contacts via GSM.
Bill of material
- Arduino Nano
- ADXL-335 Sensor
- Sim800l GSM Module
- GPS Neo-6m Module
- LM2596 Stepdown Converter
- Buzzer
- Push Button
Circuit Diagram
Code
LiquidCrystal_I2C
AltSoftSerial
TinyGPS++
Set ADXL-335 Sensor Magnitude Using The Code
Open the Serial Monitor And check the ADXL335 Sensor reading.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
//Prateek //www.justdoelectronics.com const int x = A0; const int y = A1; const int z = A2; int xv = 0, yv = 0, zv = 0; #define buzzer 12 void setup() { Serial.begin(9600); Serial.println("Serial.begin"); pinMode(buzzer, OUTPUT); delay(2000); } void loop() { adxl(); delay(1000); } void adxl() { xv = analogRead(x); yv = analogRead(y); zv = analogRead(z); Serial.print("x= "); Serial.print(xv); Serial.print(" y= "); Serial.print(yv); Serial.print(" z= "); Serial.println(zv); if ((xv < 280) || (xv > 380) || (yv < 280) || (yv > 380)) { Serial.println("Accident happen"); digitalWrite(buzzer, HIGH); } else { Serial.println("Normal"); digitalWrite(buzzer, LOW); } } |
Now Select the Proper Board And Upload the Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
#include <LiquidCrystal_I2C.h> #include <SoftwareSerial.h> #include <AltSoftSerial.h> #include <TinyGPS++.h> #include <math.h> #include <Wire.h> LiquidCrystal_I2C lcd(0x27, 16, 2); const String EMERGENCY_PHONE = "+9188xxxxxxxx"; #define rxPin 2 #define txPin 3 SoftwareSerial sim800(rxPin, txPin); AltSoftSerial neogps; TinyGPSPlus gps; String sms_status, sender_number, received_date, msg; String latitude, longitude; #define BUZZER 13 #define BUTTON 10 #define xPin A0 #define yPin A1 #define zPin A2 byte updateflag; int xaxis = 0, yaxis = 0, zaxis = 0; int deltx = 0, delty = 0, deltz = 0; int vibration = 2, devibrate = 75; int magnitude = 0; int sensitivity = 65; double angle; boolean impact_detected = false; unsigned long time1; unsigned long impact_time; unsigned long alert_delay = 10000; void setup() { Serial.begin(9600); sim800.begin(9600); neogps.begin(9600); pinMode(BUZZER, OUTPUT); pinMode(BUTTON, INPUT_PULLUP); lcd.init(); lcd.backlight(); lcd.clear(); sms_status = ""; sender_number = ""; received_date = ""; msg = ""; sim800.println("AT"); delay(1000); sim800.println("ATE1"); delay(1000); sim800.println("AT+CPIN?"); delay(1000); sim800.println("AT+CMGF=1"); delay(1000); sim800.println("AT+CNMI=1,1,0,0,0"); delay(1000); time1 = micros(); xaxis = analogRead(xPin); yaxis = analogRead(yPin); zaxis = analogRead(zPin); } void loop() { if (micros() - time1 > 1999) Impact(); if (updateflag > 0) { updateflag = 0; Serial.println("Impact detected!!"); Serial.print("Magnitude:"); Serial.println(magnitude); getGps(); digitalWrite(BUZZER, HIGH); impact_detected = true; impact_time = millis(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("Crash Detected"); lcd.setCursor(0, 1); lcd.print("Effect:" + String(magnitude)); } if (impact_detected == true) { if (millis() - impact_time >= alert_delay) { digitalWrite(BUZZER, LOW); delay(1000); impact_detected = false; impact_time = 0; } } if (digitalRead(BUTTON) == LOW) { delay(200); digitalWrite(BUZZER, LOW); impact_detected = false; impact_time = 0; } while (sim800.available()) { parseData(sim800.readString()); } while (Serial.available()) { sim800.println(Serial.readString()); } } void Impact() { time1 = micros(); int oldx = xaxis; int oldy = yaxis; int oldz = zaxis; xaxis = analogRead(xPin); yaxis = analogRead(yPin); zaxis = analogRead(zPin); vibration--; if (vibration < 0) vibration = 0; if (vibration > 0) return; deltx = xaxis - oldx; delty = yaxis - oldy; deltz = zaxis - oldz; magnitude = sqrt(sq(deltx) + sq(delty) + sq(deltz)); if (magnitude >= sensitivity) { updateflag = 1; vibration = devibrate; } else { magnitude = 0; } } void parseData(String buff) { Serial.println(buff); unsigned int len, index; index = buff.indexOf("\r"); buff.remove(0, index + 2); buff.trim(); if (buff != "OK") { index = buff.indexOf(":"); String cmd = buff.substring(0, index); cmd.trim(); buff.remove(0, index + 2); if (cmd == "+CMTI") { index = buff.indexOf(","); String temp = buff.substring(index + 1, buff.length()); temp = "AT+CMGR=" + temp + "\r"; sim800.println(temp); } else if (cmd == "+CMGR") { if (buff.indexOf(EMERGENCY_PHONE) > 1) { buff.toLowerCase(); if (buff.indexOf("get gps") > 1) { getGps(); String sms_data; sms_data = "GPS Location Data\r"; sms_data += "http://maps.google.com/maps?q=loc:"; sms_data += latitude + "," + longitude; } } } } else { } } void getGps() { boolean newData = false; for (unsigned long start = millis(); millis() - start < 2000;) { while (neogps.available()) { if (gps.encode(neogps.read())) { newData = true; break; } } } if (newData) { latitude = String(gps.location.lat(), 6); longitude = String(gps.location.lng(), 6); newData = false; } else { Serial.println("No GPS data is available"); latitude = ""; longitude = ""; } Serial.print("Latitude= "); Serial.println(latitude); Serial.print("Lngitude= "); Serial.println(longitude); } boolean SendAT(String at_command, String expected_answer, unsigned int timeout) { uint8_t x = 0; boolean answer = 0; String response; unsigned long previous; while (sim800.available() > 0) sim800.read(); sim800.println(at_command); x = 0; previous = millis(); do { if (sim800.available() != 0) { response += sim800.read(); x++; if (response.indexOf(expected_answer) > 0) { answer = 1; break; } } } while ((answer == 0) && ((millis() - previous) < timeout)); Serial.println(response); return answer; } |
Video