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

The circuit in question appears to be designed for a mobile robot platform equipped with an Arduino UNO microcontroller, an ultrasonic sensor for distance measurement, DC gearmotors for locomotion, a servomotor for directional control or other actuation, and an L298N motor driver for controlling the gearmotors. Power is supplied by a 2x 18650 battery pack, and a rocker switch is used to control power to the circuit. An Arduino Sensor Shield v5.0 is used to facilitate easy connections between the Arduino UNO and the various peripherals.

Component List

Microcontroller

Sensors

Actuators

Motor Driver

Power Supply

Interface Board

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

Gearmotor DC Wheels (left and right)

Servomotor SG90

L298N DC Motor Driver

Rocker Switch (SPST)

2x 18650 Battery Pack

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 is designed to control a robot with differential steering. It uses an ultrasonic sensor to detect obstacles and makes decisions to move forward or turn based on the distance readings. The servomotor is used to change the direction of the ultrasonic sensor for scanning the environment. The L298N motor driver is controlled through the Arduino pins to drive the gearmotors for movement.