
Components
Wiring diagram# Project: Gyroscope Control Home Automation System
This project uses an ESP32, an MPU6050 gyroscope/accelerometer, a 4-channel relay module, and a buzzer. The ESP32 continuously reads the hand's tilt (X and Y axis) from the MPU6050 and controls four electrical appliances through relays. The code uses simple threshold-based classification to simulate Machine Learning-based gesture recognition.
---
# Connections
## 1. ESP32 ↔ MPU6050
| MPU6050 Pin | ESP32 Pin | Description || ----------- | ------------- | ------------- || VCC | 3.3V | Power Supply || GND | GND | Common Ground || SDA | GPIO21 | I2C Data || SCL | GPIO22 | I2C Clock || INT | Not Connected | Optional |
---
## 2. ESP32 ↔ Relay Module (4-Channel)
| ESP32 Pin | Relay Module | Appliance || --------- | ------------ | ------------- || GPIO25 | IN1 | Light 1 || GPIO26 | IN2 | Light 2 || GPIO27 | IN3 | Fan / Light 3 || GPIO33 | IN4 | Light 4 |
Relay Power
| Relay Pin | Connection || --------- | ---------- || VCC | 5V || GND | GND |
---
## 3. ESP32 ↔ Buzzer
| ESP32 Pin | Buzzer || --------- | ------------ || GPIO32 | Positive (+) || GND | Negative (-) |
The buzzer produces a tone during startup using ESP32's PWM (`ledc`) functionality.
---
## 4. AC Appliance Connections
Each appliance is connected through one relay.
```textAC Live││Relay COM│Relay NO│Appliance│Neutral -------------------- Appliance```
Example:
| Relay | Appliance || ------- | --------- || Relay 1 | Bulb 1 || Relay 2 | Bulb 2 || Relay 3 | Fan || Relay 4 | Bulb 3 |
---
# ESP32 Pin Mapping
| ESP32 GPIO | Connected Device || ---------- | ---------------------- || GPIO21 | MPU6050 SDA || GPIO22 | MPU6050 SCL || GPIO25 | Relay IN1 || GPIO26 | Relay IN2 || GPIO27 | Relay IN3 || GPIO32 | Buzzer || GPIO33 | Relay IN4 || 3.3V | MPU6050 VCC || 5V | Relay Module VCC || GND | MPU6050, Relay, Buzzer |
---
## Step 1: System InitializationWhen the ESP32 is powered ON:* Serial communication starts.* I2C communication is initialized.* The MPU6050 is awakened from sleep mode.* The buzzer sounds briefly to indicate startup.* All relays are turned OFF.```textPower ON│▼Initialize ESP32│▼Initialize I2C│▼Wake MPU6050│▼Turn ON Buzzer│▼Turn OFF All Relays```---## Step 2: Read Gyroscope DataThe ESP32 continuously reads the X and Y acceleration values from the MPU6050.```textRead MPU6050│▼Get X-axisGet Y-axis```These values represent the tilt of the user's hand.---## Step 3: Gesture Recognition (Simulated ML)The code uses threshold ranges to classify the hand position into different gestures. This acts as a simple gesture classifier rather than a trained machine learning model.### X-axis Decision```textX Value│┌───────────────┼────────────────┐│ │ │0–3999 4000–7999 ≥8000│ │ │Relay 1 Relay 2 Relay 3```### Logic#### Gesture 1```textX between 0 and 4000│▼Turn ON Relay 1```#### Gesture 2```textX between 4000 and 8000│▼Turn ON Relay 2```#### Gesture 3```textX greater than 8000│▼Turn ON Relay 3```---## Step 4: Y-axis Priority ControlThe Y-axis has higher priority than the X-axis.If:```textY > 4000```then:```textTurn OFF Relay 1Turn OFF Relay 2Turn OFF Relay 3Turn ON Relay 4```This means Relay 4 overrides any X-axis decision.```textY > 4000 ?│Yes ──┘│▼Turn OFF Relay1Turn OFF Relay2Turn OFF Relay3Turn ON Relay4```---# Complete Project Flow```textSTART│▼Initialize ESP32│▼Initialize MPU6050 (I2C)│▼Turn ON Buzzer (Startup)│▼Turn OFF All Relays│▼Read X and Y from MPU6050│▼Turn OFF All Relays│┌───────────┴─────────────┐│ │Check X Value Check Y Value│ │▼ ▼X=0–3999 → Relay1 ON Y>4000 ?X=4000–7999 → Relay2 ON │X≥8000 → Relay3 ON │▼Turn OFF Relay1Turn OFF Relay2Turn OFF Relay3Turn ON Relay4│▼Delay 300 ms│▼Repeat ForeverFunctional Summary| Component | Function || ------------ | ---------------------------------------------------------------------- || ESP32 | Main controller that reads sensor data and controls outputs || MPU6050 | Detects hand tilt along the X and Y axes || Relay Module | Switches electrical appliances ON/OFF || Buzzer | Startup indication || Relay 1 | Activated by small positive X-axis tilt || Relay 2 | Activated by medium positive X-axis tilt || Relay 3 | Activated by large positive X-axis tilt || Relay 4 | Activated when Y-axis exceeds the threshold and overrides other relays |
Although the project description mentions "Machine Learning," the provided code does **not** implement a trained ML model. It performs **rule-based gesture classification** using predefined threshold ranges on the MPU6050 sensor values. A true ML implementation would involve training a model (e.g., TinyML) and running inference on the ESP32.
Download the complete source code for this project.
Get every component for this project delivered to your door.