DIY Follow Me Robot: Turn Your Arduino Obstacle-Avoiding Robot Into a Follow-Me Pet (5 Easy Steps)

Arduino UNO follow me robot using HC-SR04 ultrasonic sensor to detect hand distance at 20cm
The HC-SR04 ultrasonic sensor continuously measures the distance to your hand — the robot’s “eyes” for this project.

Introduction: Give Your Existing Robot a New Personality

What if you could take the same Arduino robot you built in our previous project, keep its sensors, motors, and wiring exactly as they are, and turn it into something completely different?

You can.

In our previous project, we built an Obstacle Avoiding Robot using an Arduino UNO, an HC-SR04 ultrasonic sensor, and an L298N motor driver. That robot used distance measurements to detect obstacles and steer itself safely away from them.

Now, in Post 7, we’re going to change only the programming logic and transform that same robot into a fun DIY Follow Me Robot Pet.

The idea is simple: the robot continuously measures the distance between its HC-SR04 ultrasonic sensor and your hand. When your hand moves too far away, the robot moves forward. When your hand comes too close, it moves backward. When your hand sits inside the programmed “Sweet Spot,” the robot stops.

No new sensor. No new motor. No new wiring — if you’ve already completed Post 5, your circuit is ready to go.

This is one of the most important lessons in robotics:

Hardware gives a robot its physical abilities. Software determines how those abilities are used.

If you’re starting directly with this project, you can still build the DIY follow me robot using the hardware and wiring instructions from our previous post, then return here for the new programming.

Why Ultrasonic Beats Infrared (IR) for This DIY Follow Me Robot Project

If you search around for DIY follow me robot tutorials, you’ll find two common hardware paths: basic Infrared (IR) obstacle sensors, or the Ultrasonic distance sensor we’re already using. Understanding why we’re sticking with ultrasonic is a great mini STEM lesson in itself.

Comparison diagram of infrared sensor binary detection versus ultrasonic sensor distance measurement for robotics
IR sensors give a simple yes/no signal; ultrasonic sensors measure exact distance — that’s why we use the HC-SR04 for this project.

The IR Sensor — “The Binary Switch” An IR module works like a simple light switch. It sends out a beam of invisible light; if that light bounces back off your hand, it tells the robot, “Yes, something is there!” If nothing bounces back, it says, “No.” Because it can’t measure how far away your hand is, a robot built on IR tends to react crudely — jerking forward at full power the instant it senses you, often bumping straight into your hand.

The Ultrasonic Sensor — “The Spatial Radar” The HC-SR04 works more like a bat’s echolocation, or our own depth perception. It calculates the sensor’s precise distance to your hand in centimeters, in real time. That continuous stream of measurement data lets us program a much more nuanced “personality matrix” — instead of a blunt yes/no reaction, the robot can make smooth decisions based on exactly how close or far away you are, creating the illusion of a living, thinking companion.

That’s the foundation of a genuinely interesting Arduino DIY Follow Me Robot — instead of:

Object detected → Do something

here we get:

Less than 10 cm  → Move backward
10–18 cm         → Stop (Sweet Spot)
19–35 cm         → Move forward
More than 35 cm  → Stop

Note: this project uses a single front-facing HC-SR04, so it responds to distance only — it doesn’t detect whether a target has moved left or right. More advanced human-tracking robots typically add extra sensors for that. Think of this as a distance-based Follow Me Robot Pet, not a full human-tracking system.

Step 1 — Reuse Your Existing Robot Hardware

The biggest advantage of this project is that you don’t need to rebuild anything if you’ve already completed the Obstacle Avoiding Robot project. Keep the same:

The circuit does not change for DIY Follow Me Robot. Only the Arduino program changes :

Wiring diagram of DIY Follow Me Robot showing Arduino UNO, HC-SR04 ultrasonic sensor, and L298N motor driver with IN1–IN4 connected to digital pins 9, 8, 7, and 6
Complete wiring layout for the DIY Follow Me Robot — no changes needed if you’ve already wired the Obstacle Avoiding Robot from Post 6.

Existing Pin Configuration

ComponentArduino Pin
HC-SR04 Trig11
HC-SR04 Echo12
Motor A (left) forward — IN19
Motor A (left) backward — IN28
Motor B (right) forward — IN37
Motor B (right) backward — IN46

The HC-SR04 is especially useful here because it doesn’t just tell the Arduino that an object exists — it lets the program calculate an approximate distance by timing how long an ultrasonic pulse takes to bounce back.

