Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
163 views9 pages

The Project

My project

Uploaded by

abobaker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views9 pages

The Project

My project

Uploaded by

abobaker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

The project

Building a drone using an **ESP32**, **MPU6050**, **720 Coreless Motors**, **L293D motor
driver**, and **Wi-Fi control** by phone requires setting up a stable system for motor control, motion
detection, and Wi-Fi communication. I will guide you step by step through component installation, the
complete code, and the design of the interface to control the drone over Wi-Fi.

---

### **Components Required:**

1. **ESP32** - Microcontroller with Wi-Fi capability.

2. **MPU6050** - Gyroscope and accelerometer for motion sensing.

3. **720 Coreless Motors (x4)** - Lightweight motors to provide lift.

4. **L293D Motor Driver** - To control the motors.

5. **LiPo Battery** - Power supply for the motors and ESP32.

6. **Propellers (x4)** - Attached to the motors to create lift.

7. **Frame** - To hold all the components together.

8. **Wires and Connectors** - To connect everything.

---

### **Step 1: Wiring the Components**

#### 1. **MPU6050 to ESP32 Wiring:**

- **VCC (MPU6050)** → **3.3V (ESP32)**

- **GND (MPU6050)** → **GND (ESP32)**


- **SCL (MPU6050)** → **GPIO22 (ESP32)** (SCL)

- **SDA (MPU6050)** → **GPIO21 (ESP32)** (SDA)

#### 2. **720 Coreless Motors to L293D Motor Driver:**

- **Motors**: Connect the four coreless motors to the L293D motor driver as follows:

- Motor 1: OUT1 & OUT2

- Motor 2: OUT3 & OUT4

- Motor 3: OUT1 & OUT2 (of another L293D if using two drivers)

- Motor 4: OUT3 & OUT4 (of another L293D if using two drivers)

#### 3. **L293D Motor Driver to ESP32 Wiring:**

- **IN1 (L293D)** → **GPIO12 (ESP32)** (Motor 1 forward)

- **IN2 (L293D)** → **GPIO13 (ESP32)** (Motor 1 backward)

- **IN3 (L293D)** → **GPIO14 (ESP32)** (Motor 2 forward)

- **IN4 (L293D)** → **GPIO15 (ESP32)** (Motor 2 backward)

- **EN1 (L293D)** → **GPIO16 (ESP32)** (PWM for Motor 1)

- **EN2 (L293D)** → **GPIO17 (ESP32)** (PWM for Motor 2)

- **Power Supply**: Connect a 7.4V LiPo battery to the power input of the L293D and the motors.
Connect the ground of the battery to **GND** of ESP32 and the L293D.

#### 4. **Power Supply for ESP32:**

- Connect the ESP32 to a separate 3.7V LiPo battery or power it using the USB port.

---

### **Step 2: Understanding the Control Mechanism**


The ESP32 will handle two tasks:

- **Motion Stabilization**: Using data from the MPU6050 to maintain balance.

- **Motor Control via Wi-Fi**: Using the L293D motor driver to control the 720 coreless motors. The
motors will be controlled via a **web interface** hosted by the ESP32.

---

### **Step 3: Designing the Web Interface**

We will create a simple web server using ESP32 to control the drone motors via a mobile phone
browser. The webpage will have controls (sliders and buttons) for adjusting motor speeds and
controlling flight directions.

---

### **Complete Code for ESP32**

Here is the code that includes Wi-Fi setup, web server creation, MPU6050 integration, and motor
control using the L293D driver:

