#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
Adafruit_MPU6050 mpu;
// Pins
#define PIR_PIN 2 // Helmet detection
#define MQ2_PIN 34 // Alcohol detection (analog)
#define RELAY_PIN 12 // Ignition control
#define BUZZER_PIN 14 // Crash alert
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
// Initialize MPU6050
if (!mpu.begin()) {
Serial.println("MPU6050 not found. Check connections.");
while (1);
}
Serial.println("Smart Helmet System Initialized.");
}
void loop() {
bool helmetWorn = digitalRead(PIR_PIN); // HIGH = motion detected = helmet worn
int alcoholLevel = analogRead(MQ2_PIN); // Simulated analog alcohol level
bool sober = alcoholLevel < 3000;
if (helmetWorn && sober) {
digitalWrite(RELAY_PIN, HIGH); // Allow ignition
Serial.println("Helmet worn and sober - Ignition ON");
} else {
digitalWrite(RELAY_PIN, LOW); // Lock ignition
if (!helmetWorn) Serial.println("Helmet not detected – Ignition locked");
if (!sober) Serial.println("Alcohol detected – Ignition locked");
}
// Crash Detection
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
float totalAccel = abs(a.acceleration.x) + abs(a.acceleration.y) + abs(a.acceleration.z);
if (totalAccel > 30) {
Serial.println("🚨 Crash detected! Please send SMS via phone app.");
tone(BUZZER_PIN,1000);
delay(1000);
noTone(BUZZER_PIN);
delay(1000);
}
delay(1000);
}