Step 2 — Understand How the Robot “Thinks”

Before opening the Arduino IDE, let’s understand the decision-making process. The robot isn’t actually “thinking” like a person — the Arduino simply repeats a measurement and checks it against a set of programmed conditions.

For this project, we’ve defined four distance zones:

Distance measured by HC-SR04Robot behaviour
0 cm or above 35 cmStop
Below 10 cmMove backward
10–18 cmStop (Sweet Spot)
Above 18 cm, up to 35 cmMove forward

The “Sweet Spot”

When your hand sits within 10–18 cm, the robot stops. Move your hand farther away, and the robot moves forward. Move it too close, and the robot moves backward. This simple system is an excellent, hands-on way to introduce children to conditional programming.

The “Gravity Tether” : A 3-Minute Screen-Free Game

Diagram of Gravity Tether game showing red, green, and blue distance zones for DIY Follow Me Robot Sweet Spot
The three zones that power the robot’s decision logic: Red = too close, Green = Sweet Spot, Blue = too far.

Before writing a single line of code, let’s act like the robot ourselves.

  1. Stand about 20 cm away from your child. This is the Comfort Zone — the Sweet Spot.
  2. Take one slow step toward your child. You’re now too close. Your instruction: “Move backward!”
  3. Return to the comfortable distance. Your instruction: “Stop!”
  4. Take two steps away. You’re now too far. Your instruction: “Move forward!”

Congratulations — you’ve just acted out an if – else if – else program!

This simple unplugged activity helps children understand that the robot isn’t moving randomly. It’s repeatedly checking a condition and choosing the appropriate response — exactly the nested conditional logic running inside the Arduino.

Step 3 — Program Your Arduino DIY Follow Me Robot

Now it’s time to give your robot its new behavior. We’re using the exact same Arduino UNO, HC-SR04, and L298N wiring from Post 5 — pins 11 (Trig) and 12 (Echo) for the sensor, and pins 9, 8, 7, 6 (IN1–IN4) for the motor driver. Only the code changes.

Complete Arduino Code:

/*
 ========================================================================
 STEM Series Post 7: The Interactive "DIY Follow Me" Robot Pet
 Target Hardware: Arduino UNO, HC-SR04 Ultrasonic, L298N Motor Driver
 ========================================================================
*/

// --- PIN CONFIGURATIONS ---
const int TRIG_PIN = 11;
const int ECHO_PIN = 12;

// Left Motor Control Pins (L298N) - Motor A
const int LEFT_MOTOR_FORWARD = 9;   // IN1
const int LEFT_MOTOR_BACKWARD = 8;  // IN2

// Right Motor Control Pins (L298N) - Motor B
const int RIGHT_MOTOR_FORWARD = 7;  // IN3
const int RIGHT_MOTOR_BACKWARD = 6; // IN4

void setup() {
  // Initialize Serial Monitor for real-time distance tracking
  Serial.begin(9600);

  // Set Pin Modes
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(LEFT_MOTOR_FORWARD, OUTPUT);
  pinMode(LEFT_MOTOR_BACKWARD, OUTPUT);
  pinMode(RIGHT_MOTOR_FORWARD, OUTPUT);
  pinMode(RIGHT_MOTOR_BACKWARD, OUTPUT);

  // Start with motors stopped
  stopMotors();
}

void loop() {
  // Get the current distance reading
  long distance = readDistanceCM();

  // Print distance to Serial Monitor
  Serial.print("Pet Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // --- COMPANION BEHAVIOUR MATRIX ---
  if (distance == 0 || distance > 35) {
    // Target is too far away or missing
    stopMotors();
  }
  else if (distance > 0 && distance < 10) {
    // Too close - move backward
    moveBackward();
  }
  else if (distance >= 10 && distance <= 18) {
    // Sweet Spot - stop
    stopMotors();
  }
  else if (distance > 18 && distance <= 35) {
    // Target is farther away - move forward
    moveForward();
  }

  delay(60);
}

// --- MOTOR CONTROLS ---
void moveForward() {
  digitalWrite(LEFT_MOTOR_FORWARD, HIGH);
  digitalWrite(LEFT_MOTOR_BACKWARD, LOW);
  digitalWrite(RIGHT_MOTOR_FORWARD, HIGH);
  digitalWrite(RIGHT_MOTOR_BACKWARD, LOW);
}

void moveBackward() {
  digitalWrite(LEFT_MOTOR_FORWARD, LOW);
  digitalWrite(LEFT_MOTOR_BACKWARD, HIGH);
  digitalWrite(RIGHT_MOTOR_FORWARD, LOW);
  digitalWrite(RIGHT_MOTOR_BACKWARD, HIGH);
}

void stopMotors() {
  digitalWrite(LEFT_MOTOR_FORWARD, LOW);
  digitalWrite(LEFT_MOTOR_BACKWARD, LOW);
  digitalWrite(RIGHT_MOTOR_FORWARD, LOW);
  digitalWrite(RIGHT_MOTOR_BACKWARD, LOW);
}

// --- ULTRASONIC DISTANCE FUNCTION ---
long readDistanceCM() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH, 30000);
  long distanceCM = duration * 0.034 / 2;
  return distanceCM;
}

