Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino Mega 2560 Emergency Alert System with Panic Button and Vital Monitoring

Image of Arduino Mega 2560 Emergency Alert System with Panic Button and Vital Monitoring

Circuit Documentation

Summary

This circuit is designed to monitor vital signs and handle emergency situations. It includes an Arduino Mega 2560 microcontroller, a green button for emergency alerts, and a loudspeaker for audio notifications. The Arduino is programmed to read various sensors, display data on an OLED screen, and send alerts via GSM.

Component List

Green Button

  • Description: A push-button switch used to trigger emergency alerts.
  • Pins: OUT, VCC, GND

Loudspeaker

  • Description: A device that produces sound, used for audio notifications.
  • Pins: pin1, pin2

Arduino Mega 2560

  • Description: A microcontroller board based on the ATmega2560, used to control the circuit and process sensor data.
  • Pins: IOREF, RESET, 3V3, 5V, GND, VIN, A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, D21/SCL, D20/SDA, D19/RX1, D18/TX1, D17 PWM/RX2, D16 PWM/TX2, D15/RX3, D14/TX3, D0 RX0, D1 TX0, D2 PWM, D3 PWM, D4 PWM, D5 PWM, D6 PWM, D7 PWM, D8 PWM, D9 PWM, D10 PWM, D11 PWM, D12 PWM, D13 PWM, AREF, SDA, SCL, D52, D50, D48, D46, D44, D42, D40, D38, D36, D34, D32, D30, D28, D26, D24, D22, D53, D51, D49, D47, D45, D43, D41, D39, D37, D35, D33, D31, D29, D27, D25, D23, 6swdw

Wiring Details

Green Button

  • VCC: Connected to 5V on Arduino Mega 2560
  • GND: Connected to GND on Arduino Mega 2560
  • OUT: Connected to D4 PWM on Arduino Mega 2560

Loudspeaker

  • pin1: Not connected in the provided net list
  • pin2: Not connected in the provided net list

Arduino Mega 2560

  • 5V: Connected to VCC on Green Button
  • GND: Connected to GND on Green Button
  • D4 PWM: Connected to OUT on Green Button

Code Documentation

#include <Adafruit_MAX31855.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_MAX30105.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <MPU6050.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>

// OLED Display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);

// MAX30102
Adafruit_MAX30105 max30102;

// DS18B20
OneWire oneWire(2);
DallasTemperature tempSensor(&oneWire);

// MPU6050
MPU6050 mpu;

// GSM Module
SoftwareSerial gsmSerial(12, 13); // GSM TX, RX
#define GUARDIAN_NUMBER "+1234567890"
#define HOSPITAL_NUMBER "+0987654321"

// ECG Sensor
#define ECG_PIN A0
#define LEAD_OFF_PLUS_PIN 7
#define LEAD_OFF_MINUS_PIN 8

// Emergency Button
#define PANIC_BUTTON_PIN 4

// GPS Data
String latitude = "N/A";
String longitude = "N/A";

// DFPlayer Mini
SoftwareSerial dfSerial(10, 11); // DFPlayer RX, TX
DFRobotDFPlayerMini dfPlayer;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  
  // OLED Initialization
  if (!display.begin(SSD1306_I2C_ADDRESS, 0x3C)) {
    Serial.println("OLED initialization failed.");
    for (;;);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);

  // Initialize MAX30102
  if (!max30102.begin()) {
    Serial.println("MAX30102 not detected.");
    for (;;);
  }

  // Initialize DS18B20
  tempSensor.begin();

  // Initialize MPU6050
  mpu.initialize();

  // Initialize ECG Sensor
  pinMode(LEAD_OFF_PLUS_PIN, INPUT);
  pinMode(LEAD_OFF_MINUS_PIN, INPUT);

  // Initialize Emergency Button
  pinMode(PANIC_BUTTON_PIN, INPUT_PULLUP);

  // Initialize GSM
  gsmSerial.begin(9600);

  // Initialize DFPlayer Mini
  dfSerial.begin(9600);
  if (!dfPlayer.begin(dfSerial)) {
    Serial.println("DFPlayer initialization failed!");
    for (;;);
  }
  dfPlayer.volume(25); // Set volume level
}

