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

Arduino UNO-Based Autonomous Obstacle-Avoiding Robot with Ultrasonic and IR Sensors

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

Circuit Documentation

Summary

This document provides a detailed overview of a circuit that includes an Arduino UNO microcontroller, various sensors, motor drivers, motors, LEDs, a pushbutton, and a wireless relay. The circuit is designed to control the movement of a robot based on sensor inputs and to provide visual feedback using LEDs.

Component List

  1. 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
  2. L298N DC Motor Driver

    • Description: A 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. DC Motor

    • Description: A simple DC motor.
    • Pins: pin 1, pin 2
  4. LM2596

    • Description: A step-down voltage regulator.
    • Pins: IN, OUT
  5. HC-SR04 Ultrasonic Sensor

    • Description: An ultrasonic distance sensor.
    • Pins: VCC, TRIG, ECHO, GND
  6. IR Sensor

    • Description: An infrared sensor.
    • Pins: out, gnd, vcc
  7. 12V 200Ah Battery

    • Description: A high-capacity battery.
    • Pins: GND, 12V
  8. Pushbutton

    • Description: A simple pushbutton switch.
    • Pins: Pin 2, Pin 1, Pin 3, Pin 4
  9. LED: Two Pin (red)

    • Description: A red LED.
    • Pins: cathode, anode
  10. 12V Wireless Relay

    • Description: A wireless relay module.
    • Pins: NEGATIF, POSITIF, NO, C, NC

Wiring Details

Arduino UNO

L298N DC Motor Driver

12V Wireless Relay

12V 200Ah Battery

Pushbutton

Code Documentation

#define LEFT 6
#define RIGHT 7
#define trigPin 5
#define echoPin 4

#define LedLeft 2
#define LedRight 3

const int LeftMotorForward = 9;
const int LeftMotorBackward = 8;
const int RightMotorForward = 10;
const int RightMotorBackward = 11;

const int minDistance = 10;
const int maxDistance = 300;

void setup() {
  Serial.begin(9600);

  pinMode(RIGHT, INPUT_PULLUP);  // Try with pull-up resistor
  pinMode(LEFT, INPUT_PULLUP);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(RightMotorForward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);

  pinMode(LedLeft, OUTPUT);
  pinMode(LedRight, OUTPUT);
}

void loop() {
  long distance = read_cm();
  int Right_Value = digitalRead(RIGHT);
  int Left_Value = digitalRead(LEFT);

  Serial.print("Right Sensor = ");
  Serial.print(Right_Value);
  Serial.print(" | Left Sensor = ");
  Serial.print(Left_Value);
  Serial.print(" | Distance = ");
  Serial.print(distance);
  Serial.println(" cm");

  if ((Right_Value == 0) && (distance >= minDistance && distance <= maxDistance) && (Left_Value == 0)) {
    Serial.println("Moving Forward");
    forward();
  } else if ((Right_Value == 0) && (Left_Value == 1)) {
    Serial.println("Turning Right");
    turnRight();
  } else if ((Right_Value == 1) && (Left_Value == 0)) {
    Serial.println("Turning Left");
    turnLeft();
  } else if ((Right_Value == 0) && (Left_Value == 0)) {
    Serial.println("Stopping");
    stop();
  } else if (distance < minDistance) {
    Serial.println("Moving Backward");
    backward();
  }

  delay(50);
}

long read_cm() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long time = pulseIn(echoPin, HIGH, 30000); // 30 ms timeout
  if (time == 0) {
    return maxDistance + 1;
  }
  return time / 29 / 2;
}

void forward() {
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(LeftMotorBackward, LOW);
  LEDForwardSignal();
}

void backward() {
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
  LEDBackSignal();
}

void turnRight() {
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(LeftMotorBackward, LOW);

  digitalWrite(LedLeft, LOW);
  digitalWrite(LedRight, HIGH);
}

void turnLeft() {
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);

  digitalWrite(LedLeft, HIGH);
  digitalWrite(LedRight, LOW);
}

void stop() {
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, LOW);

  digitalWrite(LedLeft, LOW);
  digitalWrite(LedRight, LOW);
}

void LED