Accident Detection with Raspberry Pi, GPS and GSM - Proteus Simulation: an MPU6050, a SIM808 GSM GPS module, a cancel button, a buzzer and an LCD on a Raspberry Pi 5

Accident Detection with Raspberry Pi, GPS and GSM - Proteus Simulation

40
700
60
25
60
PCBWay

Hello friends, I hope you all are doing great. Today, I am going to share the Accident Detection with Raspberry Pi, GPS and GSM - Proteus Simulation, project 110 of our IoT Projects series. After a road accident, the first minutes decide a lot, and on a quiet road at night nobody may see a car that has left the road or turned over. A vehicle accident detection and alert system calls for help by itself: it notices the crash, and it sends a text message with the exact place to a family member or an emergency number. It is one of the most searched final year projects, and here is a complete one on a Raspberry Pi 5, with the circuit, the Python program and a Proteus simulation you can run before you build anything.

An MPU6050 accelerometer measures how the vehicle sits and how hard it is shaken, 50 times a second. If the vehicle turns over, or a jolt is stronger than 3 g, the Pi starts a 10 second countdown: a buzzer beeps and the LCD asks the driver to press CANCEL. A driver who is fine presses the button and nothing is sent. If nobody cancels, a SIM808 module, which has a GSM modem and a GPS receiver on one board, reads the position and texts a Google Maps link to the emergency contact. The contact can also text LOCATION at any time and get the vehicle's position back. In this tutorial, we will go through the circuit, the detection rules, the AT commands and the Python code, and then crash the vehicle three times in the simulation. At the end, you can download the project with the program and every library file.

NOTICE: Our team has put a lot of effort into this project: the circuit, the code and every part in the simulation are our own work, and you get them all free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below. And if our free projects help you, you can buy us a coffee. So, let's get started with the accident detection with Raspberry Pi, GPS and GSM in Proteus simulation:

Accident Detection with Raspberry Pi, GPS and GSM - Proteus Simulation: an MPU6050, a SIM808 GSM GPS module, a cancel button, a buzzer and an LCD on a Raspberry Pi 5
Figure: The vehicle accident detection system in a Proteus simulation.
Accident detection with Raspberry Pi, GPS and GSM Proteus simulation: three rollovers, the countdown and the SMS alert
Figure: The project working in the Proteus simulation (an animation of the main steps).

What Does This Accident Detection System Do?

  • Rollover detection: the tilt from upright, from the direction of gravity; over 60 degrees for 1 second is an accident.
  • Crash detection: a jolt over 3 g, far more than braking or a pothole gives.
  • A chance to cancel: 10 seconds of beeps and a message on the LCD; the CANCEL button stops a false alarm.
  • An SMS with the place: the SIM808's GPS gives the position and its GSM modem texts a Google Maps link.
  • Where are you? an SMS with the word LOCATION from the contact is answered with the position and the speed.
  • A dashboard: the state, the tilt, the GPS and the events in any browser, with CANCEL and RESET links.

In the simulation, the MPU6050's pop-up turns the vehicle over, the SIM808's pop-up has a phone that receives the messages, the push button's pop-up presses CANCEL, and the LCD and the buzzer show what a driver would see and hear.

Components Used in the Proteus Simulation

All the parts come from our Proteus libraries, each with its own tutorial. The project zip at the end includes every LIB and DLL file.

The parts of the accident detection system and their library posts
PartProteus deviceLibrary postConnection
Raspberry Pi 5RASPBERRY PI 5Raspberry Pi 5 Library for ProteusPI1, accident_alert.py, Ethernet 192.168.1.51:8000
SIM808 GSM + GPSSIM808ADVTEPSIM808 Library for ProteusU1 on the UART: TXD GPIO14, RXD GPIO15
MPU6050 accelerometerMPU6050ADVTEPMPU6050 Library for ProteusU2 on I2C1 (0x68): SDA GPIO2, SCL GPIO3
Push buttonBUTTONGREENTEPPush Button Library for ProteusSW1 CANCEL: 3V3 to GPIO17
10k resistorRESISTORSMD1206TEPSMD Resistor Library for ProteusR1: the pull-down on GPIO17
Active buzzerBUZZERACTIVETEPActive Buzzer Library for ProteusBZ1: the alarm, GPIO18
I2C LCD 16x2I2CLCD16X2BLUEADVTEPI2C LCD 16x2 Library for ProteusLCD1 on I2C1 (0x27)
Laptop WiFi MonitorLAPTOP WIFI MONITORLaptop WiFi MonitorPC1
Mobile WiFi MonitorMOBILE WIFI MONITORMobile WiFi MonitorMOB1