void loop() {
  monitorVitals();

  if (digitalRead(PANIC_BUTTON_PIN) == LOW) {
    handleEmergencyButton();
  }
}

void monitorVitals() {
  float temperature = readTemperature();
  float heartRate = readHeartRate();
  float bloodPressure = calculateBloodPressure(heartRate);
  float spo2 = readSpO2();
  int ecgValue = readECG();

  displayData(temperature, heartRate, bloodPressure, spo2, ecgValue);
  sendToBlynk(temperature, heartRate, bloodPressure, spo2, ecgValue);
  
  if (isAbnormal(temperature, heartRate, bloodPressure, spo2)) {
    fetchGPSLocation();
    alertGuardianAndHospital(temperature, heartRate, bloodPressure, spo2, ecgValue);
    playVoiceAlert();
  }
}

float readTemperature() {
  tempSensor.requestTemperatures();
  return tempSensor.getTempCByIndex(0);
}

float readHeartRate() {
  // Example: Replace with actual MAX30102 processing logic
  return random(60, 100);
}

float calculateBloodPressure(float heartRate) {
  return 120 + (heartRate - 60) * 0.5;
}

float readSpO2() {
  return random(95, 100);
}

int readECG() {
  if (digitalRead(LEAD_OFF_PLUS_PIN) == HIGH || digitalRead(LEAD_OFF_MINUS_PIN) == HIGH) {
    return 0; // Lead off detected
  }
  return analogRead(ECG_PIN);
}

void displayData(float temp, float hr, float bp, float spo2, int ecg) {
  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Temp: ");
  display.print(temp);
  display.println(" C");

  display.print("Heart Rate: ");
  display.print(hr);
  display.println(" BPM");

  display.print("Blood Pressure: ");
  display.print(bp);
  display.println(" mmHg");

  display.print("SpO2: ");
  display.print(spo2);
  display.println(" %");

  display.print("ECG: ");
  display.print(ecg);
  display.println(" mV");

  display.display();
}

void sendToBlynk(float temp, float hr, float bp, float spo2, int ecg) {
  // Replace with actual Blynk code for Arduino Mega 2560
}

bool isAbnormal(float temp, float hr, float bp, float spo2) {
  return temp > 37.5 || hr < 60 || hr > 100 || bp > 140 || spo2 < 95;
}

void fetchGPSLocation() {
  gsmSerial.println("AT+CGPSINFO");
  delay(2000);
  while (gsmSerial.available()) {
    String gpsData = gsmSerial.readString();
    if (gpsData.indexOf("+CGPSINFO") != -1) {
      int comma1 = gpsData.indexOf(",");
      latitude = gpsData.substring(10, comma1);
      int comma2 = gpsData.indexOf(",", comma1 + 1);
      longitude = gpsData.substring(comma1 + 1, comma2);
    }
  }
}

void alertGuardianAndHospital(float temp, float hr, float bp, float spo2, int ecg) {
  String message = "EMERGENCY ALERT\n";
  message += "Vitals:\n";
  message += "Temp: " + String(temp) + "C\n";
  message += "HR: " + String(hr) + "BPM\n";
  message += "BP: " + String(bp) + "mmHg\n";
  message += "SpO2: " + String(spo2) + "%\n";
  message += "ECG: " + String(ecg) + "mV\n";
  message += "Location: https://maps.google.com/?q=" + latitude + "," + longitude;

  sendSMS(GUARDIAN_NUMBER, message);
  sendSMS(HOSPITAL_NUMBER, message);
}

void handleEmergencyButton() {
  fetchGPSLocation();
  alertGuardianAndHospital(0, 0,