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

Arduino-Controlled Obstacle Avoiding Robot with Ultrasonic Sensor and L298N Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Obstacle Avoiding Robot with Ultrasonic Sensor and L298N Motor Driver

Circuit Documentation

Summary

This circuit is designed to control a mobile robot with ultrasonic sensing capabilities. It uses an Arduino UNO as the main microcontroller, interfaced with an Arduino Sensor Shield v5.0 for easy connection to various peripherals. The robot's movement is driven by two pairs of gearmotors connected to an L298N DC motor driver, which allows for independent control of the left and right sides of the robot. An HC-SR04 Ultrasonic Sensor is used for distance measurement, and a Servomotor SG90 is included for directional control or additional functionality. Power is supplied by a 2x 18650 battery pack, and a Rocker Switch (SPST) is used to control the power flow to the circuit.

Component List

Wiring Details

HC-SR04 Ultrasonic Sensor

Gearmotor DC Wheels (Left and Right)

Servomotor SG90

L298N DC Motor Driver

Rocker Switch (SPST)

Documented Code

#include <Servo.h>

volatile int IN1;
volatile int IN2;
volatile int IN3;
volatile int IN4;
volatile int rightDistance;
volatile int leftDistance;
Servo servo_4;

void left_side_forward(int speed) {
  // Left
  analogWrite(IN1, (speed * 0.92));
  analogWrite(IN2, 0);
}

void right_side_forward(int speed) {
  // Right
  analogWrite(IN3, speed);
  analogWrite(IN4, 0);
}

void left_side_backward(int speed) {
  // Left
  analogWrite(IN1, 0);
  analogWrite(IN2, (speed * 0.92));
}

void right_side_backward(int speed) {
  // Right
  analogWrite(IN3, 0);
  analogWrite(IN4, speed);
}

float checkdistance_A1_A2() {
  digitalWrite(A1, LOW);
  delayMicroseconds(2);
  digitalWrite(A1, HIGH);
  delayMicroseconds(10);
  digitalWrite(A1, LOW);
  float distance = pulseIn(A2, HIGH) / 58.00;
  delay(10);
  return distance;
}

void forward(int speed) {
  left_side_forward(speed);
  right_side_forward(speed);
}

void backward(int speed) {
  left_side_backward(speed);
  right_side_backward(speed);
}

void left(int speed, int time) {
  left_side_backward(speed);
  right_side_forward(speed);
  delay(time);
  stop();
}

void right(int speed, int time) {
  left_side_forward(speed);
  right_side_backward(speed);
  delay(time);
  stop();
}

void stop() {
  left_side_forward(0);
  right_side_forward(0);
}

void setup() {
  IN1 = 11;
  IN2 = 6;
  IN3 = 5;
  IN4 = 3;
  rightDistance = 3;
  leftDistance = 3;
  servo_4.attach(4);
  pinMode(A1, OUTPUT);
  pinMode(A2, INPUT);
}

void loop() {
  servo_4.write(90);
  delay(500);
  while (checkdistance_A1_A2() > 15) {
    forward(100);
  }
  stop();
  servo_4.write(0);
  delay(500);
  rightDistance = checkdistance_A1_A2();
  servo_4.write(180);
  delay(500);
  leftDistance = checkdistance_A1_A2();
  if (rightDistance >= leftDistance) {
    right(200, 380);
  } else {
    left(200, 380);
  }
}

This code controls the robot's movement based on the distance measurements from the ultrasonic sensor. It includes functions for moving forward, backward, turning left, and turning right. The checkdistance_A1_A2 function is used to measure the distance from obstacles, and the robot makes decisions based on these measurements to navigate its environment.