How the Robot Measures Distance

Before the robot can decide anything, it first needs to know how far away your hand is. That’s the job of the readDistanceCM() function. It sends a short ultrasonic pulse from the Trig pin, then uses Arduino’s built-in pulseIn() function to measure how long the Echo pin stays HIGH while the pulse travels to your hand and bounces back. That time is then converted into centimeters using the speed of sound.

How the Main Decision Logic Works

The most important part for a beginner isn’t the entire sketch — it’s this decision block:

  • if (distance == 0 || distance > 35) — If the target is missing or farther than 35 cm, the robot stops.
  • else if (distance > 0 && distance < 10) — If the target is closer than 10 cm, the robot moves backward.
  • else if (distance >= 10 && distance <= 18) — If the target is inside the Sweet Spot, the robot stops.
  • else if (distance > 18 && distance <= 35) — If the target is between 19 and 35 cm away, the robot moves forward.

This is a simple but powerful example of distance sensing + conditional logic + motor control working together.

Step 4 — Test and Calibrate Your Robot Pet

Once the program has uploaded successfully, disconnect the USB cable and place the robot on a smooth floor. Switch it on and give it some space.

Test 1 — Move Your Hand to About 30 cm Hold your open hand roughly 30 cm in front of the HC-SR04. That’s inside the 19–35 cm range. Expected behaviour: the robot moves forward toward you.

Test 2 — Enter the Sweet Spot Keep your hand about 10–18 cm from the sensor. Expected behaviour: the robot stops. This is its programmed comfort zone.

Test 3 — Move Your Hand Too Close Slowly bring your hand closer than 10 cm. Expected behaviour: the robot moves backward, trying to restore distance.

Test 4 — Move Your Hand Too Far Away Move your hand beyond 35 cm. Expected behaviour: the robot stops. This is intentional — the program is designed to stay near its target rather than chase something indefinitely.

How to Use the Serial Monitor (For First-Time Users)

The Serial Monitor is a built-in tool in the Arduino IDE that lets you see live data your DIY follow me robot is sending back to your computer — in this case, the distance readings from the HC-SR04 sensor.

  1. Connect your Arduino to your computer using the USB cable (keep it connected for this step — don’t disconnect it like you do for normal robot operation).
  2. Open the Arduino IDE and make sure your Follow Me Robot code is uploaded.
  3. Click the Serial Monitor icon — it’s the magnifying-glass icon in the top-right corner of the Arduino IDE window (or go to Tools → Serial Monitor from the menu).
  4. Check the baud rate at the bottom-right of the Serial Monitor window. It must be set to 9600, matching Serial.begin(9600); in the code. If it’s set to a different number, the text will look like garbled symbols.
  5. Move your hand in front of the sensor. You’ll see live text scrolling like:
    • Pet Distance: 25 cm
    • Pet Distance: 18 cm
    • Pet Distance: 9 cm
  6. Use these numbers to debug. If the robot isn’t behaving as expected, compare what the Serial Monitor shows against the distance zones from Step 2 — this tells you whether the sensor is misreading distance, or whether the motors aren’t responding correctly to a correct reading.
    Note: The robot’s motors will still run while the USB cable is connected and Serial Monitor is open — so hold the robot steady or place it somewhere it can’t drive off your desk while testing this way.

Step 5 — Troubleshoot and Tune Your DIY Follow Me Robot

Even with correct code, a physical robot can behave differently depending on motors, wheels, floor surface, battery condition, and ultrasonic reflections. Here are the most common issues.