Proteus Simulation of the Accident Alert: the Circuit

Accident detection Proteus simulation circuit: a Raspberry Pi 5, a SIM808 GSM GPS module, an MPU6050, a button, a buzzer and an LCD
Figure: The complete circuit of the accident detection system on an A3 Proteus sheet.
The connections of the accident detection system
SignalRaspberry Pi 5Goes to
TXD / RXDGPIO14 / GPIO15 (pins 8 / 10)the SIM808's RXD / TXD, crossed, 9600 baud
SDA / SCLGPIO2 / GPIO3 (pins 3 / 5)the MPU6050 (0x68) and the LCD (0x27) on one I2C bus
CANCELGPIO17 (pin 11)SW1 to 3.3 V, R1 10k to ground: HIGH = pressed
BUZGPIO18 (pin 12)the active buzzer
5V / 3V3pins 2 / 1the LCD / the button

The serial lines cross: the Pi's TXD goes to the module's RXD, and the module's TXD to the Pi's RXD. The MPU6050 and the LCD share the I2C bus at different addresses. The SIM808 has its own 4 V supply, because a GSM modem draws up to 2 A in short bursts when it transmits, far more than a Pi pin could give. Its POWERON property is AUTO, so it starts by itself without a pulse on its PWRKEY pin.

How the Accident Detection Works

Measuring the Tilt and the Jolt

An accelerometer at rest measures gravity: 1 g pointing down. When the vehicle is upright, all of that 1 g is on the sensor's Z axis. When it tips, gravity moves to the X and Y axes, and the angle between the Z axis and gravity is the tilt. The program takes it from the arc cosine of Z divided by the total. The total itself, the length of the acceleration vector, stays near 1 g while the vehicle drives normally, and jumps in a crash: a collision gives many g for a fraction of a second, while even hard braking gives less than 1 g. The MPU6050 is set to plus or minus 8 g so a crash does not clip, with its 44 Hz filter to calm the vibration of the road.

Two Kinds of Accident

A rollover is a tilt over 60 degrees that lasts 1 second. The second matters: a car on a steep ramp or a bike leaning into a corner can tilt for a moment, but a vehicle on its side or roof stays there. A crash is a total acceleration over 3 g, even for a single reading, because a collision is over in a fraction of a second. Both limits are at the top of the program, so you can tune them for a car, a bike or a truck.

The Countdown and the CANCEL Button

A false alarm sent to a family member in the middle of the night is frightening, and an emergency service will not trust a system that cries wolf. So the Pi does not text at once. It gives the driver 10 seconds: the buzzer beeps twice a second, a quarter of a second on and a quarter off, and the LCD shows ACCIDENT! with the seconds left and PRESS CANCEL. A press of the button, or CANCEL on the dashboard, stops it: the state is CANCELLED, and when the vehicle is upright again the program goes back to watching. A driver who is hurt or unconscious cannot press anything, and that is exactly when the message must go.

The SIM808: GSM and GPS on One UART

The Raspberry Pi 5 has one serial port on its header, and the SIM808 needs only one: the same AT command channel controls the GSM modem and the GPS. At the start, the program sends AT until the module answers, switches the echo off, sets text mode for SMS, asks to be told when a message arrives, and powers the GPS. Then it waits until the module is registered on the network. Every 5 seconds it asks for the position with AT+CGNSINF, whose answer has the fix, the time, the latitude, the longitude and the speed. To send a text it uses AT+CMGS with the number, waits for the module's prompt, sends the message and ends it with Ctrl+Z.

What the Message Says

The SMS is short on purpose: ROLLOVER! or CRASH! and a Google Maps link with the latitude and longitude, under 60 characters, so it can be read at a glance on a locked phone and a tap opens the map. If the GPS has no fix, for example in a tunnel, the message carries the last known position. The contact number is the emergency contact's; in the simulation, it is the number of the phone drawn on the SIM808's pop-up.

Asking Where the Vehicle Is

The module reports a new message with a +CMTI line. The program reads it with AT+CMGR, deletes it to keep the SIM card from filling up, and if the text says LOCATION or WHERE, answers with the position and the speed. This turns the alarm into a simple tracker: a parent can ask where the car is without waiting for an accident.

After an Alert

After the SMS, the system stays in ALERT SENT, so the state is not lost when the vehicle is put back on its wheels. RESET on the dashboard, or the CANCEL button held for 2 seconds, takes it back to NORMAL. If the SIM808 could not send, the state is ALERT FAILED, and the dashboard and the LCD say so.

The Python Code Explained

