Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

ESP32 and L298N-Based Obstacle Avoiding Robot with IR Sensors

Image 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)

  • 3V3: Connected to vcc of both IR sensors.
  • GND: Connected to gnd of both IR sensors, GND of L298N motor driver, and Gnd of 7805 voltage regulator.
  • D34: Connected to out of the first IR sensor.
  • D35: Connected to out of the second IR sensor.
  • D25: Connected to ENA of L298N motor driver.
  • D26: Connected to IN1 of L298N motor driver.
  • D27: Connected to IN2 of L298N motor driver.
  • D14: Connected to ENB of L298N motor driver.
  • D12: Connected to IN3 of L298N motor driver.
  • D13: Connected to IN4 of L298N motor driver.
  • Vin: Connected to Vout of 7805 voltage regulator.

L298N DC Motor Driver

  • OUT1: Connected to pin 1 of the first DC motor.
  • OUT2: Connected to pin 2 of the first DC motor.
  • OUT3: Connected to pin 1 of the second DC motor.
  • OUT4: Connected to pin 2 of the second DC motor.
  • 12V: Connected to + of the 9V battery.
  • GND: Connected to GND of ESP32, Gnd of 7805 voltage regulator, and - of the 9V battery.
  • ENA: Connected to D25 of ESP32.
  • IN1: Connected to D26 of ESP32.
  • IN2: Connected to D27 of ESP32.
  • ENB: Connected to D14 of ESP32.
  • IN3: Connected to D12 of ESP32.
  • IN4: Connected to D13 of ESP32.

DC Motor (First Unit)

  • pin 1: Connected to OUT1 of L298N motor driver.
  • pin 2: Connected to OUT2 of L298N motor driver.

DC Motor (Second Unit)

  • pin 1: Connected to OUT3 of L298N motor driver.
  • pin 2: Connected to OUT4 of L298N motor driver.

IR Sensor (First Unit)

  • vcc: Connected to 3V3 of ESP32.
  • gnd: Connected to GND of ESP32.
  • out: Connected to D34 of ESP32.

IR Sensor (Second Unit)

  • vcc: Connected to 3V3 of ESP32.
  • gnd: Connected to GND of ESP32.
  • out: Connected to D35 of ESP32.

9V Battery

  • +: Connected to Vin of 7805 voltage regulator and 12V of L298N motor driver.
  • -: Connected to Gnd of 7805 voltage regulator, GND of L298N motor driver, and GND of ESP32.

7805 Voltage Regulator

  • Vin: Connected to + of the 9V battery.
  • Gnd: Connected to - of the 9V battery, GND of L298N motor driver, and GND of ESP32.
  • Vout: Connected to Vin of ESP32.

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