Build Your First Robot: Step-by-Step Obstacle-Avoiding Project
Build a two-wheeled obstacle-avoiding robot in one weekend with common, low-cost parts. Includes parts list, wiring, full Arduino code and troubleshooting.
This guide takes you from zero to a working obstacle-avoiding robot in one weekend. Every part is a common hobby component sold by electronics shops and online stores worldwide. The whole kit is inexpensive, and many sellers bundle these parts together.
Final result
A two-wheeled robot that drives forward, detects obstacles with an ultrasonic sensor, and turns to avoid them. Battery-powered, no laptop needed once you upload the code.
Parts list
| Part | Quantity | Notes |
|---|---|---|
| Arduino Uno (or compatible) | 1 | A clone works fine |
| L298N motor driver | 1 | Drives the two motors |
| Gear motor + wheel pair | 1 (2 motors) | Often sold as "BO motors" or "TT motors" |
| HC-SR04 ultrasonic sensor | 1 | Measures distance |
| SG90 servo motor | 1 | Turns the sensor left and right |
| 2-wheel chassis | 1 | Acrylic kits include a caster wheel |
| Jumper wires (M-M, M-F) | 1 pack | |
| 9V battery + clip | 1 |
Buy everything from one seller to save on shipping. Many shops sell this exact set as a single "smart car kit".
Tools you need
A small Phillips screwdriver, scissors, double-sided foam tape, a USB-A to USB-B cable (the kind old printers use). That's it. No soldering required.
Step 1 — Build the chassis (30 min)
Most acrylic chassis kits come with peel-off film on both sides. Remove the film, screw the two motors to the underside of the bottom plate using the M3 screws and brackets included. Add the caster wheel at the back. Snap the wheels onto the motor shafts. The chassis is now done — you have a rolling platform.
Step 2 — Mount the electronics (15 min)
Place the Arduino on top of the chassis using double-sided foam tape. Place the L298N motor driver next to it. Mount the ultrasonic sensor on the SG90 servo (some kits include a small bracket; otherwise foam tape works), then mount the servo at the front of the chassis facing forward.
Step 3 — Wire it up
Motors to L298N:
- Left motor wires → L298N OUT1, OUT2 (either polarity — we'll fix direction in code)
- Right motor wires → L298N OUT3, OUT4
L298N to Arduino:
- IN1 → Arduino pin 5
- IN2 → Arduino pin 6
- IN3 → Arduino pin 9
- IN4 → Arduino pin 10
- ENA and ENB → leave the jumper caps ON (uses full speed)
Ultrasonic to Arduino:
- VCC → 5V
- GND → GND
- TRIG → pin 7
- ECHO → pin 8
Servo to Arduino:
- Orange (signal) → pin 11
- Red (5V) → 5V
- Brown (GND) → GND
Power:
- 9V battery + → L298N 12V input
- 9V battery − → L298N GND
- L298N 5V output → Arduino VIN (so one battery powers both)
- Common GND between Arduino and L298N
Double-check GND is shared between Arduino and L298N. This is the #1 mistake.
Step 4 — Upload the code
Open Arduino IDE on your laptop. Paste this code:
#include <Servo.h>
Servo head;
const int TRIG = 7, ECHO = 8;
const int IN1 = 5, IN2 = 6, IN3 = 9, IN4 = 10;
long cm() {
digitalWrite(TRIG, LOW); delayMicroseconds(2);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long us = pulseIn(ECHO, HIGH, 30000);
return us == 0 ? 999 : us / 58;
}
void drive(int l, int r) {
digitalWrite(IN1, l > 0); digitalWrite(IN2, l < 0);
digitalWrite(IN3, r > 0); digitalWrite(IN4, r < 0);
}
void setup() {
pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
head.attach(11); head.write(90);
delay(500);
}
void loop() {
long d = cm();
if (d > 25) {
drive(1, 1); // clear → forward
} else {
drive(0, 0); delay(200);
head.write(30); delay(400);
long right = cm();
head.write(150); delay(400);
long left = cm();
head.write(90); delay(300);
if (right > left) { drive(1, -1); delay(350); }
else { drive(-1, 1); delay(350); }
}
}
Connect Arduino to your laptop via USB. In Arduino IDE, select Tools → Board → Arduino Uno and Tools → Port → (your COM port). Click Upload.
Step 5 — Disconnect and run
Once uploaded, unplug the USB cable. Connect the 9V battery. The robot should start driving forward, stop when something is within 25 cm, look right and left with its sensor, then turn toward the open side.
Common problems and fixes
"Motors don't move at all" — check that L298N's 12V input is wired correctly and battery has charge. Also, the ENA/ENB jumper caps must be ON.
"Motors spin wrong direction" — swap IN1↔IN2 (or IN3↔IN4) in code, OR swap the two motor wires on the L298N. Either works.
"Robot just spins in circles" — one motor is reversed. Same fix as above.
"Ultrasonic reads weird numbers" — check VCC and GND are firmly seated. Loose ground = noise. Move TRIG/ECHO wires away from motor wires.
"Servo jitters constantly" — undervoltage. The L298N's 5V output sometimes can't drive a servo while motors run. Solution: power the servo from Arduino's 5V (not L298N) or use a separate 5V power bank.
"Robot drives backward, not forward" — flip the chassis around. Joke. But also, swap which motor pair is IN1/IN2 vs IN3/IN4 in the code.
Extend the project
Once it works:
- Add Bluetooth with an HC-05 module → control from your phone
- Add a buzzer that beeps when an obstacle is detected
- Tune the avoidance — change the 25cm threshold, the turn time, the speed
- Add a line-follower mode with two IR sensors → robot follows a black line
Why this matters
You just built a real working robot. It has sensors, a controller, actuators, and feedback control. Robots from planetary rovers to warehouse carts use the same basic loop you just programmed: sense, decide, act.
Test it virtually first
If you want to test the logic before building, R2BOT's browser simulator runs the same loop — you can experiment with turn timing and thresholds without burning components.
Try the in-browser simulator → · Learn every concept used here →
Sources
- SparkFun, HC-SR04 ultrasonic distance sensor (2 cm to 4 m): https://www.sparkfun.com/products/15569
- Arduino, Servo library reference: https://docs.arduino.cc/libraries/servo/
- Arduino, built-in examples: https://docs.arduino.cc/built-in-examples/
- SparkFun, "Motors and Selecting the Right One": https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one
Was this article helpful?
Learn deeper
Master every concept mentioned in this article — for free.
R2BOT's Atlas has 500+ robotics concepts explained in simple language, with sources.
Explore R2BOT Atlas →Plain English. Free. Unsubscribe in one click.