Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32 and L298N-Based Obstacle Avoiding Robot with IR Sensors

View and Edit Project In Cirkit DesignerImage of ESP32 and L298N-Based Obstacle Avoiding Robot with IR Sensors

Circuit Documentation

Summary

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.

Component List

  1. ESP32 (30 pin)

    • Description: A powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities.
    • Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, Vin, D23, D22, TX0, RX0, D21, D19, D18, D5, TX2, RX2, D4, D2, D15, 3V3
  2. L298N DC Motor Driver

    • Description: A dual H-bridge motor driver that allows control of two DC motors.
    • Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
  3. DC Motor (2 units)

    • Description: Standard DC motors used for driving the robot.
    • Pins: pin 1, pin 2
  4. IR Sensor (2 units)

    • Description: Infrared sensors used for obstacle detection.
    • Pins: out, gnd, vcc
  5. 9V Battery

    • Description: Provides power to the circuit.
    • Pins: +, -
  6. 7805 Voltage Regulator

    • Description: Regulates the 9V battery voltage down to 5V.
    • Pins: Vin, Gnd, Vout

Wiring Details

ESP32 (30 pin)

L298N DC Motor Driver

DC Motor (First Unit)

DC Motor (Second Unit)

IR Sensor (First Unit)

IR Sensor (Second Unit)

9V Battery

7805 Voltage Regulator

Documented Code

ESP32 Code

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
}

}

Second Microcontroller Code