Wi-Fi Controlled ESP32-Based Robotic Vehicle with Servo Actuation and Ultrasonic Sensing
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
The circuit in question is designed to control multiple DC motors and servos using an ESP32 microcontroller. The ESP32 is responsible for driving a pair of L298N DC motor drivers, which in turn control the speed and direction of four DC motors. Additionally, the ESP32 manages the position of four servos. The circuit also includes two HC-SR04 ultrasonic sensors for distance measurement. Power management is handled by a 12V battery, a 12V to 5V step-down power converter, and a 48V to 5V converter to ensure that all components receive the correct operating voltage. The ESP32 is also equipped with Wi-Fi capabilities, allowing for remote control via a web server.
Component List
Microcontroller
- ESP32 (30 pin): A Wi-Fi and Bluetooth enabled microcontroller that is the central controller of the circuit.
Motor Drivers
- L298N DC motor driver: Used to control the direction and speed of DC motors.
Motors
- DC Motor: Four individual motors controlled by the L298N motor drivers.
Servos
- Servo: Four servos controlled by the ESP32 for precise angular movement.
Sensors
- HC-SR04 Ultrasonic Sensor: Two sensors used for distance measurement.
Power Management
- 12v to 5v Step Down Power Converter: Converts 12V to 5V to power the servos and the ESP32.
- 48v to 5v Converter: Steps down 48V to 5V, likely used for sensor power supply.
- Battery 12v: Provides the main power source for the circuit.
Miscellaneous
- Comment: Placeholder components that may represent annotations or notes in the circuit design.
Wiring Details
ESP32 (30 pin)
D32,D33connected to L298N motor driver (control signals for motor driver)D26,D27,D14,D12,D13,D15connected to another L298N motor driver (control signals for motor driver)D23,D22,D21,D19connected to HC-SR04 ultrasonic sensors (trigger and echo signals)D18,D5,D4,D2connected to servos (control pulses)Vinconnected to 12V power supplyGNDconnected to common ground
L298N DC motor driver
IN1,IN2,IN3,IN4connected to ESP32 (control signals from microcontroller)OUT1,OUT2,OUT3,OUT4connected to DC motors (output to motors)12Vconnected to 12V power supplyGNDconnected to common ground
DC Motor
pin 1,pin 2connected to L298N motor driver (motor terminals)
Servo
gndconnected to common groundvccconnected to 5V power supplypulseconnected to ESP32 (control signal from microcontroller)
HC-SR04 Ultrasonic Sensor
VCCconnected to 5V power supplyTRIG,ECHOconnected to ESP32 (trigger and echo signals)GNDconnected to common ground
12v to 5v Step Down Power Converter
VIN 9v-36v,VIN+connected to 12V power supplyUSB OUTPUT 5V,5v OUTPUTconnected to servos and ESP32 (5V power output)GNDconnected to common ground
Battery 12v
+connected to 12V power supply line-connected to common ground
48v to 5v Converter
out+,out--connected to HC-SR04 ultrasonic sensors (5V power output)in+,in--connected to 48V power supply line
Documented Code
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <Servo.h>
// Motor pins (L298N Motor Driver)
const int motor1Pin1 = 26;
const int motor1Pin2 = 27;
const int motor2Pin1 = 14;
const int motor2Pin2 = 12;
const int motor3Pin1 = 13;
const int motor3Pin2 = 15;
const int motor4Pin1 = 32;
const int motor4Pin2 = 33;
// Servo pins
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
const int servo1Pin = 2;
const int servo2Pin = 4;
const int servo3Pin = 5;
const int servo4Pin = 18;
// Wi-Fi credentials
const char *ssid = "YOUR_SSID";
const char *password = "YOUR_PASSWORD";
// Server object
AsyncWebServer server(80);
// Function to stop all motors
void stopMotors() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor3Pin1, LOW);
digitalWrite(motor3Pin2, LOW);
digitalWrite(motor4Pin1, LOW);
digitalWrite(motor4Pin2, LOW);
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Setup motor pins
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(motor3Pin1, OUTPUT);
pinMode(motor3Pin2, OUTPUT);
pinMode(motor4Pin1, OUTPUT);
pinMode(motor4Pin2, OUTPUT);
// Attach servos
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
servo3.attach(servo3Pin);
servo4.attach(servo4Pin);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Setup web server routes
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Control your RC Car");
});
// Control routes (adjust as per app logic)
server.on("/forward", HTTP_GET, [](AsyncWebServerRequest *request) {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor3Pin1, HIGH);
digitalWrite(motor3Pin2, LOW);
digitalWrite(motor4Pin1, HIGH);
digitalWrite(motor4Pin2, LOW);
request->send(200, "text/plain", "Moving Forward");
});
server.on("/stop", HTTP_GET, [](AsyncWebServerRequest *request) {
stopMotors();
request->send(200, "text/plain", "Stopped");
});
server.begin();
}
void loop() {
// Handle other actions like servo movement in response to app commands
}
This code is designed to run on the ESP32 microcontroller. It sets up Wi-Fi connectivity, a web server, and defines routes for controlling the motors and servos. The stopMotors function is used to stop all motors, and the setup function initializes the system and connects to Wi-Fi. The loop function is left empty, as the primary control is handled via web server routes.