Difficulty Level: Intermediate
This project will guide you through building a gesture-controlled robot using an ESP32 and an MPU6050 sensor.
Follow these steps to connect your components:
1. Open the Arduino IDE (or your preferred IDE).
2. Upload the following sample code:
#include <Wire.h> #include <MPU6050.h> MPU6050 mpu; // Define motor control pins const int motorAForward = 25; const int motorABackward = 26; const int motorBForward = 27; const int motorBBackward = 14; void setup() { Serial.begin(115200); Wire.begin(); mpu.initialize(); pinMode(motorAForward, OUTPUT); pinMode(motorABackward, OUTPUT); pinMode(motorBForward, OUTPUT); pinMode(motorBBackward, OUTPUT); } void loop() { int16_t ax, ay, az, gx, gy, gz; mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // Control robot based on gesture if (ay > 2000) { // Tilt forward moveForward(); } else if (ay < -2000) { // Tilt backward moveBackward(); } else if (ax > 2000) { // Tilt right turnRight(); } else if (ax < -2000) { // Tilt left turnLeft(); } else { stop(); } delay(100); } void moveForward() { digitalWrite(motorAForward, HIGH); digitalWrite(motorABackward, LOW); digitalWrite(motorBForward, HIGH); digitalWrite(motorBBackward, LOW); } void moveBackward() { digitalWrite(motorAForward, LOW); digitalWrite(motorABackward, HIGH); digitalWrite(motorBForward, LOW); digitalWrite(motorBBackward, HIGH); } void turnRight() { digitalWrite(motorAForward, HIGH); digitalWrite(motorABackward, LOW); digitalWrite(motorBForward, LOW); digitalWrite(motorBBackward, LOW); } void turnLeft() { digitalWrite(motorAForward, LOW); digitalWrite(motorABackward, LOW); digitalWrite(motorBForward, HIGH); digitalWrite(motorBBackward, LOW); } void stop() { digitalWrite(motorAForward, LOW); digitalWrite(motorABackward, LOW); digitalWrite(motorBForward, LOW); digitalWrite(motorBBackward, LOW); }
Your gesture-controlled robot is now ready! By tilting the MPU6050 sensor, you can control the robot's movement. This project can be further enhanced by integrating additional sensors, such as ultrasonic sensors for obstacle avoidance, or using Bluetooth/Wi-Fi for remote control.