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

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

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

Circuit Documentation

Summary

This document provides a detailed overview of a circuit designed to control a robotic car using an Arduino UNO microcontroller. The circuit includes various components such as an ultrasonic sensor, a servo motor, a DC motor driver, multiple motors with reducers, a rocker switch, and Li-ion batteries. The Arduino UNO is programmed to control the motors and the ultrasonic sensor to navigate the car and avoid obstacles.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. L298N DC Motor Driver

    • Description: Dual H-Bridge motor driver module.
    • 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. Tower Pro SG90 Servo

    • Description: Micro servo motor.
    • Pins: Signal, +5V, GND
  4. HC-SR04 Ultrasonic Sensor

    • Description: Ultrasonic distance sensor.
    • Pins: VCC, TRIG, ECHO, GND
  5. Motor with Reducer

    • Description: DC motor with a gear reducer.
    • Pins: 3 - 6 VCC, GND
  6. 18650 Li-ion Battery x 2

    • Description: Rechargeable lithium-ion batteries.
    • Pins: +, -
  7. Rocker Switch

    • Description: On/Off switch.
    • Pins: 1, 2
  8. DC Motor

    • Description: Standard DC motor.
    • Pins: pin 1, pin 2

Wiring Details

Arduino UNO

L298N DC Motor Driver

Tower Pro SG90 Servo

HC-SR04 Ultrasonic Sensor

Motor with Reducer

18650 Li-ion Battery x 2

Rocker Switch

DC Motor

Code Documentation

#include <Servo.h>

#define trigPin 8
#define echoPin 11
#define ENA 9  // PWM pin for motor A
#define ENB 10 // PWM pin for motor B
#define IN1 7
#define IN2 6
#define IN3 5
#define IN4 4

Servo ultrasonicServo;

long duration;
int distance;
int servoAngle = 90;
int motorSpeed = 250; // Adjust this value for slow speed (0 to 255)

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT); // Updated to pin 9
  pinMode(ENB, OUTPUT); // Updated to pin 10

  Serial.begin(9600);
  ultrasonicServo.attach(3);
  ultrasonicServo.write(servoAngle); // Start with the servo facing forward
}

void loop() {
  distance = measureDistance();

  Serial.print("Distance: ");
  Serial.println(distance); // Debugging output

  if (distance > 0 && distance < 30) { // Obstacle detected within 30 cm
    stopCar();
    delay(500);

    // Move backward for a short duration
    moveBackward();
    delay(1000); // Adjust this delay as needed for proper backward movement
    stopCar();
    delay(500);

    // Check distances to the left and right
    int distanceLeft = lookLeft();
    int distanceRight = lookRight();
    ultrasonicServo.write(90); // Reset servo to forward position

    // Decide direction based on distances
    if (distanceLeft > distanceRight) {
      turnLeft();
    } else {
      turnRight();
    }
    delay(1000); // Allow time for turning
  } else {
    moveForward();
  }
}

int measureDistance() {
  const int samples = 5; // Number of samples for averaging
  long sum = 0;

  for (int i = 0; i < samples; i++) {
    // Send ultrasonic pulse
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    // Read echo and calculate distance
    duration = pulseIn(echoPin, HIGH);
    int measuredDistance = duration * 0.034 / 2; // Convert to centimeters
    sum += measuredDistance;
    delay(10); // Small delay between readings
  }

  return sum / samples; // Return the average distance
}

int lookRight() {
  ultrasonicServo.write(0); // Turn servo to the right
  delay(500);
  int distance = measureDistance();
  delay(100);
  return distance;
}

int lookLeft() {
  ultrasonicServo.write(180); // Turn servo to the left
  delay(500);
  int distance = measureDistance();
  delay(100);
  return distance;
}

void moveForward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, motorSpeed); // Speed control
  analogWrite(ENB, motorSpeed);
}

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