Arduino Servilliance Mobile Robot

by engr_abdulbasit in Circuits > Arduino

20 Views, 1 Favorites, 0 Comments

Arduino Servilliance Mobile Robot

Picture1.jpg

Want to make an Arduino UNO basic Security Servilliance robot lets Make it:

Supplies

IR.jpg
Lazer.jpg
PIR.jpg
ultra.jpg
body.jpg
FK8W1IUMDG8UL6L.jpg

These are the Basic componets that you have to make this Robot Accurately some are Optional

just like IR sensor and ultrasonic that are might not be very useful but in some cases they are needfull and required.

Having These Components

Driver.jpg
FO2K3JWMDG8ULCZ.jpg

The Most Basic Components are : the 2 Whells 2 Motors and One chess Mobile Robot Body just like this image

That make the Structure of the Robot also having a one Motor driver in our case H bridge we Use.

Lets Wiring the Robot to Operate

FULA40XMDG8UL51.jpg

After that wiring the Robot accurately and perfectly i also shared the complete mechanism see how to pins connect of laser Diode with Arduino and PIR sensor pinout with Arduino now connect both also connect the IR senosors and Ultrasonic sensor with Arduino wired up now lets jump to the made up logic behind this and do a actual connecting communicating between these components by Programming !

Understand Mechanism

1. System Initialization:

The Arduino powers up and initializes all connected components.

2. Random Movement:

The robot moves in random directions (forward, left, right, U-turn) using 2 DC motors controlled by Arduino.

3. Obstacle Detection:

The ultrasonic sensor constantly checks for obstacles ahead.

If an obstacle is detected within a certain distance, the robot changes direction to avoid collision.

4. Night Operation with IR Sensor:

The IR sensor helps in detecting objects in low-light or dark environments.

It ensures the robot can continue obstacle detection or line detection even at night.

5. Motion Detection:

The PIR motion sensor monitors for human or object movement.

On detecting motion:

  1. An LED inside the house turns ON to alert presence.
  2. The laser module activates and points a beam toward the moving object.

6. Continuous Operation:

The robot loops through these functions continuously, providing 24/7 patrolling and motion-based alerting.

Do a Program to Robot and Its Operate in Real World

Robotics Project Demo

So here is the Code


#include <SoftwareSerial.h>

// === Pin Definitions ===
const int IR_NIGHT_PIN = A0;
const int BEAM_TRIP_PIN = A1;
const int PIR_PIN = 12;
const int LASER_PIN = 4;
const int ALERT_LED_PIN = 3;

const int TRIG_PIN = 7;
const int ECHO_PIN = 8;

const int IN1 = 6, IN2 = 9, IN3 = 10, IN4 = 11;

int motorSpeed = 180;

// Thresholds
int nightThreshold = 600;
int obstacleDistance = 25;

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

pinMode(IR_NIGHT_PIN, INPUT);
pinMode(BEAM_TRIP_PIN, INPUT);
pinMode(PIR_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);

pinMode(LASER_PIN, OUTPUT);
pinMode(ALERT_LED_PIN, OUTPUT);

pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);

digitalWrite(LASER_PIN, LOW); // Laser OFF initially
digitalWrite(ALERT_LED_PIN, LOW); // LED OFF initially
stopMotors();

Serial.println("Night Patrol Robot Initialized");
}

void loop() {
int irValue = analogRead(IR_NIGHT_PIN);
bool motionDetected = digitalRead(PIR_PIN) == HIGH;

// --- Handle Motion Detection (PIR) ---
if (motionDetected) {
digitalWrite(ALERT_LED_PIN, HIGH);
digitalWrite(LASER_PIN, HIGH);
} else {
digitalWrite(ALERT_LED_PIN, LOW);
digitalWrite(LASER_PIN, LOW);
}

// --- Operate only in dark (night mode) ---
if (irValue <= nightThreshold) {
moveForwardAvoidingObstacles(5000); // Move forward for 5 seconds
makeUTurn();
moveForwardAvoidingObstacles(5000); // Move forward again for 5 seconds
makeUTurn();
} else {
stopMotors(); // Daytime - stop
}
}

// === Move Forward With Obstacle Avoidance ===
void moveForwardAvoidingObstacles(unsigned long duration) {
unsigned long startTime = millis();
while (millis() - startTime < duration) {
int distance = getUltrasonicDistance();
Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm");

if (distance > 0 && distance <= obstacleDistance) {
stopMotors();
backward(); delay(400);
turnRight(); delay(400);
stopMotors();
} else {
forward();
}

// Also handle motion while moving
bool motionDetected = digitalRead(PIR_PIN) == HIGH;
digitalWrite(ALERT_LED_PIN, motionDetected ? HIGH : LOW);
digitalWrite(LASER_PIN, motionDetected ? HIGH : LOW);
delay(50);
}
stopMotors();
}

// === U-turn ===
void makeUTurn() {
Serial.println("Making U-Turn...");
turnRight(); delay(800); // Adjust timing for ~180 degree turn
stopMotors(); delay(200);
}

// === Get Distance from Ultrasonic Sensor ===
int getUltrasonicDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

long duration = pulseIn(ECHO_PIN, HIGH, 30000);
return duration ? (duration * 0.034 / 2) : obstacleDistance + 1;
}

// === Motor Controls ===
void forward() {
analogWrite(IN1, motorSpeed);
analogWrite(IN2, 0);
analogWrite(IN3, motorSpeed);
analogWrite(IN4, 0);
}

void backward() {
analogWrite(IN1, 0);
analogWrite(IN2, motorSpeed);
analogWrite(IN3, 0);
analogWrite(IN4, motorSpeed);
}

void turnLeft() {
analogWrite(IN1, 0);
analogWrite(IN2, motorSpeed);
analogWrite(IN3, motorSpeed);
analogWrite(IN4, 0);
}

void turnRight() {
analogWrite(IN1, motorSpeed);
analogWrite(IN2, 0);
analogWrite(IN3, 0);
analogWrite(IN4, motorSpeed);
}

void stopMotors() {
analogWrite(IN1, 0);
analogWrite(IN2, 0);
analogWrite(IN3, 0);
analogWrite(IN4, 0);
}


Lets jump to the Final Resuls here :