The program is accident_alert.py, written for the Raspberry Pi 5 with the lgpio library and pyserial. One thread talks to the SIM808, one watches the sensor, and the main thread serves the dashboard. The pins and the limits:

CANCEL_BTN, BUZZER = 17, 18                                        # BCM numbers
TILT_DEG, TILT_S, IMPACT_G, CANCEL_S = 60.0, 1.0, 3.0, 10.0

The MPU6050 at plus or minus 8 g, where 4096 counts are 1 g, and the tilt and the total from the three axes:

        lgpio.i2c_write_byte_data(self.h, 0x1C, 0x10)              # ACCEL_CONFIG: +-8 g (4096 LSB/g)
        return [(x - 65536 if x > 32767 else x) / 4096.0 for x in v]   # g
        g = math.sqrt(ax * ax + ay * ay + az * az)
        tilt = math.degrees(math.acos(max(-1.0, min(1.0, az / g)))) if g > 0.2 else 0.0

A rollover must last TILT_S; during the countdown the buzzer changes every quarter of a second, and the button or the dashboard cancels:

            elif tilted_at and now - tilted_at >= TILT_S:
            lgpio.gpio_write(chip, BUZZER, 1 if int(left * 4) % 2 == 0 else 0)
            if (btn and not pressed) or s['cancel_req']:

The SIM808 on the Pi's serial port, and the start-up: no echo, text mode, a note for each new SMS, the GPS on:

        self.port = serial.Serial('/dev/serial0', 9600, timeout=0.2)
    gsm.cmd('ATE0'); gsm.cmd('AT+CMGF=1'); gsm.cmd('AT+CNMI=2,1,0,0,0'); gsm.cmd('AT+CGNSPWR=1')

A position is used only when the GPS reports a fix; the SMS ends with Ctrl+Z (0x1A), and an incoming LOCATION is answered:

            if len(f) > 6 and f[1] == '1' and f[3] and f[4]:
        r = self.cmd(text + '\x1a', wait=('+CMGS', 'ERROR'), timeout=20, raw=True)
            if 'LOCATION' in text or 'WHERE' in text:

The dashboard's links are Refresh, CANCEL and RESET; /data gives the state, the tilt, the acceleration, the GPS, the counts, the last events and the map link as JSON.

How to Run the Proteus Simulation

  1. Download the project zip below and install the library files and the TEPPi folder (the steps are under the download button).
  2. Open Pi-5-Accident-Alert.pdsprj from the Proteus Simulation folder. Keep accident_alert.py and the DLL files next to it.
  3. Press Play: READY, then SIM808 READY after a few seconds, then the GPS fix on the LCD.
  4. Crash the vehicle: UPSIDE DOWN on the MPU6050's pop-up. Press the push button's pop-up within 10 s to cancel, FLAT to put it upright.
  5. Leave the next one to run out: the SIM808's phone receives the SMS. LOCATION on that phone asks where the vehicle is.
  6. The dashboard on the Laptop or the Mobile WiFi Monitor (press Go) has CANCEL and RESET.

Simulation Results: Three Rollovers

We pressed Play and turned the vehicle over three times with the MPU6050's pop-up. The first time the driver cancelled with the button, the second time nobody did and the SMS went out, and the third time the alarm was cancelled from the dashboard on the phone. In between, the contact's phone asked where the vehicle was. The times are the program's clock; the whole run took 91.6 simulated seconds.

Ready: on the Network, with a GPS Fix

The program printed READY - watching the tilt and the jolts at once. At 8.5 s the SIM808 had answered, taken the settings and registered: SIM808 READY - SMS text mode, the GPS on, on the network. Its pop-up showed the home network with a GOOD signal (CSQ 21, -71 dBm). The GPS needed 10.18 s for its first fix, and at 18.9 s the program had it: GPS FIX - 51.47785, -0.00149, Greenwich in London, the module's HOME place, with 6 of 12 satellites in use. The MPU6050 lay flat, 1.00 g on its Z axis, and the LCD showed NORMAL, 0 degrees of tilt, 1.0 g and the position.

Accident detection Proteus simulation ready: the SIM808 on the network with a GPS fix, the LCD showing NORMAL
Figure: Ready: on the network, a GPS fix.
SIM808 pop-up in the accident detection simulation: registered on the network, a 3D GPS fix with 6 satellites
Figure: The SIM808: GSM registered and a GPS fix.
MPU6050 pop-up in the accident detection simulation: the vehicle upright, 1.00 g on the Z axis
Figure: The MPU6050: upright.
I2C LCD 16x2 in the accident detection simulation: NORMAL, 0 degrees, 1.0 g and the GPS position
Figure: The LCD: NORMAL and the position.
Mobile WiFi Monitor showing the accident alert dashboard in the simulation: NORMAL, the GPS fix
Figure: The dashboard on the phone.

