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

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

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

Circuit Documentation

Summary

This document describes an obstacle-avoiding robot circuit. The robot uses an ultrasonic sensor to detect obstacles and a servo motor to change direction when an obstacle is detected. The L298N motor driver controls the DC motors for movement. When an obstacle is detected within 15 cm, the robot moves back 20 cm, then moves forward 20 cm, and stops.

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. Ultrasonic Sensor

    • Description: Sensor used to measure distance to an object using ultrasonic waves.
    • Pins: +VCC, Trigger, Echo, GND
  3. L298N DC Motor Driver

    • Description: Dual H-Bridge motor driver for controlling 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
  4. Micro Servo 9G

    • Description: Small servo motor used for precise control of angular position.
    • Pins: GND, +5V, PWM
  5. Motor amarillo motorreductor hobby

    • Description: Hobby DC motor with gear reduction.
    • Pins: vcc, GND
  6. 7.4v Battery

    • Description: Power source for the circuit.
    • Pins: +, -

Wiring Details

Arduino UNO

Ultrasonic Sensor

L298N DC Motor Driver

Micro Servo 9G

Motor amarillo motorreductor hobby

Motor amarillo motorreductor hobby

7.4v Battery

Code Documentation

/*
 * This Arduino sketch controls an obstacle-avoiding robot with a servo motor
 * and an ultrasonic sensor. The robot uses the ultrasonic sensor to detect
 * obstacles and the servo motor to change direction when an obstacle is
 * detected. The L298N motor driver controls the DC motors for movement.
 * When an obstacle is detected within 15 cm, the robot moves back 20 cm,
 * then moves forward 20 cm, and stops.
 */

// Pin definitions
const int trigPin = 8;
const int echoPin = 9;
const int servoPin = 3;
const int motorIn1 = 7;
const int motorIn2 = 6;
const int motorIn3 = 5;
const int motorIn4 = 4;

// Motor control
const int motorSpeed = 255;

#include <Servo.h>
Servo myServo;

void setup() {
  // Initialize pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
  pinMode(motorIn3, OUTPUT);
  pinMode(motorIn4, OUTPUT);
  myServo.attach(servoPin);
  myServo.write(90); // Center the servo
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;

  if (distance < 15) { // If obstacle is detected within 15 cm
    moveBackward(20); // Move back 20 cm
    moveForward(20); // Move forward 20 cm
    stopMotors(); // Stop
  } else {
    moveForward();
  }
}

void moveForward(int cm = 0) {
  if (cm > 0) {
    long duration = cm * 29.1 * 2;
    unsigned long startTime = millis();
    while (millis() - startTime < duration) {
      analogWrite(motorIn1, motorSpeed);
      analogWrite(motorIn2, 0);
      analogWrite(motorIn3, motorSpeed);
      analogWrite(motorIn4, 0);
    }
    stopMotors();
  } else {
    analogWrite(motorIn1, motorSpeed);
    analogWrite(motorIn2, 0);
    analogWrite(motorIn3, motorSpeed);
    analogWrite(motorIn4, 0);
  }
}

void moveBackward(int cm) {
  long duration = cm * 29.1 * 2;
  unsigned long startTime = millis();
  while (millis() - startTime < duration) {
    analogWrite(motorIn1, 0);
    analogWrite(motorIn2, motorSpeed);
    analogWrite(motorIn3, 0);
    analogWrite(motorIn4, motorSpeed);
  }
  stopMotors();
}

void stopMotors() {
  analogWrite(motorIn1, 0);
  analogWrite(motorIn2, 0);
  analogWrite(motorIn3, 0);
  analogWrite(motorIn4, 0);
}

This code controls the obstacle-avoiding robot by using the ultrasonic sensor to detect obstacles and the servo motor to change direction. The L298N motor driver controls the DC motors for movement. When an obstacle is detected within 15 cm, the robot moves back 20 cm, then moves forward 20 cm, and stops.