Problem 1: The Robot Moves Backward Instead of Forward If the robot moves opposite to what you expect, the motor direction is likely reversed. Look at the moveForward() and moveBackward() functions and swap the HIGH/LOW combination for the affected motor. This is a quick software fix — not a reason to rebuild the robot.

Problem 2: The Robot Shakes Around the Sweet Spot If your hand sits near the 18 cm boundary, the HC-SR04 may report jittery values like 18 → 19 → 18 → 19 cm, causing the robot to rapidly flicker between Stop and Forward. Widen the Sweet Spot to smooth this out:

else if (distance >= 10 && distance <= 22) {

This changes the stopping range from 10–18 cm to 10–22 cm. Experiment to find what works best for your robot.

Problem 3: The Robot Keeps Moving Even When Your Hand Is Gone Ultrasonic waves can bounce off nearby walls, furniture, table legs, floors, or other objects. Test in a more open area, and make sure the HC-SR04 points parallel to the floor.

Problem 4: The Robot Doesn’t Move Reliably Check: battery charge, motor connections, L298N connections, wheel movement, Arduino connections, HC-SR04 wiring, and motor direction. Watch the Serial Monitor — the program prints Pet Distance: XX cm in real time, which makes it a great debugging and learning tool.

Frequently Asked Questions

Can I use an Arduino Nano or Mega instead of an Arduino UNO?

Yes. The logic is identical — just make sure your wiring matches the pin numbers used in the code, or update the pin definitions accordingly. For this tutorial, we recommend sticking with the UNO configuration from the previous project so you can reuse the same wiring.

Does this robot really “follow” a person?

It uses a single front-facing HC-SR04 to respond to the distance of whatever is directly in front of it — it doesn’t detect left/right movement. So it’s best described as a distance-based Follow Me Robot Pet rather than a full human-tracking robot. More advanced designs add extra sensors for directional tracking.

Can I use an IR sensor instead of the HC-SR04?

You could build a different robot around IR sensors, but that changes both the hardware and the programming approach. This project intentionally reuses the HC-SR04 from Post 6 so kids can see how changing software alone can create a new robot behaviour.

What is the maximum distance this robot can follow?

The program currently uses 35 cm as the upper tracking threshold. You can experiment with a larger value (up to roughly 100 cm), but tracking accuracy depends on the HC-SR04, your target’s size and surface, the environment, and the robot’s mechanics. Keeping the range short makes the interaction easier to observe for younger learners.

Is this safe for young children to play with?

Yes. The robot runs on low voltage (typically 6V–9V from AA batteries) and stops completely inside the Sweet Spot or when it loses its target, making it safe for hands-on play. Adult supervision is still recommended when handling batteries, wiring, and moving parts — keep fingers, loose clothing, and cables clear of the wheels and gears.

What Did We Actually Learn?

This may look like a simple robot game, but it teaches several genuinely important robotics concepts. Your child has worked with:

  • Ultrasonic distance sensing
  • Arduino programming
  • Conditional logic (if – else if – else)
  • Motor control
  • Sensor data interpretation
  • Distance thresholds
  • Calibration
  • Debugging
  • Hardware/software interaction

Most importantly, the project demonstrates something fundamental: you don’t always need new hardware to create a new robot — sometimes a new idea comes from new software.

Conclusion: Same Robot, Completely New Behaviour

In our previous project (Post 5), the robot used its ultrasonic sensor to avoid obstacles. Now, using the exact same Arduino UNO, HC-SR04 sensor, L298N motor driver, motors, and wiring, we’ve created a completely different behaviour — simply by changing the instructions inside the Arduino.

The robot now moves forward when its target is too far → stops inside the Sweet Spot → moves backward when the target gets too close.

That’s one of the most important lessons in robotics: hardware gives a robot its body — software gives it behaviour. And that’s exactly why programming is so powerful. A child doesn’t just learn to copy an Arduino code example; they begin to understand that changing a few conditions in software can completely change what a physical machine does.

Coming Next: From Robot Decisions to Digital Logic

So far, our Arduino has been making decisions using sensor measurements and programmed conditions. But what happens before a computer or microcontroller makes those decisions?

In Post 8, we’ll put the microcontroller aside and build an interactive, screen-free “mechanical computer circuit” using simple household switches — exploring the basic ideas behind digital logic gates and discovering how simple ON/OFF states combine to create surprisingly powerful digital processing.

The robot taught us how software can control hardware. Now let’s discover how the basic building blocks of digital systems make decisions in the first place.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top