A real-time fall detection system built for the STM32L4S5-IoT01A Discovery Board (B-L4S5I-IOT01) using FreeRTOS, accelerometer/gyroscope sensors, and cloud connectivity.
- Overview
- Features
- System Architecture
- Hardware Requirements
- Software Components
- Configuration
- Usage
- Fall Detection Algorithm
- Cloud Infrastructure
- Project Structure
- Authors
This project implements a fall detection system that monitors accelerometer and gyroscope data to detect fall events in real-time. When a fall is detected, the system:
- Activates visual alerts (LED blinking)
- Triggers audio alerts (buzzer)
- Sends data to cloud via WiFi for remote monitoring
- Provides real-time visualization through web dashboard
- Real-time sensor monitoring at 50 Hz sampling rate
- Multi-sensor fusion: LSM6DSL accelerometer and gyroscope
- WiFi connectivity via Inventek ES-WIFI module
- Visual feedback: LED2 blinks at different rates (500ms normal, 50ms on fall)
- Audio alerts: Buzzer with customizable patterns
- User reset: Push button to clear fall detection state
- FreeRTOS multi-tasking architecture with 6 concurrent tasks
- Optimized signal processing: Assembly-language moving average filter
- State machine-based fall detection with configurable thresholds
- Cloud data transmission via HTTP POST
- Live monitoring dashboard with real-time charts
- Local proxy server for testing without cloud dependency
-
SensorTask (Priority: 3 - Highest)
- Samples accelerometer and gyroscope at 50 Hz
- Applies 4-point moving average filter (Assembly optimized)
- Publishes sensor data to queue
-
DetectionTask (Priority: 2)
- Implements fall detection state machine
- Monitors for low-g followed by high-g and gyroscope spike
- Updates fall_detected flag
- Distributes detection results to WiFi and UART tasks
-
BlinkTask (Priority: 1)
- Controls LED2 blink rate based on fall state
- 500ms period: Normal operation
- 50ms period: Fall detected
-
BuzzerTask (Priority: 1)
- Monitors fall detection flag
- Plays alert patterns when fall detected
- Provides audio feedback
-
WifiTask (Priority: 1)
- Maintains WiFi connection
- Sends fall detection data to cloud via HTTP POST
- Handles connection errors and retries
-
UartTask (Priority: 1)
- Outputs diagnostic information via UART1
- Logs sensor readings and detection events
- Board: STM32L4S5-IoT01A Discovery Kit (B-L4S5I-IOT01)
- Microcontroller: STM32L4S5VIT6 (ARM Cortex-M4)
- Sensors:
- LSM6DSL 3D accelerometer and gyroscope
- WiFi Module: Inventek ISM43362-M3G-L44
- Peripherals:
- User button (On board)
- LED2 (On board)
- Buzzer (PWM-controlled)
- UART: UART1 for logging (115200 baud)
main.c: System initialization and main entry pointfreertos.c: Task definitions and RTOS configurationfall_cloud.c: WiFi connectivity and HTTP clientbuzzer.c: Buzzer control functionsmov_avg.s: Assembly-optimized moving average filterfall_detect.h: Fall detection thresholds and constants
- Platform: Vercel (serverless)
- Runtime: Node.js
- API: REST endpoint at
/api/data - Storage: In-memory (global variable)
- Authentication: Simple API key (X-Ingest-Key header)
- Framework: Express.js
- Port: 3000
- Features:
- In-memory circular buffer (500 samples)
- Live charting with Chart.js
- Optional forwarding to Vercel
- No-cache headers for real-time updates
Edit CG2028_Assignment/Core/Inc/wifi_config.h:
#define WIFI_SSID "your_ssid"
#define WIFI_PASS "your_password"
#define WIFI_SECURITY ES_WIFI_SEC_WPA2
#define SERVER_IP0 3 // AWS server IP
#define SERVER_IP1 107
#define SERVER_IP2 208
#define SERVER_IP3 198Edit CG2028_Assignment/Core/Inc/fall_detect.h:
#define LOW_G 6.0f // Low-g threshold (m/s²)
#define HIGH_G 18.0f // High-g threshold (m/s²)
#define GYRO_SPIKE 1800.0f // Gyroscope spike (dps)
#define MAX_GAP 1000 // Maximum time gap (ms)Edit CG2028_Assignment/Core/Src/fall_cloud.c:
#define SERVER_PORT 3000
#define API_PATH "/api/data"
#define INGEST_KEY "devkey"
#define SERVER_HOST_STR "3.107.208.198" // Your server IPEdit fall-proxy/proxy.js:
const PORT = 3000;
const MAX_SAMPLES = 500;
const FORWARD_TO_VERCEL = true;
const VERCEL_INGEST_URL = "https://fall-cloud.vercel.app/api/data";
const VERCEL_INGEST_KEY = "devkey";-
Power on the board and ensure WiFi is available
-
Monitor UART output (115200 baud) for debug logs:
Blink Task Sensor Task Uart Task WIFI START wifi: RegisterBusIO wifi: Init wifi: Connect ... -
Observe LED behavior:
- Slow blink (500ms): Normal operation
- Fast blink (50ms): Fall detected
-
View live data at
http://localhost:3000or your Vercel URL -
Reset fall state: Press the blue user button
- LED blinks every 500ms
- Sensor data streams to UART
- Data posted to cloud every detection cycle
- LED blinks rapidly (50ms)
- Buzzer sounds alert pattern
- Fall flag sent to cloud immediately
- UART prints sample stream in the format:
t_ms, acc_mag, gyro_mag Fall
- Fall state cleared
- LED returns to slow blink
- Buzzer stops
- Reset message sent to cloud
Connect via serial terminal (PuTTY, minicom, screen, Tera Term)
- Proxy server: Real-time chart of acc_mag and gyro_mag
- Vercel dashboard: Simple status display
- Auto-refreshes every 500ms-1000ms
The system uses a two-stage state machine:
if (acc_mag < LOW_G):
enter_lowg_state()
start_timer()
if (saw_lowg AND time_since_lowg < MAX_GAP):
if (acc_mag > HIGH_G AND gyro_mag > GYRO_SPIKE):
fall_detected = true
- LOW_G: 6.0 m/s² (≈0.61g) - free fall threshold
- HIGH_G: 18.0 m/s² (≈1.84g) - impact threshold
- GYRO_SPIKE: 1800 dps - rotational velocity threshold
- MAX_GAP: 1000 ms - maximum time between stages
- Raw sensor data acquired at 50 Hz
- 4-point moving average filter (Assembly optimized)
- Magnitude calculation:
sqrt(x² + y² + z²) - Unit conversion to m/s² and dps
The moving average filter is implemented in ARM assembly for efficiency:
mov_avg:
PUSH {r2-r4, lr}
MOV r2, #0 @ sum = 0
MOV r3, #0 @ i = 0
loop:
CMP r3, r0 @ i >= N?
BGE end
LDR r4, [r1], #4 @ load and increment
ADD r2, r2, r4 @ accumulate
ADD r3, r3, #1 @ i++
B loop
end:
ASR r0, r2, #2 @ divide by 4 (shift right 2)
POP {r2-r4, pc}-
POST /api/data (from STM32)
- Headers:
X-Ingest-Key: devkey - Body: JSON
{ "t_ms": 123456, "acc_mag": 9.8, "gyro_mag": 45.2, "fall": 0 }
- Headers:
-
GET /api/data (dashboard polling)
- Response:
{ "latest": { "t_ms": 123456, "acc_mag": 9.8, "gyro_mag": 45.2, "fall": 0 } }
- Response:
Assignment/
├── CG2028_Assignment/ # Main embedded project
│ ├── Core/
│ │ ├── Inc/ # Header files
│ │ │ ├── main.h
│ │ │ ├── fall_detect.h # Detection thresholds
│ │ │ ├── fall_cloud.h # WiFi/HTTP API
│ │ │ ├── buzzer.h # Buzzer control
│ │ │ ├── wifi_config.h # WiFi credentials
│ │ │ └── FreeRTOSConfig.h
│ │ ├── Src/ # Source files
│ │ │ ├── main.c # Entry point, init
│ │ │ ├── freertos.c # Task implementations
│ │ │ ├── fall_cloud.c # WiFi & HTTP client
│ │ │ ├── buzzer.c # Buzzer driver
│ │ │ ├── mov_avg.s # Assembly filter
│ │ │ └── stm32l4xx_*.c # HAL/BSP
│ │ └── Startup/
│ │ └── startup_stm32l4s5vitx.s
│ ├── Drivers/ # STM32 HAL & BSP
│ │ ├── BSP/
│ │ │ └── B-L4S5I-IOT01/ # Board support
│ │ ├── CMSIS/ # ARM CMSIS
│ │ └── STM32L4xx_HAL_Driver/
│ ├── Middlewares/
│ │ └── Third_Party/
│ │ └── FreeRTOS/ # Real-time OS
│ ├── fall-server/ # Legacy/local server prototype
│ │ ├── package.json
│ │ └── server.js
│ ├── Debug/ # Build output
│ └── *.ld # Linker scripts
│
├── CG2028_Assignment_Test/ # Test project (moving avg validation)
│ └── (similar structure)
│
├── fall-cloud/ # Vercel serverless backend
│ ├── api/
│ │ └── data.js # Serverless function
│ ├── public/
│ │ └── index.html # Simple dashboard
│ ├── package.json
│ └── vercel.json # Vercel config
│
├── fall-proxy/ # Local Express proxy/dashboard
│ ├── proxy.js # Server with live charts
│ └── package.json
│
├── fall-dashboard/ # (Reserved for future expansion)
│
├── Diagram/ # PlantUML system diagrams
│ ├── system_arch.puml # Architecture diagram
│ ├── fall_detection.puml # Detection algorithm
│ ├── interrupt.puml # ISR flow
│ └── real_time.puml # Real-time constraints
│
└── README.md # This file
CG2028 Assignment, Semester 2, AY 2025/26
- Foo Kang
- Yeo Teck Ian Bryan
Course: CG2028 - Computer Organization and Architecture
Institution: National University of Singapore (NUS)
