Make a WiFi-Controlled Robot with Speed Control Using ESP32: The Best Step-by-Step Guide

Are you ready to build a WiFi-controlled robot that’s faster and more efficient than traditional Bluetooth-controlled robots? With the ESP32, you can create a high-speed, responsive robot that you can control via a smartphone app. This guide will walk you through every step, from understanding the basics of WiFi and ESP32 to building and controlling your robot. Let’s dive in!

Make a WiFi-Controlled Robot with Speed Control Using ESP32: The Best Step-by-Step Guide

What is WiFi?

WiFi is a wireless technology that allows devices to connect to the internet or communicate with each other over a local network. Unlike Bluetooth, WiFi offers a longer range, faster data transfer, and the ability to handle multiple connections simultaneously. This makes it ideal for projects like a WiFi-controlled robot, where speed and responsiveness are crucial.

What is ESP32?

The ESP32 is a powerful, low-cost microcontroller with built-in WiFi and Bluetooth capabilities. It’s widely used in IoT projects due to its versatility, dual-core processor, and extensive GPIO pins. The ESP32 Dev Board is a development board that makes it easy to program and use the ESP32 in your projects.

Why Choose ESP32 for a WiFi-Controlled Robot?

  1. Faster Communication: Compared to HC-05 Bluetooth modules, the ESP32 offers faster data transfer and lower latency.
  2. Longer Range: WiFi provides a longer range than Bluetooth, allowing you to control your robot from a greater distance.
  3. Ease of Use: The ESP32 is beginner-friendly and integrates seamlessly with the Arduino IDE.
  4. Speed Control: With PWM (Pulse Width Modulation), you can precisely control the speed of your robot’s motors.

Step-by-Step Guide to Building a WiFi-Controlled Robot

Materials Needed

  • ESP32 Dev Board
  • L298N Motor Driver Module
  • DC Motors (2)
  • Wheels (2)
  • Chassis
  • Battery Pack (7.4V or 9V)
  • Jumper Wires
  • Smartphone with the ESP32 WiFi Controller App

Step 1: Add ESP32 to Arduino IDE

  1. Open the Arduino IDE and go to File > Preferences.
  2. In the Additional Boards Manager URLs field, paste this link:
   https://dl.espressif.com/dl/package_esp32_index.json
  1. Go to Tools > Board > Boards Manager, search for ESP32, and install the package.
  2. Select your ESP32 board under Tools > Board > ESP32 Arduino.

Step 2: Circuit Connections

Here’s how to connect the ESP32 to the L298N motor driver and motors:

ESP32 PinL298N PinFunction
GPIO 26ENALeft Motor Speed
GPIO 27IN1Left Motor Direction
GPIO 25IN2Left Motor Direction
GPIO 14ENBRight Motor Speed
GPIO 12IN3Right Motor Direction
GPIO 13IN4Right Motor Direction
GNDGNDGround
5V5VPower
  • Connect the motors to the L298N outputs.
  • Power the L298N and ESP32 using the battery pack.

Step 3: Upload the Code

Copy and paste the following code into the Arduino IDE:

#include <WiFi.h>
#include <WebServer.h>

// WiFi AP credentials
const char* ssid = "ESP32-AP";
const char* password = "12345678";

// Create web server on port 80
WebServer server(80);

// L298N motor driver pins
const int ENA = 26;  // Left motor PWM (speed)
const int IN1 = 27;  // Left motor direction 1
const int IN2 = 25;  // Left motor direction 2
const int ENB = 14;  // Right motor PWM (speed)
const int IN3 = 12;  // Right motor direction 1
const int IN4 = 13;  // Right motor direction 2

// Variables to store command and speed
String currentCommand = "";
int currentSpeed = 0;
const int defaultSpeed = 100; // Default speed when no value is provided

void setup() {
  // Start Serial
  Serial.begin(115200);
  delay(1000);

  // Set up motor pins
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

  // Set up Access Point
  WiFi.softAP(ssid, password);

  Serial.println();
  Serial.print("AP IP address: ");
  Serial.println(WiFi.softAPIP());

  // Define server routes
  server.onNotFound(handleCommand);

  // Start server
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}

