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

Arduino UNO-Based Obstacle-Avoiding Robot with Ultrasonic Sensors and DC Motors

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Obstacle-Avoiding Robot with Ultrasonic Sensors and DC Motors

Circuit Documentation

Summary

This circuit is designed to control two DC motors using an L298N motor driver and an Arduino UNO. It also includes three HC-SR04 ultrasonic sensors for obstacle detection. The power is supplied by a 3xAAA battery pack with a switch.

Component List

  1. DC Motor

    • Description: A standard DC motor.
    • Pins: pin 1, pin 2
  2. L298N DC Motor Driver

    • Description: A dual H-Bridge motor driver that allows control of two 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
  3. HC-SR04 Ultrasonic Sensor

    • Description: An ultrasonic distance sensor.
    • Pins: VCC, TRIG, ECHO, GND
  4. Arduino UNO

    • Description: A 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
  5. 3xAAA Battery Pack with Switch and JST

    • Description: A battery pack that holds three AAA batteries and includes a switch.
    • Pins: POS, NEG

Wiring Details

DC Motor 1

DC Motor 2

L298N DC Motor Driver

HC-SR04 Ultrasonic Sensor 1

HC-SR04 Ultrasonic Sensor 2

HC-SR04 Ultrasonic Sensor 3

Arduino UNO

3xAAA Battery Pack with Switch and JST

Documented Code

#include <NewPing.h>

// Motor Pins
const int IN_LEFT_F = 12; // Motor Left 
const int IN_LEFT_R = 13; // Motor Left control pin

const int IN_RIGHT_F = 8; // Motor Right control pin
const int IN_RIGHT_R = 9; // Motor Right control pin

const int EN_LEFT = 11; // PWM control for Motor Left
const int EN_RIGHT = 10; // PWM control for Motor Right

// Ultrasonic Sensor Pins and Settings
const int TRIG_FRONT = 4; 
const int ECHO_FRONT = 3;

// Ultrasonic Sensor Pins and Settings

const int TRIG_LEFT = 6; 
const int ECHO_LEFT = 5;

// Ultrasonic Sensor Pins and Settings
const int TRIG_RIGHT = 15; 
const int ECHO_RIGHT = 14;

/*Define RX and TX pins for SoftwareSerial
SoftwareSerial BTSerial(1, 0); // RX, TX
String incomingData = "";
*/
const int motorSpeed_RIGHT = 100; // (max PWM value 255)
const int motorSpeed_LEFT = 100;  // (max PWM value 255, 25 to support)

// Max distance to measure in cm
const int MAX_DISTANCE = 250; 

//Create NewPing objects for each sensor
NewPing sonarFront(TRIG_FRONT, ECHO_FRONT, MAX_DISTANCE); 
NewPing sonarRight(TRIG_RIGHT, ECHO_RIGHT, MAX_DISTANCE);
NewPing sonarLeft(TRIG_LEFT, ECHO_LEFT, MAX_DISTANCE);

const int distanceFThreshold = 15; // Distance in cm for obstacle detection
const int distanceRThreshold = 8; // Distance in cm for obstacle detection
const int distanceLThreshold = 8; // Distance in cm for obstacle detection

int distanceFront=0;
int distanceLeft=0;
int distanceRight=0;
int hitCounter = 0;

void setup() {
  // Motor pins setup
  pinMode(IN_RIGHT_F, OUTPUT);
  pinMode(IN_RIGHT_R, OUTPUT);
  pinMode(IN_LEFT_F, OUTPUT);
  pinMode(IN_LEFT_R, OUTPUT);
  
  pinMode(EN_RIGHT, OUTPUT);
  pinMode(EN_LEFT, OUTPUT);
  
  analogWrite(EN_RIGHT, motorSpeed_RIGHT); // Set motor speed to 10%
  analogWrite(EN_LEFT, motorSpeed_LEFT); // Set motor speed to 10%
/*
  BTSerial.begin(9600); // Default baud rate of HC-05/HC-06
  Serial.println("Bluetooth module ready.");
*/
  Serial.begin(9600); // Initialize Serial for debugging
}

void loop() {

  distanceFront = sonarFront.convert_cm(sonarFront.ping_median(5));
  distanceRight = sonarRight.convert_cm(sonarRight.ping_median(5));
  distanceLeft = sonarLeft.convert_cm(sonarLeft.ping_median(5));

  Serial.print("Front: ");
  Serial.print(distanceFront);
  Serial.print(" cm | Left: ");
  Serial.print(distanceLeft);
  Serial.print(" cm | Right: ");
  Serial.print(distanceRight);
  Serial.println(" cm");

  if (distanceFront == 0 || distanceLeft == 0 || distanceRight == 0) {
    Serial.println(" ------ Sensor issue or I am hit");
    stopMotors();
    delay(200);
    hitCounter = hitCounter + 1;
    if (hitCounter > 5){
      moveBackward();
      delay