fall detection (copy) — 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_GFX.h>
#include <Adafruit_SSD1306.h>
#include <math.h>

// =====================================================
// PIN DEFINITIONS
// =====================================================

#define SDA_PIN 9
#define SCL_PIN 8

#define BUZZER_PIN 16

#define SOS_BUTTON_PIN 17
#define FALL_BUTTON_PIN 18

// =====================================================
// I2C ADDRESSES
// =====================================================

#define MPU6050_ADDR 0x68
#define OLED_ADDR    0x3C

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

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

// =====================================================
// MPU6050 REGISTERS
// =====================================================

#define PWR_MGMT_1   0x6B
#define ACCEL_CONFIG 0x1C
#define GYRO_CONFIG  0x1B
#define ACCEL_XOUT_H 0x3B

// =====================================================
// FALL DETECTION THRESHOLDS
// =====================================================

// Acceleration in g
#define FALL_ACCEL_THRESHOLD 2.0

// Gyroscope in deg/s
#define FALL_GYRO_THRESHOLD 200.0

// =====================================================
// MPU6050 VARIABLES
// =====================================================

int16_t accelX;
int16_t accelY;
int16_t accelZ;

int16_t gyroX;
int16_t gyroY;
int16_t gyroZ;

float ax;
float ay;
float az;

float gx;
float gy;
float gz;

float totalAcceleration;
float totalGyro;

// =====================================================
// I2C WRITE
// =====================================================

void writeMPU(byte reg, byte value)
{
  Wire.beginTransmission(MPU6050_ADDR);

  Wire.write(reg);
  Wire.write(value);

  Wire.endTransmission();
}

// =====================================================
// I2C READ
// =====================================================

void readMPU(byte reg, byte *buffer, byte length)
{
  Wire.beginTransmission(MPU6050_ADDR);

  Wire.write(reg);

  Wire.endTransmission(false);

  Wire.requestFrom(
    MPU6050_ADDR,
    length
  );

  for (byte i = 0; i < length; i++)
  {
    if (Wire.available())
    {
      buffer[i] = Wire.read();
    }
    else
    {
      buffer[i] = 0;
    }
  }
}

// =====================================================
// INITIALIZE MPU6050
// =====================================================

void initializeMPU()
{
  Serial.println("Initializing MPU6050...");

  // Wake sensor
  writeMPU(PWR_MGMT_1, 0x00);

  delay(100);

  // Accelerometer ±8g
  writeMPU(ACCEL_CONFIG, 0x10);

  // Gyroscope ±500 deg/s
  writeMPU(GYRO_CONFIG, 0x08);

  delay(100);

  Serial.println("MPU6050 READY!");
}

// =====================================================
// READ MPU6050
// =====================================================

void readMPU6050()
{
  byte data[14];

  readMPU(
    ACCEL_

More projects by 2023beec071