```cpp

#include <WiFi.h>

#include <Wire.h>

#include <Adafruit_MPU6050.h>

#include <Adafruit_Sensor.h>

#include <WebServer.h>

// Motor control pins


const int motor1Pin1 = 12;

const int motor1Pin2 = 13;

const int motor2Pin1 = 14;

const int motor2Pin2 = 15;

const int motorSpeedPin1 = 16; // Motor 1 speed (PWM)

const int motorSpeedPin2 = 17; // Motor 2 speed (PWM)

// Wi-Fi Credentials

const char* ssid = "Your_SSID"; // Replace with your Wi-Fi network name

const char* password = "Your_PASSWORD"; // Replace with your Wi-Fi password

// Web Server on port 80

WebServer server(80);

// MPU6050 setup

Adafruit_MPU6050 mpu;

int motor1Speed = 0;

int motor2Speed = 0;

// HTML for Web Interface

String webPage = "<html>\

<head>\

<title>Drone Control</title>\

</head>\
<body>\

<h1>Drone Control</h1>\

<form action=\"/motor1\" method=\"GET\">\

Motor 1 Speed: <input type=\"range\" min=\"0\" max=\"255\" value=\"0\" name=\"speed1\"


oninput=\"this.nextElementSibling.value=this.value\"> <output>0</output><br><br>\

<input type=\"submit\" value=\"Set Motor 1\">\

</form>\

<br>\

<form action=\"/motor2\" method=\"GET\">\

Motor 2 Speed: <input type=\"range\" min=\"0\" max=\"255\" value=\"0\" name=\"speed2\"


oninput=\"this.nextElementSibling.value=this.value\"> <output>0</output><br><br>\

<input type=\"submit\" value=\"Set Motor 2\">\

</form>\

</body>\

</html>";

void setup() {

Serial.begin(115200);

// Set up Wi-Fi

WiFi.begin(ssid, password);

Serial.print("Connecting to WiFi...");

while (WiFi.status() != WL_CONNECTED) {

delay(1000);

Serial.print(".");

}
Serial.println();

Serial.println("Connected to WiFi");

// Start the web server

server.on("/", handleRoot); // Serve the web page

server.on("/motor1", handleMotor1); // Handle motor 1 control

server.on("/motor2", handleMotor2); // Handle motor 2 control

server.begin();

Serial.println("Web server started");

// Set motor control pins as output

pinMode(motor1Pin1, OUTPUT);

pinMode(motor1Pin2, OUTPUT);

pinMode(motorSpeedPin1, OUTPUT);

pinMode(motor2Pin1, OUTPUT);

pinMode(motor2Pin2, OUTPUT);

pinMode(motorSpeedPin2, OUTPUT);

// MPU6050 initialization

if (!mpu.begin()) {

Serial.println("Failed to find MPU6050 chip");

while (1) {

delay(10);

}
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);

mpu.setGyroRange(MPU6050_RANGE_500_DEG);

Serial.println("Setup complete");

void loop() {

// Handle client requests

server.handleClient();

// You can add code here to read MPU6050 data and implement PID control later

// Function to serve the root webpage

void handleRoot() {

server.send(200, "text/html", webPage);

// Function to handle motor 1 speed control

void handleMotor1() {

if (server.hasArg("speed1")) {

motor1Speed = server.arg("speed1").toInt();

analogWrite(motorSpeedPin1, motor1Speed);

if (motor1Speed > 0) {

digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);

} else {

digitalWrite(motor1Pin1, LOW);

digitalWrite(motor1Pin2, LOW);

Serial.print("Motor 1 Speed: ");

Serial.println(motor1Speed);

server.send(200, "text/html", webPage);

// Function to handle motor 2 speed control

void handleMotor2() {

if (server.hasArg("speed2")) {

motor2Speed = server.arg("speed2").toInt();

analogWrite(motorSpeedPin2, motor2Speed);

if (motor2Speed > 0) {

digitalWrite(motor2Pin1, HIGH);

digitalWrite(motor2Pin2, LOW);

} else {

digitalWrite(motor2Pin1, LOW);

digitalWrite(motor2Pin2, LOW);

Serial.print("Motor 2 Speed: ");

Serial.println(motor2Speed);
}

server.send(200, "text/html", webPage);

```

### **Explanation of the Code:**

- **Wi-Fi Setup**: The ESP32 connects to the Wi-Fi network using the provided SSID and password.

- **Web Server**: The ESP32 hosts a simple web server on port 80. When accessed from your phone's
browser, it serves a page with sliders for controlling motor speeds.

- **Motor Control**: The L293D motor driver controls two motors using PWM for speed adjustment.
The speed is set by the sliders in the web interface.

- **MPU6050**: The MPU6050 sensor is initialized

You might also like