Build it step by step
0 of 8 doneAssemble the chassis
Motors, wheels, caster, holder.
Wire the L298N and Uno
As in the table, wheels in the air.
Test the motors alone
Check each wheel direction.
Add the ultrasonic sensor
Print distances first.
Add the two IR sensors
Trim them for about 30 cm.
Upload the follow sketch
Try it in your hand first.
Test on the floor
Walk slowly in front.
Tune the speeds and distances
Keep them gentle.
How do you build a robot that follows you?
An ultrasonic sensor in the middle and IR sensors on the sides let a small robot follow a hand or a person at a set distance. You need Arduino Uno R3, HC-SR04 Ultrasonic Sensor, IR obstacle avoidance sensor module × 2, L298N Dual H-Bridge Module, Robot car chassis kit 2WD (acrylic), Battery holder 18650 2S (series), 18650 cell 2600 mAh (branded) × 2 and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 150 minutes.
- Level
- Intermediate
- Time
- About 150 min
- Parts
- 8
Wiring
| From | To | Why |
|---|---|---|
| 18650 holder (2 cells in series) plus | L298N 12V terminal | Motor supply, 7.4 to 8.4 V |
| 18650 holder minus | L298N GND and Uno GND | Common ground |
| L298N 5V terminal | Uno 5V pin | Powers the Uno, keep the regulator jumper on |
| Uno D5, D7, D8 | L298N ENA, IN1, IN2 | Left motor |
| Uno D6, D9, D10 | L298N ENB, IN3, IN4 | Right motor |
| HC-SR04 TRIG, ECHO | Uno D2, D3 | Front distance |
| Left IR sensor OUT | Uno D11 | Goes LOW when it sees you |
| Right IR sensor OUT | Uno D12 | Goes LOW when it sees you |
| All sensors VCC and GND | Uno 5V and GND | Power |
Code
This robot keeps a polite distance. The ultrasonic sensor measures how far away you are. Between 30 and 100 cm the robot drives forward faster the farther you are (proportional control), inside 30 cm it holds still, and inside 15 cm it backs away. The two IR obstacle sensors on the front corners tell it which side you are on: when you are on the left, the left wheel slows so the robot curves toward you. If it cannot see you ahead but one side sensor can, it spins towards that side, and if nothing is seen it stops, which is the safe default. The speeds are kept modest so it cannot chase anyone fast, and you should always test it with the wheels in the air first. IR sensors work poorly in direct sunlight and on black clothes, and ultrasonic sensors can miss thin legs, so expect it to lose you sometimes; stop-on-nothing is deliberate. Keep it away from stairs, pets and small children. Adjust the little trimmer on each IR module until it detects you at about 30 cm. Power from two 18650 cells in a holder through the L298N, which also supplies 5 V to the Uno (leave its regulator jumper on). Use protected cells and never leave them charging unattended. Setup: Arduino IDE, board "Arduino Uno".
Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
const uint8_t TRIG = 2, ECHO = 3, IR_L = 11, IR_R = 12;
const uint8_t ENA = 5, IN1 = 7, IN2 = 8, ENB = 6, IN3 = 9, IN4 = 10;
const int STOP_NEAR = 15, HOLD_MIN = 30, FOLLOW_MAX = 100; // distances in cm
const int SPEED_MIN = 110, SPEED_MAX = 200; // PWM, kept modest on purpose
int readCm() {
digitalWrite(TRIG, LOW); delayMicroseconds(2);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
unsigned long us = pulseIn(ECHO, HIGH, 15000UL);
return us ? us / 58 : 999;
}
void motor(uint8_t en, uint8_t a, uint8_t b, int speed) { // speed -255 to 255
digitalWrite(a, speed > 0);
digitalWrite(b, speed < 0);
analogWrite(en, abs(speed));
}
void drive(int left, int right) { motor(ENA, IN1, IN2, left); motor(ENB, IN3, IN4, right); }
void setup() {
pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
pinMode(IR_L, INPUT); pinMode(IR_R, INPUT);
uint8_t outs[] = {ENA, IN1, IN2, ENB, IN3, IN4};
for (uint8_t p : outs) pinMode(p, OUTPUT);
drive(0, 0);
}
void loop() {
int d = readCm();
bool left = !digitalRead(IR_L); // modules are active low
bool right = !digitalRead(IR_R);
if (d < STOP_NEAR) { // too close: back away slowly
drive(-SPEED_MIN, -SPEED_MIN);
} else if (d <= HOLD_MIN) { // in the sweet spot: hold position
drive(0, 0);
} else if (d <= FOLLOW_MAX) { // follow, faster the farther you are
int v = map(d, HOLD_MIN, FOLLOW_MAX, SPEED_MIN, SPEED_MAX);
int l = v, r = v;
if (left && !right) { l = v / 3; } // you are on the left: slow the left wheel to turn left
if (right && !left) { r = v / 3; }
drive(l, r);
} else if (left && !right) { // lost ahead but seen on one side: spin towards it
drive(-SPEED_MIN, SPEED_MIN);
} else if (right && !left) {
drive(SPEED_MIN, -SPEED_MIN);
} else {
drive(0, 0); // nothing seen: stop
}
delay(40);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Hand 20 cm in front | Robot holds still |
| Hand at 60 cm | Robot drives forward at medium speed |
| Hand 10 cm away | Robot backs off |
| Hand moved to the left side | Robot curves left |
| Remove everything | Robot stops |
If it does not work
Robot spins or twitches
Motors wired backwards; swap the pair on that side.
Never follows
Check the HC-SR04 pins and that you are within 100 cm.
Chases a wall
Ultrasonic sees the wall; keep the area clear or lower FOLLOW_MAX.
IR always on
Trimmer too sensitive; turn it down.
Uno resets when motors start
Weak cells or a loose connection; fully charge the cells.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the robot that follows you?
Arduino Uno R3, HC-SR04 Ultrasonic Sensor, IR obstacle avoidance sensor module × 2, L298N Dual H-Bridge Module, Robot car chassis kit 2WD (acrylic), Battery holder 18650 2S (series), 18650 cell 2600 mAh (branded) × 2 and Jumper wires male to male (40 pcs, 20 cm).
How long does the robot that follows you take to build?
About 150 minutes for a intermediate maker, in 8 steps.
How do I know the robot that follows you works?
Hand 20 cm in front: Robot holds still. Hand at 60 cm: Robot drives forward at medium speed. Hand 10 cm away: Robot backs off.
Robot spins or twitches: what should I check?
Motors wired backwards; swap the pair on that side.
Never follows: what should I check?
Check the HC-SR04 pins and that you are within 100 cm.
Chases a wall: what should I check?
Ultrasonic sees the wall; keep the area clear or lower FOLLOW_MAX.
IR always on: what should I check?
Trimmer too sensitive; turn it down.
