fall detection — ESP32-S3 simulation

An interactive ESP32-S3 circuit simulation you can run free in your browser on Velxio, by 2023beec071.

Components

Sketch code

#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <math.h>

// ================= PIN CONFIG =================

#define SDA_PIN 9
#define SCL_PIN 8

#define BUZZER_PIN 16
#define SOS_BUTTON_PIN 17
#define FALL_BUTTON_PIN 18

// ================= OLED =================

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

// ================= FALL SETTINGS =================

// Wokwi testing threshold
#define ACCEL_THRESHOLD 1.5
#define GYRO_THRESHOLD 2.0

// ================= OBJECTS =================

Adafruit_MPU6050 mpu;

Adafruit_SSD1306 display(
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  &Wire,
  -1
);

// =================================================
// SETUP
// =================================================

void setup() {

  Serial.begin(115200);

  Wire.begin(SDA_PIN, SCL_PIN);

  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);

  pinMode(SOS_BUTTON_PIN, INPUT_PULLUP);
  pinMode(FALL_BUTTON_PIN, INPUT_PULLUP);

  // MPU6050
  if (!mpu.begin()) {

    Serial.println("MPU6050 NOT FOUND!");

    while (1) {
      delay(100);
    }
  }

  Serial.println("MPU6050 CONNECTED!");

  // OLED
  if (!display.begin(
        SSD1306_SWITCHCAPVCC,
        0x3C
      )) {

    Serial.println("OLED NOT FOUND!");

    while (1) {
      delay(100);
    }
  }

  Serial.println("OLED CONNECTED!");

  display.clearDisplay();

  display.setTextColor(SSD1306_WHITE);

  display.setTextSize(2);
  display.setCursor(12, 5);
  display.println("WRISTBAND");

  display.setTextSize(1);
  display.setCursor(25, 35);
  display.println("SYSTEM READY");

  display.display();

  delay(2000);
}

// =================================================
// ALERT FUNCTION
// =================================================

void showAlert(
  const char* title,
  const char* message,
  const char* footer
) {

  digitalWrite(BUZZER_PIN, HIGH);

  display.clearDisplay();

  display.setTextColor(SSD1306_WHITE);

  display.setTextSize(2);

  display.setCursor(10, 5);
  display.println(title);

  display.setCursor(10, 30);
  display.println(message);

  display.setTextSize(1);

  display.setCursor(20, 52);
  display.println(footer);

  display.display();

  delay(3000);

  digitalWrite(BUZZER_PIN, LOW);

  delay(300);
}

// =================================================
// LOOP
// =================================================

void loop() {

  // =================================================
  // SOS BUTTON
  // =================================================

  if (digitalRead(SOS_BUTTON_PIN) == LOW) {

    Serial.println("!!! SOS EMERGENCY !!!");

    showAlert(
      "SOS",
      "EMERGENCY!",
      "HELP NEEDED"
    );

    while (digitalRead(SOS_BUTTON_PIN) == LOW) {
      delay(20);
    }

    return;
  }

  // =================================================
  // MANUAL FALL BUTTON
  // =================================================

  if (digitalRead(FALL_BUTTON

More projects by 2023beec071