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

ESP32-Controlled Obstacle Avoidance Robot with L298N Motor Driver and HC-SR04 Sensors

View and Edit Project In Cirkit DesignerImage of ESP32-Controlled Obstacle Avoidance Robot with L298N Motor Driver and HC-SR04 Sensors

Circuit Documentation

Summary

This circuit is designed to control a robot with the capability to move in different directions while avoiding obstacles. It utilizes three HC-SR04 ultrasonic sensors to detect obstacles from the front, left, and right directions. The L298N DC motor driver is used to control the gearmotors attached to the robot's wheels, allowing it to move forward, backward, and turn. The ESP32 microcontroller is the brain of the robot, processing sensor data and sending control signals to the motor driver. A 12V battery provides the power source for the motor driver and the ESP32, which in turn powers the sensors.

Component List

L298N DC Motor Driver

Gearmotor DC Wheels (Left and Right)

Sensor HC-SR04 (x3)

Battery 12V

ESP32 38 PINS

Wiring Details

L298N DC Motor Driver

Gearmotor DC Wheels (Left and Right)

Sensor HC-SR04 (Front, Left, Right)

Battery 12V

ESP32 38 PINS

Documented Code

/*
 * This code controls a robot that moves forward, backward, left, and right
 * to avoid obstacles using three HC-SR04 sensors positioned at the front,
 * left, and right. The L298N motor driver controls the motors, and the ESP32
 * microcontroller processes the sensor data and controls the motor driver.
 */

#define TRIG_FRONT 34
#define ECHO_FRONT 35
#define TRIG_LEFT 32
#define ECHO_LEFT 33
#define TRIG_RIGHT 25
#define ECHO_RIGHT 26
#define IN1 4
#define IN2 16
#define IN3 17
#define IN4 5

void setup() {
  pinMode(TRIG_FRONT, OUTPUT);
  pinMode(ECHO_FRONT, INPUT);
  pinMode(TRIG_LEFT, OUTPUT);
  pinMode(ECHO_LEFT, INPUT);
  pinMode(TRIG_RIGHT, OUTPUT);
  pinMode(ECHO_RIGHT, INPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
}

long readDistance(int trigPin, int echoPin) {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  return pulseIn(echoPin, HIGH) * 0.034 / 2;
}

void moveForward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void moveBackward() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void turnLeft() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void turnRight() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void stopMoving() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

void loop() {
  long distanceFront = readDistance(TRIG_FRONT, ECHO_FRONT);
  long distanceLeft = readDistance(TRIG_LEFT, ECHO_LEFT);
  long distanceRight = readDistance(TRIG_RIGHT, ECHO_RIGHT);

  if (distanceFront < 20) {
    stopMoving();
    delay(500);
    if (distanceLeft < distanceRight) {
      turnRight();
    } else {
      turnLeft();
    }
    delay(500);
  } else {
    moveForward();
  }
  delay(100);
}

This code is responsible for initializing the GPIO pins connected to the HC-SR04 sensors and the L298N motor driver. It defines functions to read distances from the sensors and to control the motors for moving forward, backward, turning left, and turning right. The loop function continuously checks the distances from the sensors and decides the movement of the robot accordingly. If an obstacle is detected within 20 cm from the front sensor, the robot stops, determines the direction with more space (left or right), and turns accordingly. If no obstacle is detected, the robot moves forward.