How to Build a Human Following Robot with Arduino and Sensors

We will build a simple Human Following Robot using Arduino, an Adafruit Motor Shield, an ultrasonic sensor, and IR sensors. The robot will detect and follow a person based on their position relative to the sensors.


Components Required

ComponentDescription
Arduino UNOMicrocontroller board
Motor Driver HW-130To drive 4 DC motors
4 x DC MotorsFor the robot wheels
HC-SR04 Ultrasonic SensorFor obstacle detection and distance measurement
2 x IR SensorsFor detecting the person’s direction (left and right)
Jumper wiresFor connections
Battery PackTo power the robot
Chassis and WheelsBase structure of the robot

Hardware Setup

1. Motors and Motor Shield

  • Mount the Adafruit Motor Shield on top of the Arduino UNO.
  • Connect the 4 DC motors to the motor shield ports M1, M2, M3, and M4.
  • This shield controls the motors’ speed and direction easily.

2. Ultrasonic Sensor (HC-SR04)

  • Connect Trig pin to Arduino analog pin A4.
  • Connect Echo pin to Arduino analog pin A5.
  • Connect VCC and GND to 5V and GND respectively.

3. IR Sensors (Left and Right)

  • Connect left IR sensor output to Arduino analog pin A2.
  • Connect right IR sensor output to Arduino analog pin A3.
  • Connect VCC and GND to 5V and GND.

Arduino Code:

#include <AFMotor.h>

// Motors: M1–M4 on motor shield
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

// Sensor pins
#define trigPin A4
#define echoPin A5
#define irLeftPin A2
#define irRightPin A3

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

  // Set initial motor speeds
  motor1.setSpeed(150);
  motor2.setSpeed(150);
  motor3.setSpeed(150);
  motor4.setSpeed(150);

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

  pinMode(irLeftPin, INPUT);
  pinMode(irRightPin, INPUT);
}

void loop() {
  int distance = getDistance();

  bool leftDetected = digitalRead(irLeftPin) == LOW;  // assuming IR active LOW
  bool rightDetected = digitalRead(irRightPin) == LOW;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print(" cm, IR Left: ");
  Serial.print(leftDetected);
  Serial.print(", IR Right: ");
  Serial.println(rightDetected);

  if (distance < 20) {  // Obstacle too close
    stopMotors();
    delay(200);
    moveBackward();
    delay(500);
    stopMotors();
    delay(200);
  }
  else if (leftDetected && !rightDetected) {
    // Object detected on left: turn right to follow
    turnRight();
  }
  else if (rightDetected && !leftDetected) {
    // Object detected on right: turn left to follow
    turnLeft();
  }
  else if (leftDetected && rightDetected) {
    // Object centered, move forward
    moveForward();
  }
  else {
    // No object detected, stop or slowly move forward
    stopMotors();
  }

  delay(100);
}

// Ultrasonic distance measurement
int getDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH, 30000);  // timeout 30ms
  if (duration == 0) return 999; // no echo

  return duration * 0.034 / 2;
}

void moveForward() {
  motor1.setSpeed(180);
  motor2.setSpeed(180);
  motor3.setSpeed(180);
  motor4.setSpeed(180);

  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

void moveBackward() {
  motor1.setSpeed(150);
  motor2.setSpeed(150);
  motor3.setSpeed(150);
  motor4.setSpeed(150);

  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}

void turnLeft() {
  motor1.setSpeed(180);
  motor2.setSpeed(180);
  motor3.setSpeed(180);
  motor4.setSpeed(180);

  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
  delay(600);
  stopMotors();
}

void turnRight() {
  motor1.setSpeed(180);
  motor2.setSpeed(180);
  motor3.setSpeed(180);
  motor4.setSpeed(180);

  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
  delay(600);
  stopMotors();
}

void stopMotors() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

How the Robot Works

  • The ultrasonic sensor helps the robot avoid obstacles by measuring the distance ahead.
  • The two IR sensors detect the human or object’s presence to the left or right.
  • Based on IR inputs, the robot turns toward the detected direction, following the person.
  • If the person is centered (both IR sensors detect), it moves forward.
  • If the path is blocked, the robot reverses briefly.
  • If no object is detected, the robot stops to save power.

Tips for Improvement

  • Use more IR sensors or add a camera for better human tracking.
  • Adjust motor speeds and delays to improve smoothness.
  • Add Bluetooth or remote control for manual override.
  • Integrate obstacle avoidance algorithms for complex environments.

Conclusion

Building a human-following robot is an exciting project combining sensors, motors, and microcontroller programming. This setup provides a solid foundation for creating autonomous robots that can interact with their environment.

Feel free to customize the code and hardware for your specific needs!