A Rollover, Cancelled by the Driver

We pressed UPSIDE DOWN on the MPU6050's pop-up: roll 180 degrees, -1.00 g on the Z axis. After the tilt had lasted a second, at 27.1 s, the program printed ACCIDENT - the vehicle overturned (tilted 180 deg): 10 s to CANCEL. The buzzer started to beep and the LCD counted down: ACCIDENT! and the seconds left, PRESS CANCEL. At 33.2 s we pressed the push button: CANCELLED - a false alarm (the button), the buzzer stopped and the LCD showed CANCELLED and FALSE ALARM. Nothing was sent. When we put the vehicle back on its wheels (FLAT), the program went back to watching by itself at 38.4 s: NORMAL - watching again (upright).

Accident detection Proteus simulation: a rollover, the LCD counting down and asking for CANCEL
Figure: The countdown.
I2C LCD in the accident detection simulation: ACCIDENT! 7 s, PRESS CANCEL
Figure: The LCD asks the driver to cancel.
MPU6050 pop-up in the accident detection simulation: the vehicle upside down, roll 180 degrees, -1.00 g
Figure: The MPU6050: upside down.
Accident detection Proteus simulation: CANCELLED with the push button, nothing sent
Figure: Cancelled with the button.
I2C LCD in the accident detection simulation: CANCELLED, FALSE ALARM, still tilted 180 degrees
Figure: The LCD: a false alarm.

A Rollover Nobody Cancels: the SMS

Then the real test. We turned the vehicle over again at 40.6 s and did not touch anything. The buzzer beeped for the full 10 seconds, 4.97 V and 29.2 mA on its pop-up, two beeps a second. When the countdown ran out, the program read the position once more and sent the message. At 52.7 s: SMS ALERT SENT to +15555550192 - maps.google.com/?q=51.47790,-0.00147. The phone drawn on the SIM808's pop-up received it: ROLLOVER! maps.google.com/?q=51.47790,-0.00147, a link that opens the map at the place of the accident. The LCD showed ALERT SENT, SMS 1, GPS, and the laptop's dashboard the same.

Accident detection Proteus simulation: a second rollover, the countdown running out with no cancel
Figure: The second rollover: no cancel.
Active buzzer pop-up in the accident detection simulation: sounding, 4.97 V, 29.2 mA, 2 beeps a second
Figure: The alarm beeping.
Accident detection Proteus simulation: the SMS alert sent, the LCD showing ALERT SENT
Figure: The SMS sent.
SIM808 pop-up in the accident detection simulation: the phone received ROLLOVER! with a Google Maps link
Figure: The SMS on the contact's phone.
I2C LCD in the accident detection simulation: ALERT SENT, SMS 1, GPS
Figure: The LCD: ALERT SENT.
Laptop WiFi Monitor showing the accident alert dashboard in the simulation: ALERT SENT and the map link
Figure: The dashboard: ALERT SENT.

Where Is the Vehicle?

Next we pressed LOCATION on the SIM808's phone, which texts the word LOCATION to the module. The module reported the new message, the program read it, deleted it and answered: at 67.3 s, SMS LOCATION from the phone - answered. The phone showed the reply, HERE with the map link and the speed, 0 km/h, because the vehicle was standing still. The terminal had every step so far.

SIM808 pop-up in the accident detection simulation: LOCATION sent from the phone and answered with the map link
Figure: LOCATION asked and answered.
Raspberry Pi 5 terminal in the accident detection simulation: start-up, two rollovers, the SMS and LOCATION
Figure: The terminal: the first steps.

Reset, and a Cancel from the Phone

With the vehicle upright again, RESET on the laptop's dashboard took the system back to NORMAL at 74.0 s. Then a third rollover, at 79.0 s, and this time we cancelled it from the phone's dashboard: the CANCEL link answered with the page already showing CANCELLED, and the terminal printed CANCELLED - a false alarm (the dashboard) at 79.7 s. Put back on its wheels, the vehicle was NORMAL again at 83.6 s, with 3 accidents, 2 cancelled and 1 SMS on the dashboard.

Laptop WiFi Monitor in the accident detection simulation: RESET from the dashboard, back to NORMAL
Figure: RESET from the laptop.
Mobile WiFi Monitor in the accident detection simulation: CANCEL pressed during the countdown, CANCELLED
Figure: CANCEL from the phone.
Accident detection Proteus simulation: the third rollover cancelled from the phone's dashboard
Figure: Cancelled from the dashboard.
Mobile WiFi Monitor in the accident detection simulation: NORMAL, 3 accidents, 2 cancelled, 1 SMS sent
Figure: The dashboard at the end.

