SVSEmbedded will do new innovative thoughts. Any latest idea will comes we will take that idea & implement that idea in a few days. We always encourage the students to take good ideas/projects. SVSEmbedded providing latest innovative electronics projects to B.E/B.Tech/M.E/M.Tech students. We developed thousands of projects for engineering student to develop their skills in electrical and electronics
Thursday, 28 May 2026
AI-Powered Driver Drowsiness Detection and Accident Prevention System
๐ AI-Powered Driver Drowsiness Detection & Accident Prevention System
(ESP32 + IoT + n8n + Telegram + Google Sheets + ThingSpeak + AI Agentic Workflow)
Below is a complete, structured, step-by-step documentation for your project:
๐ AI-Powered Driver Drowsiness Detection & Accident Prevention System
(ESP32 + IoT + n8n + Telegram + Google Sheets + ThingSpeak + AI Agentic Workflow)
1. ๐ Project Overview
This system detects driver drowsiness in real time using AI and sensors and triggers instant multi-channel alerts using:
๐ฑ Telegram (text + voice alerts)
๐ Google Sheets (logging)
☁️ ThingSpeak (IoT dashboard)
๐ n8n automation workflows (AI agentic orchestration)
๐ค ESP32 microcontroller (edge device)
๐ฏ Goal:
Prevent road accidents by detecting:
Eye closure (PERCLOS method or IR sensor)
Head tilt / nodding
Sudden inactivity / fatigue patterns
2. ๐งฐ Components List
Hardware
ESP32 Dev Board
IR Eye Blink Sensor / Camera module (OV2640 optional)
MPU6050 (Accelerometer + Gyroscope)
Buzzer (Alarm)
Vibration motor (optional)
OLED Display (optional)
Power bank / 5V supply
Software / Cloud
n8n Automation Server (self-hosted or cloud)
Telegram Bot API
Google Sheets API
ThingSpeak IoT Cloud
Python (optional AI processing)
Arduino IDE / PlatformIO
3. ⚡ System Architecture
Sensors (Eye + Head movement)
↓
ESP32 (Edge Processing)
↓ WiFi
n8n Webhook Trigger
↓
┌───────────────┬────────────────┬─────────────────┐
│ Telegram Bot │ Google Sheets │ ThingSpeak │
│ Voice Alerts │ Data Logging │ Dashboard │
└───────────────┴────────────────┴─────────────────┘
↓
AI Agent (n8n / Python)
↓
Risk Score + Alert Decision
4. ๐ Flowchart (System Logic)
START
↓
Read Eye Blink + Head Movement
↓
Compute Drowsiness Score
↓
Is Score > Threshold?
├── NO → Continue Monitoring
└── YES →
↓
Trigger ESP32 Alarm
↓
Send data to n8n webhook
↓
AI Agent evaluates risk
↓
Send:
→ Telegram message + voice alert
→ Google Sheets log entry
→ ThingSpeak update
END LOOP
5. ๐ Circuit Schematic (Connections)
ESP32 Pin Mapping
IR Eye Sensor
VCC → 3.3V
GND → GND
OUT → GPIO 34
MPU6050
VCC → 3.3V
GND → GND
SDA → GPIO 21
SCL → GPIO 22
Buzzer
→ GPIO 25
→ GND
Vibration Motor (optional)
GPIO 26 → transistor → motor
6. ๐ป ESP32 Source Code (Arduino)
#include
#include
#include
#define EYE_SENSOR 34
#define BUZZER 25
const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASS";
String n8n_url = "https://your-n8n-webhook-url";
void setup() {
Serial.begin(115200);
pinMode(EYE_SENSOR, INPUT);
pinMode(BUZZER, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void loop() {
int eyeValue = analogRead(EYE_SENSOR);
Serial.println(eyeValue);
if (eyeValue < 1000) { // drowsy threshold example
digitalWrite(BUZZER, HIGH);
sendToN8N(eyeValue);
} else {
digitalWrite(BUZZER, LOW);
}
delay(500);
}
void sendToN8N(int value) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(n8n_url);
http.addHeader("Content-Type", "application/json");
String payload = "{\"eye_status\":" + String(value) + "}";
http.POST(payload);
http.end();
}
}
7. ๐ n8n Workflow (JSON)
Workflow Steps:
Webhook trigger
AI Function node
Telegram node
Google Sheets node
HTTP request to ThingSpeak
{
"nodes": [
{
"name": "Webhook",
"type": "n8n-nodes-base.webhook"
},
{
"name": "AI Risk Analyzer",
"type": "n8n-nodes-base.function",
"parameters": {
"functionCode": "const val = $json.eye_status;\nreturn [{ risk: val < 1000 ? 'HIGH' : 'LOW' }];"
}
}
]
}
(Full n8n workflows can be extended with drag-and-drop UI)
8. ๐ค Telegram Bot Setup
Step 1: Create Bot
Open Telegram → search: BotFather
Send: /newbot
Get API token
Step 2: Get Chat ID
Search: @userinfobot
Copy chat ID
Step 3: n8n Telegram Node
Bot Token → paste API key
Chat ID → your ID
Message Example:
๐จ Drowsiness Alert!
Driver fatigue detected.
Immediate attention required.
9. ๐ Google Sheets Integration
Steps:
Open Google Sheets
Create columns:
Time | Eye Value | Risk Level | Action
In Google Cloud:
Enable Google Sheets API
Create Service Account
Share sheet with service email
In n8n:
Add “Google Sheets Node”
Append row on trigger
10. ☁️ ThingSpeak Setup
Step 1:
Create channel:
Field1 → Eye value
Field2 → Risk level
Step 2:
API Key from:
Channel → API Keys
ESP32 / n8n send:
https://api.thingspeak.com/update?api_key=XXXX&field1=value
11. ๐ง AI Drowsiness Detection Logic
Method 1 (Simple Threshold)
Eye sensor value < threshold → drowsy
Method 2 (Better AI Model)
Train ML model using:
Eye blink rate
Eye closure duration
Head tilt angle
Example Model:
Logistic Regression
Random Forest
Lightweight TensorFlow Lite (ESP32-compatible)
12. ๐ Voice Notification System
Method:
Use Telegram voice message or TTS API
Option A (n8n + TTS API)
Convert text → speech file
Send as audio in Telegram
Option B:
Telegram voice message node
13. ⚙️ Automation Logic (n8n AI Agent Style)
IF risk = HIGH:
→ Send Telegram alert
→ Trigger buzzer via ESP32 webhook
→ Log to Google Sheets
→ Update ThingSpeak
→ Send voice alert
ELSE:
→ Only log data
14. ๐ Future Enhancements
Advanced AI:
CNN-based eye detection (camera module)
Real-time face landmark detection (MediaPipe)
Vehicle Integration:
CAN bus integration
Automatic braking system trigger
Cloud AI:
AWS IoT Core integration
Azure Digital Twin model
Smart Features:
Driver identity recognition
Fatigue prediction over time
Accident prediction model
15. ๐ฆ Deployment Guide
Step 1
Upload ESP32 code
Step 2
Deploy n8n server (Docker recommended)
Step 3
Connect Telegram bot
Step 4
Enable Google Sheets API
Step 5
Create ThingSpeak dashboard
Step 6
Test full pipeline:
Simulate eye closure
Check alerts flow
16. ๐งช Testing Checklist
ESP32 connects to WiFi ✔
Eye sensor reading changes ✔
Buzzer triggers ✔
n8n receives webhook ✔
Telegram alert sent ✔
Google Sheets logs ✔
ThingSpeak updates ✔
17. ๐ Final System Summary
Feature Technology
Edge device ESP32
Detection IR / ML / MPU6050
Automation n8n
Messaging Telegram Bot
Data logging Google Sheets
Cloud IoT ThingSpeak
AI logic Rule-based / ML
If you want, I can also next provide:
✅ 1. ๐งฉ Full Circuit Diagram (Text + Image Description)
๐ Final Hardware Architecture
┌────────────────────┐
│ ESP32 DevKit │
└─────────┬──────────┘
│
┌────────────────────┼────────────────────┐
│ │ │
┌────▼────┐ ┌──────▼──────┐ ┌─────▼─────┐
│ IR Eye │ │ MPU6050 │ │ Buzzer │
│ Sensor │ │ Gyro+Accel │ │ Alert │
└────┬────┘ └──────┬──────┘ └─────┬─────┘
│ │ │
GPIO34 GPIO21/22 GPIO25
WiFi (Internet)
│
▼
┌──────────────────────────┐
│ n8n Server │
└─────────┬────────────────┘
│
┌──────────────┼───────────────┐
▼ ▼ ▼
Telegram Bot Google Sheets ThingSpeak
(Alert + Voice) Logging Dashboard
๐ผ️ If you want real diagram image:
I can generate a clean PNG/SVG circuit diagram (professional Fritzing style).
Just say:
๐ “Make PCB-style diagram”
✅ 2. ๐ n8n Workflow (READY IMPORT JSON)
Save this as:
drowsiness_n8n_workflow.json
๐ฅ FULL IMPORTABLE WORKFLOW
{
"name": "Driver Drowsiness Detection System",
"nodes": [
{
"parameters": {
"path": "drowsiness",
"method": "POST"
},
"name": "Webhook",
"type": "n8n-nodes-base.webhook",
"typeVersion": 1,
"position": [250, 300]
},
{
"parameters": {
"functionCode": "const eye = $json.eye_status;\nconst risk = eye < 1000 ? 'HIGH' : 'LOW';\nreturn [{ eye, risk }];"
},
"name": "AI Risk Engine",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [500, 300]
},
{
"parameters": {
"chatId": "YOUR_CHAT_ID",
"text": "๐จ Drowsiness Alert!\nRisk Level: {{$json.risk}}\nEye Value: {{$json.eye}}"
},
"name": "Telegram Alert",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1,
"position": [750, 200]
},
{
"parameters": {
"operation": "append",
"sheetId": "YOUR_SHEET_ID",
"range": "Sheet1!A:D",
"data": "={{[$now, $json.eye, $json.risk, 'Alert Sent']}}"
},
"name": "Google Sheets Logger",
"type": "n8n-nodes-base.googleSheets",
"typeVersion": 1,
"position": [750, 400]
},
{
"parameters": {
"url": "https://api.thingspeak.com/update",
"method": "GET",
"queryParametersUi": {
"parameter": [
{
"name": "api_key",
"value": "YOUR_API_KEY"
},
{
"name": "field1",
"value": "={{$json.eye}}"
}
]
}
},
"name": "ThingSpeak Update",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [1000, 300]
}
],
"connections": {
"Webhook": {
"main": [[{"node": "AI Risk Engine", "type": "main", "index": 0}]]
},
"AI Risk Engine": {
"main": [
[{"node": "Telegram Alert", "type": "main", "index": 0}],
[{"node": "Google Sheets Logger", "type": "main", "index": 0}],
[{"node": "ThingSpeak Update", "type": "main", "index": 0}]
]
}
}
}
✅ 3. ๐ง Advanced ML Model (Python + Dataset)
๐ Dataset Structure
driver_data.csv
eye_closure blink_rate head_tilt drowsy
0.2 18 5 0
0.8 5 25 1
๐ค Training Model (Random Forest)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import joblib
# Load dataset
data = pd.read_csv("driver_data.csv")
X = data[['eye_closure', 'blink_rate', 'head_tilt']]
y = data['drowsy']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)
accuracy = model.score(X_test, y_test)
print("Accuracy:", accuracy)
joblib.dump(model, "drowsiness_model.pkl")
๐ฎ Real-time Prediction (ESP32 backend or server)
import joblib
import numpy as np
model = joblib.load("drowsiness_model.pkl")
def predict(eye, blink, tilt):
data = np.array([[eye, blink, tilt]])
result = model.predict(data)
return "DROWSY" if result[0] == 1 else "NORMAL"
✅ 4. ๐ฑ Mobile Dashboard (Flutter Alternative)
๐ Option A: Flutter App
Features:
Live drowsiness status
Graph (ThingSpeak API)
Alert history
Driver score
๐ฑ Flutter UI Code (Basic)
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Dashboard(),
);
}
}
class Dashboard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Driver Monitor")),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Drowsiness Status: NORMAL",
style: TextStyle(fontSize: 20)),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {},
child: Text("Refresh Data"),
)
],
),
),
);
}
}
๐ Option B: No-Code Dashboard (Recommended)
Use:
Blynk IoT
ThingSpeak IoT Platform
Features:
Drag-drop widgets
Live graph
Mobile alerts
Easy ESP32 integration
✅ 5. ๐️ GitHub-Ready Project Structure
driver-drowsiness-ai-iot/
│
├── esp32/
│ ├── drowsiness.ino
│
├── n8n-workflow/
│ ├── drowsiness_n8n_workflow.json
│
├── ml-model/
│ ├── train_model.py
│ ├── predict.py
│ ├── driver_data.csv
│ ├── drowsiness_model.pkl
│
├── flutter-app/
│ ├── lib/
│ │ ├── main.dart
│
├── docs/
│ ├── circuit_diagram.png
│ ├── system_architecture.png
│ ├── flowchart.png
│
├── cloud-setup/
│ ├── thingspeak_guide.md
│ ├── telegram_setup.md
│ ├── google_sheets_setup.md
│
├── README.md
└── LICENSE
๐ README.md (Sample)
# AI Driver Drowsiness Detection System
Tech Stack:
- ESP32
- n8n Automation
- Telegram Bot
- ThingSpeak IoT
- Google Sheets
- Machine Learning (Random Forest)
Features:
- Real-time drowsiness detection
- AI-powered risk prediction
- Multi-channel alerts
- Cloud dashboard monitoring
Setup:
1. Upload ESP32 code
2. Import n8n workflow
3. Configure Telegram bot
4. Connect Google Sheets
5. Enable ThingSpeak channel
Subscribe to:
Post Comments (Atom)
AI-Powered Home Automation Using Voice and Face Recognition
๐ AI-Powered Home Automation Using Voice & Face Recognition (ESP32 + Agentic IoT + n8n + Telegram + Google Sheets + ThingSpeak) ๐ AI-...
-
www.svsembedded.com SVSEMBEDDED svsembedded@gmail.com , CONTACT: 9491535690, 7842358459 ------------------------------------------...
-
Watch Video Demonstration Carefully Till End -- Temperature and Humidity Controller For Incubator Temperature and Humidity Controller For ...
-
Electronic KITS: DTDC Courier Proof Of Delivery Receipts - 2024 - 2023 - 2022 - 2021 - 2020 - 2019 - 2018 - 2017 - 2016...


No comments:
Post a Comment