void handleCommand() {
  String request = server.uri();

  if (request.startsWith("/")) {
    request = request.substring(1);
  }

  // Parse command and speed (if provided)
  if (request.indexOf(":") != -1) {
    int colonIndex = request.indexOf(":");
    currentCommand = request.substring(0, colonIndex);
    String speedStr = request.substring(colonIndex + 1);
    currentSpeed = speedStr.toInt();
  } else {
    currentCommand = request;
    currentSpeed = (currentCommand == "S") ? 0 : defaultSpeed; // Default speed unless "S"
  }

  // Process command and control robot
  controlRobot();
  displayCommand();

  // Send response back to client
  server.send(200, "text/plain", "Command received: " + request);
}

void controlRobot() {
  // Stop motors by default
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);

  if (currentCommand == "F") { // Forward
    digitalWrite(IN1, HIGH);  // Left motor forward
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, HIGH);  // Right motor forward
    digitalWrite(IN4, LOW);
    analogWrite(ENA, currentSpeed);
    analogWrite(ENB, currentSpeed);
  } 
  else if (currentCommand == "B") { // Backward
    digitalWrite(IN1, LOW);   // Left motor backward
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, LOW);   // Right motor backward
    digitalWrite(IN4, HIGH);
    analogWrite(ENA, currentSpeed);
    analogWrite(ENB, currentSpeed);
  } 
  else if (currentCommand == "L") { // Left
    digitalWrite(IN1, LOW);   // Left motor backward
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);  // Right motor forward
    digitalWrite(IN4, LOW);
    analogWrite(ENA, currentSpeed);
    analogWrite(ENB, currentSpeed);
  } 
  else if (currentCommand == "R") { // Right
    digitalWrite(IN1, HIGH);  // Left motor forward
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);   // Right motor backward
    digitalWrite(IN4, HIGH);
    analogWrite(ENA, currentSpeed);
    analogWrite(ENB, currentSpeed);
  } 
  else if (currentCommand == "S") { // Stop
    analogWrite(ENA, 0);
    analogWrite(ENB, 0);
  }
}

void displayCommand() {
  Serial.println("-------------------");
  Serial.print("Received Command: ");
  Serial.println(currentCommand);

  if (currentSpeed > 0) {
    Serial.print("Speed Value: ");
    Serial.println(currentSpeed);
  }

  if (currentCommand == "F") {
    Serial.println("Action: Moving Forward");
  } else if (currentCommand == "B") {
    Serial.println("Action: Moving Backward");
  } else if (currentCommand == "L") {
    Serial.println("Action: Turning Left");
  } else if (currentCommand == "R") {
    Serial.println("Action: Turning Right");
  } else if (currentCommand == "S") {
    Serial.println("Action: Stop");
  }
  Serial.println("-------------------");
}

Step 4: Download the ESP32 WiFi Controller App

  1. Search for the ESP32 WiFi Controller App on the Google Play Store or Apple App Store.
  2. Connect your smartphone to the ESP32’s WiFi network (SSID: ESP32-AP, Password: 12345678).
  3. Open the app and enter the ESP32’s IP address.
  4. Use the app to control your robot’s movement and speed.

Step 5: Test and Calibrate

  • Power on your robot and ensure it connects to the app.
  • Test each command (Forward, Backward, Left, Right, Stop) and adjust the speed as needed.

Commonly Asked Questions

1. Can I use a different motor driver?

Yes, you can use other motor drivers like the L293D or TB6612FNG. Just adjust the pin connections accordingly.

2. How do I increase the range of my WiFi-controlled robot?

Use an external antenna or a WiFi repeater to extend the range.

3. Can I control multiple robots with one app?

Yes, but each robot must have a unique SSID and IP address.

Conclusion

Building a WiFi-controlled robot with the ESP32 is a fun and rewarding project that combines electronics, programming, and robotics. With its superior speed and range, this robot outperforms traditional Bluetooth-controlled models. Follow this guide, and you’ll have a fully functional robot in no time. Happy building!

By following this guide, you’ve created a WiFi-controlled robot that’s faster, smarter, and more efficient. Whether you’re a beginner or an experienced maker, this project is a great way to explore the capabilities of the ESP32 and WiFi technology.

Read More –

  1. 10 Beginner-Friendly Machine Learning Projects
  2. 10 Best Essential Basics of Machine Learning 

Downlaod Basic electronics e-Book Click Here

Visit : Home Page

Learn about other sensors, such as Arduino sensors.