This circuit is designed to control two DC motors using an ESP32 microcontroller and an L298N motor driver. The circuit also includes two IR sensors for obstacle detection. The ESP32 reads the IR sensor inputs and controls the motors accordingly to navigate around obstacles. A 9V battery powers the entire circuit, with a 7805 voltage regulator providing a stable 5V supply to the components.
ESP32 (30 pin)
L298N DC Motor Driver
DC Motor (2 units)
IR Sensor (2 units)
9V Battery
7805 Voltage Regulator
vcc
of both IR sensors.gnd
of both IR sensors, GND
of L298N motor driver, and Gnd
of 7805 voltage regulator.out
of the first IR sensor.out
of the second IR sensor.ENA
of L298N motor driver.IN1
of L298N motor driver.IN2
of L298N motor driver.ENB
of L298N motor driver.IN3
of L298N motor driver.IN4
of L298N motor driver.Vout
of 7805 voltage regulator.pin 1
of the first DC motor.pin 2
of the first DC motor.pin 1
of the second DC motor.pin 2
of the second DC motor.+
of the 9V battery.GND
of ESP32, Gnd
of 7805 voltage regulator, and -
of the 9V battery.D25
of ESP32.D26
of ESP32.D27
of ESP32.D14
of ESP32.D12
of ESP32.D13
of ESP32.OUT1
of L298N motor driver.OUT2
of L298N motor driver.OUT3
of L298N motor driver.OUT4
of L298N motor driver.3V3
of ESP32.GND
of ESP32.D34
of ESP32.3V3
of ESP32.GND
of ESP32.D35
of ESP32.Vin
of 7805 voltage regulator and 12V
of L298N motor driver.Gnd
of 7805 voltage regulator, GND
of L298N motor driver, and GND
of ESP32.+
of the 9V battery.-
of the 9V battery, GND
of L298N motor driver, and GND
of ESP32.Vin
of ESP32.void setup() {
// put your setup code here, to run once:
}
void loop() {
// Define motor control pins
#define MOTOR1_IN1 14 // Motor 1 (left side)
#define MOTOR1_IN2 27
#define MOTOR2_IN3 26 // Motor 2 (right side)
#define MOTOR2_IN4 25
// Define IR sensor pins (front, left, right)
#define IR_SENSOR_FRONT 33
#define IR_SENSOR_LEFT 32
#define IR_SENSOR_RIGHT 35
// Function to move forward
void moveForward() {
digitalWrite(MOTOR1_IN1, HIGH);
digitalWrite(MOTOR1_IN2, LOW);
digitalWrite(MOTOR2_IN3, HIGH);
digitalWrite(MOTOR2_IN4, LOW);
}
// Function to move backward
void moveBackward() {
digitalWrite(MOTOR1_IN1, LOW);
digitalWrite(MOTOR1_IN2, HIGH);
digitalWrite(MOTOR2_IN3, LOW);
digitalWrite(MOTOR2_IN4, HIGH);
}
// Function to stop
void stopRobot() {
digitalWrite(MOTOR1_IN1, LOW);
digitalWrite(MOTOR1_IN2, LOW);
digitalWrite(MOTOR2_IN3, LOW);
digitalWrite(MOTOR2_IN4, LOW);
}
// Function to turn left
void turnLeft() {
digitalWrite(MOTOR1_IN1, LOW); // Stop left motors
digitalWrite(MOTOR1_IN2, LOW);
digitalWrite(MOTOR2_IN3, HIGH); // Run right motors
digitalWrite(MOTOR2_IN4, LOW);
}
// Function to turn right
void turnRight() {
digitalWrite(MOTOR1_IN1, HIGH); // Run left motors
digitalWrite(MOTOR1_IN2, LOW);
digitalWrite(MOTOR2_IN3, LOW); // Stop right motors
digitalWrite(MOTOR2_IN4, LOW);
}
// Setup function
void setup() {
// Initialize motor control pins
pinMode(MOTOR1_IN1, OUTPUT);
pinMode(MOTOR1_IN2, OUTPUT);
pinMode(MOTOR2_IN3, OUTPUT);
pinMode(MOTOR2_IN4, OUTPUT);
// Initialize IR sensor pins
pinMode(IR_SENSOR_FRONT, INPUT);
pinMode(IR_SENSOR_LEFT, INPUT);
pinMode(IR_SENSOR_RIGHT, INPUT);
// Initialize serial communication for debugging
Serial.begin(115200);
}
// Main loop
void loop() {
// Read IR sensors
bool frontObstacle = digitalRead(IR_SENSOR_FRONT);
bool leftObstacle = digitalRead(IR_SENSOR_LEFT);
bool rightObstacle = digitalRead(IR_SENSOR_RIGHT);
if (!frontObstacle) {
// No obstacle in front, move forward
moveForward();
} else if (!rightObstacle) {
// Obstacle in front, no obstacle on the right, turn right
turnRight();
delay(300); // Small delay for turning
} else if (!leftObstacle) {
// Obstacle in front and right, no obstacle on the left, turn left
turnLeft();
delay(300); // Small delay for turning
} else {
// Obstacles on all sides, move backward
moveBackward();
delay(300); // Small delay for reversing
}
delay(100); // Short delay for stability
}
}