One number deserves a note: the buzzer drew 29.2 mA. In the simulation the GPIO outputs drive Proteus's standard 5 V logic level; a real Pi pin gives 3.3 V, so the buzzer would draw less, but still more than a Pi pin should supply. On real hardware, drive the buzzer through a small transistor. Our first run also taught us something about the dashboard: the CANCEL page was built in the same moment the request arrived, before the sensor thread had acted, so it still said COUNTDOWN. The page now waits a few hundredths of a second for the new state, so the person who pressed CANCEL sees that it worked.

Raspberry Pi 5 terminal in the accident detection simulation: all three rollovers, the SMS, LOCATION and RESET
Figure: The terminal: every step.

Download the Project Files

Click the button below to download Accident-Detection-Raspberry-Pi-GPS-GSM-Proteus-Simulation-v1.0.zip (about 15.2 MB, without the source code of the models):

Accident Detection with Raspberry Pi, GPS and GSM - Proteus Simulation V1.0
  • Proteus Simulation: Pi-5-Accident-Alert.pdsprj, accident_alert.py and copies of the models.
  • Python Code: accident_alert.py, the program.
  • Proteus Library Files: the LIB files of every part.
  • Proteus Model Files: the DLL models and the TEPPi folder (the Pi's Python runtime).
  • README.txt: the installation steps, the properties, the test results and the links to every library post.

No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.

How to Install the Project Files

  1. Close Proteus and extract the whole zip file, for example to your Desktop.
  2. Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
  3. Copy everything in Proteus Model Files, the DLL files and the TEPPi folder, into the MODELS folder.
  4. Start Proteus and open the project from the Proteus Simulation folder.

Changing the Code

accident_alert.py is a plain Python file: open it in any editor, put your own contact number in CONTACT, change the tilt, the impact limit or the countdown, save it next to the project and press Play again. The same file runs on a real Raspberry Pi 5 with lgpio and pyserial; enable the serial port and disable the serial console in raspi-config first.

Ideas to Extend the Project

  • Text two or three contacts, and call the first one with ATD so the phone rings as well.
  • Read the speed from the GPS just before the crash and put it in the message.
  • A free fall check (the total near 0 g) for a motorbike that leaves the road.
  • Upload each alert to a web server over GPRS, with the SIM808's AT+HTTP commands.
  • An ignition input so the system only arms while the engine runs.
  • A camera that saves a picture at the moment of the crash.

From Simulation to Real Hardware

  • The SIM808's power: 3.4 to 4.4 V and up to 2 A in bursts; a good buck converter from the battery and a large capacitor near the module.
  • The antennas: the GSM and the GPS antennas both need a view out of the vehicle; the GPS needs the sky for its first fix.
  • Mounting: fix the MPU6050 firmly, flat and level, so upright really reads 0 degrees and road vibration does not shake it loose.
  • The buzzer: a buzzer draws about 30 mA, more than a Pi pin should give; drive it through a transistor.
  • Not a certified system: commercial eCall systems are tested to strict standards; this project is for learning and prototypes.

Troubleshooting

  • SIM808 READY never comes: the TXD and RXD wires must cross, and both sides must use 9600 baud.
  • No SMS: the module must be registered on the network; the terminal shows each step and the LCD the state.
  • Every bump is an accident: raise IMPACT_G, or TILT_DEG and TILT_S, at the top of the program.
  • No dashboard: the program serves on port 8000 once the Ethernet is up; press Go again after a few seconds.

Limitations of the Simulation

  • The SIM808, the MPU6050, the button, the buzzer and the LCD are our libraries' models, built from their datasheets and the parts' behaviour, not every detail of real parts.
  • The network, the SIM card and the receiving phone are part of the SIM808 model; no real message leaves your PC, and the GPS position is the model's chosen place.
  • The MPU6050's pop-up gives fixed poses such as FLAT and UPSIDE DOWN; a real crash is a short, messy pulse that you can test on the bench by dropping the sensor onto foam.
  • Raspberry Pi is a trademark of Raspberry Pi Ltd; this project is not made or endorsed by them or by Labcenter.

So, that was all about the Accident Detection with Raspberry Pi, GPS and GSM - Proteus Simulation. I hope it helps you understand how an accelerometer sees a rollover and a crash, why a short countdown with a cancel button makes an alarm trustworthy, and how a SIM808 sends the position over SMS with a handful of AT commands, all tested in a simulation before you build it. More popular projects with the ESP32 and the Raspberry Pi 5 are coming. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!


Comments

0

Join the conversation