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 vehicle using an Arduino UNO microcontroller. The vehicle is equipped with an HC-SR04 ultrasonic sensor for obstacle detection, a Tower Pro SG90 servo for directional control, and an L298N motor driver to control four DC motors. The circuit is powered by a 3xAA battery pack and includes a rocker switch for power control.

Component List

HC-SR04 Ultrasonic Sensor

Arduino UNO

L298N DC Motor Driver

Motor amarillo motorreductor hobby (4 units)

3xAA Battery

Rocker Switch (SPST)

Tower Pro SG90 Servo

Wiring Details

HC-SR04 Ultrasonic Sensor

Arduino UNO

L298N DC Motor Driver

Motor amarillo motorreductor hobby

3xAA Battery

Rocker Switch (SPST)

Tower Pro SG90 Servo

Documented Code

#include <Servo.h>          // Standard library for the servo
#include <NewPing.h>        // Library for ultrasonic sensor function

// L298N motor control pins
const int LeftMotorForward = 8;
const int LeftMotorBackward = 7;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;
const int enA = 6;  // Speed control for left motor
const int enB = 3;  // Speed control for right motor

// Sensor pins
#define trig_pin 12
#define echo_pin 11

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
const int motorSpeed = 100; // Set motor speed (0-255, lower = slower)
const int obstacleThreshold = 25; // Minimum distance to detect obstacle (in cm)

NewPing sonar(trig_pin, echo_pin, maximum_distance); // Sensor function
Servo servo_motor; 

void setup() {
  // Initialize motor pins
  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);

  // Attach servo motor
  servo_motor.attach(9);
  servo_motor.write(115); // Center the servo
  delay(1000);

  // Initialize sensor
  distance = readPing();
}

void loop() {
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);
  
  if (distance <= obstacleThreshold) { // Use new threshold
    moveStop();
    delay(200);
    moveBackward();
    delay(300);
    moveStop();
    delay(200);
    distanceRight = lookRight();
    distanceLeft = lookLeft();

    if (distanceRight >= distanceLeft) {
      turnRight();
    } else {
      turnLeft();
    }
  } else {
    moveForward(); 
  }
  
  distance = readPing();
}

int lookRight() {  
  servo_motor.write(50);
  delay(300);
  int distance = readPing();
  delay(100);
  servo_motor.write(115); // Reset servo to center
  return distance;
}

int lookLeft() {
  servo_motor.write(170);
  delay(300);
  int distance = readPing();
  delay(100);
  servo_motor.write(115); // Reset servo to center
  return distance;
}

int readPing() {
  delay(50);
  int cm = sonar.ping_cm();
  if (cm == 0) {
    cm = 250; // Assume no obstacle if out of range
  }
  return cm;
}

// Stop the motors
void moveStop() {
  analogWrite(enA, 0);
  analogWrite(enB, 0);
}

// Move forward with controlled speed
void moveForward() {
  if (!goesForward) {
    goesForward = true;
    analogWrite(enA, motorSpeed);
    analogWrite(enB, motorSpeed);
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);
  }
}

// Move backward with controlled speed
void moveBackward() {
  goesForward = false;
  analogWrite(enA, motorSpeed);
  analogWrite(enB, motorSpeed);
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
}

// Turn right
void turnRight() {
  analogWrite(enA, 150);
  analogWrite(enB,