ESP32-Based Wireless Robot System: A Complete DIY Guide


ESP32-Based Wireless Robot System: A Complete DIY Guide

In this post, I’m going to share a detailed guide to building your own ESP32-based wireless robot. The system includes an ESP32 for wireless control, a custom PCB for motor driving, and two motorized wheels for movement. This project is perfect for hobbyists who want to dive into wireless robotics using ESP32.

Components Required:

  • ESP32 module: A microcontroller with built-in Wi-Fi and Bluetooth, perfect for wireless control.
  • Custom motor driver PCB: A printed circuit board that houses MOSFETs and other components for controlling motors.
  • Two DC motors: These are attached to wheels for mobility.
  • Battery pack: To power the entire system.
  • Miscellaneous components: LEDs, resistors, capacitors, wires, and connectors.

Understanding the Setup

  1. ESP32 Microcontroller: The ESP32 is mounted on the PCB and acts as the central controller of the system. It receives commands via Wi-Fi or Bluetooth and drives the motors accordingly.
  2. Motor Driver Circuit: The motor driver circuit is crucial for controlling the speed and direction of the motors. The custom PCB shown in the picture has:
    • MOSFETs: For controlling the power delivered to the motors.
    • LED indicators: To indicate the state of the motor driver.
    • Capacitors and resistors: To ensure stable operation of the circuit.
  3. Power Supply: The system is powered by a battery pack, which supplies the required voltage to both the ESP32 and the motors.

How the System Works

The ESP32 receives wireless commands from a smartphone or computer through either Wi-Fi or Bluetooth. These commands dictate the movement of the robot (forward, backward, left, right). The ESP32 processes the commands and generates control signals to the motor driver circuit, which drives the motors to move the robot accordingly.

Circuit Diagram

Here’s a simple overview of the circuit:

Code for ESP32

Below is the code that you can upload to your ESP32 to control the robot via Wi-Fi. This example sets up a simple web server that allows you to control the robot from a web browser.

///////////////////////////////////////////

#include <WiFi.h>

// Motor control pins
#define motor1Pin1 26
#define motor1Pin2 27
#define motor2Pin1 14
#define motor2Pin2 12

// Wi-Fi credentials
const char* ssid = “Your_SSID”;
const char* password = “Your_PASSWORD”;

// Web server on port 80
WiFiServer server(80);

void setup() {
// Initialize serial communication
Serial.begin(115200);

// Set motor pins as outputs
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);

// Start connecting to Wi-Fi
Serial.println(“Connecting to Wi-Fi…”);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}

Serial.println(“\nWi-Fi connected”);
Serial.println(WiFi.localIP());

// Start the server
server.begin();
}

void loop() {
WiFiClient client = server.available(); // Check if a client has connected
if (!client) {
return;
}

// Wait until the client sends some data
while (!client.available()) {
delay(1);
}

// Read the request from the client
String request = client.readStringUntil(‘\r’);
client.flush();

// Handle the request
if (request.indexOf(“/FORWARD”) != -1) {
moveForward();
} else if (request.indexOf(“/BACKWARD”) != -1) {
moveBackward();
} else if (request.indexOf(“/LEFT”) != -1) {
moveLeft();
} else if (request.indexOf(“/RIGHT”) != -1) {
moveRight();
} else if (request.indexOf(“/STOP”) != -1) {
stopMotors();
}

// Send the response to the client
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“”);
client.println(“<html><body><h1>ESP32 Robot Control</h1>”);
client.println(“<a href=\”/FORWARD\”>Forward</a><br>”);
client.println(“<a href=\”/BACKWARD\”>Backward</a><br>”);
client.println(“<a href=\”/LEFT\”>Left</a><br>”);
client.println(“<a href=\”/RIGHT\”>Right</a><br>”);
client.println(“<a href=\”/STOP\”>Stop</a><br>”);
client.println(“</body></html>”);
client.println(“”);
}

// Motor control functions
void moveForward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}

void moveBackward() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}

void moveLeft() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}

void moveRight() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}

void stopMotors() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}

/////////////////////////////

 

How to Upload the Code:

  1. Install ESP32 Libraries: If you haven’t already, install the ESP32 board package in Arduino IDE.
  2. Connect ESP32: Connect the ESP32 to your computer using a USB cable.
  3. Upload the Code: Open the above code in Arduino IDE and upload it to the ESP32.
  4. Connect to Wi-Fi: After uploading the code, the ESP32 will connect to your Wi-Fi network. Open the serial monitor to find the IP address of your ESP32.
  5. Control the Robot: Open a web browser and type the ESP32’s IP address in the address bar. You’ll see a simple control panel with buttons to move the robot forward, backward, left, right, or stop.

Conclusion

This ESP32-based wireless robot is a simple yet powerful project. It leverages the wireless capabilities of the ESP32 to control a robot in real-time. You can expand the functionality by adding sensors, using Bluetooth control, or integrating it with cloud services for remote control over the internet.