What You'll Build
In this project you'll build a small wheeled robot that can navigate on its own — without any remote control. When the robot detects an obstacle directly in front of it, it stops, briefly reverses, turns to find a clear path, and then continues moving forward.
It's a simple behavior, but it captures the essential loop that underlies almost every autonomous robot ever built: sense the environment, make a decision, act on it.
What You'll Learn
By completing this project you'll gain hands-on experience with several foundational robotics concepts:
- Sensors — how a robot measures the physical world
- Motors and motor control — how a robot converts electrical signals into movement
- Microcontrollers — the small computers that run your robot's logic
- Autonomous behavior — making decisions without human input
- The input → decision → output loop — the core pattern of robotics programming
- Basic embedded programming — writing code that runs directly on hardware
These concepts appear in robots ranging from simple hobby kits to industrial autonomous vehicles. Getting comfortable with them at this scale makes every more advanced project easier to understand.
Components You'll Need
This project uses common, widely available components. The exact models you choose will depend on your budget and what's available to you — the concepts apply regardless of brand.
- Microcontroller board (with digital I/O pins and a USB programming interface)
- Two-wheel robot chassis kit
- Two DC gear motors (usually included with the chassis)
- Motor driver module (to control motor direction and speed from the microcontroller)
- Ultrasonic distance sensor
- Battery holder and appropriate batteries
- Caster wheel (for balance at the rear)
- Jumper wires
- Mounting hardware (screws, standoffs, or zip ties)
Before purchasing, verify that your chosen microcontroller's operating voltage is compatible with your motor driver and sensor. Most beginner-friendly components operate at 3.3V or 5V logic — check the datasheets for each part.
How the Robot Works
Before you start building, it helps to understand the architecture. Your obstacle-avoiding robot is a simple closed-loop system:
DISTANCE SENSOR
↓
MICROCONTROLLER
↓
DECISION LOGIC
↓
MOTOR DRIVER
↓
LEFT MOTOR + RIGHT MOTOR
Here's what each layer does:
Distance Sensor
An ultrasonic sensor emits a short burst of sound and measures how long it takes to bounce back from an object. From that time, it calculates distance. The sensor outputs this measurement as a signal your microcontroller can read.
Microcontroller
The microcontroller is the brain. It reads the distance value from the sensor, runs your decision logic, and sends control signals to the motor driver. It does this in a continuous loop — typically hundreds or thousands of times per second.
Decision Logic
Your code defines a threshold distance. If the measured distance is greater than the threshold, the robot moves forward. If it's less, the robot stops, reverses, and turns. This is the entire intelligence of the robot — and it's enough to produce surprisingly capable behavior.
Motor Driver
A microcontroller's output pins can't supply enough current to drive motors directly. The motor driver acts as an amplifier: it takes low-power control signals from the microcontroller and uses them to switch higher-power current to the motors. It also lets you control motor direction by reversing the current flow.
Motors
DC gear motors convert electrical energy into rotation. By controlling each motor independently — speed and direction — you can make the robot go forward, reverse, turn left, or turn right.
Build the Chassis
Most two-wheel robot chassis kits include a platform, two motors, two wheels, and a caster wheel. Assembly is usually straightforward:
- Attach the two gear motors to the motor mounts on the chassis platform.
- Press or screw the drive wheels onto the motor shafts.
- Mount the caster wheel at the rear of the chassis. This provides a third contact point and keeps the robot level.
- Mount your microcontroller and motor driver on the chassis platform. Standoffs or double-sided foam tape both work well at this scale.
- Mount the ultrasonic sensor at the front of the chassis, facing forward, at roughly the height of the obstacles you expect to encounter.
- Secure the battery holder in a position that keeps the robot's weight roughly balanced front-to-back.
Keep wiring in mind as you place components — you'll want short, manageable wire runs between the sensor, microcontroller, motor driver, and battery.
Connect the Electronics
The wiring relationships are straightforward once you understand what each connection does.
Power
Your battery supplies power to two places: the motor driver (which powers the motors) and the microcontroller (which powers the logic). Many motor driver modules include a 5V regulated output you can use to power the microcontroller — check your specific module's documentation.
Sensor to Microcontroller
The ultrasonic sensor has four pins: power, ground, trigger, and echo. Connect power and ground to the appropriate rails. Connect the trigger pin to a digital output pin on your microcontroller, and the echo pin to a digital input pin. Your code will pulse the trigger and measure the echo duration.
Microcontroller to Motor Driver
The motor driver accepts control signals — typically direction pins and a PWM speed pin per motor channel. Connect these to digital output pins on your microcontroller. Your code will set these pins high or low to control each motor's direction and speed.
Motor Driver to Motors
Connect each motor's two terminals to the corresponding output terminals on the motor driver. Reversing these connections reverses the motor's direction — you'll use this during testing.
Always verify voltage and current ratings before connecting power. Mismatched voltages can damage components. When in doubt, consult the datasheet for each part.
Programming the Robot
The core program logic is a continuous loop. Here it is in pseudocode:
LOOP
measure distance from sensor
IF distance < threshold
stop both motors
reverse briefly
turn (run one motor forward, one backward)
ELSE
run both motors forward
END IF
END LOOP
In practice you'll translate this into whatever language your microcontroller supports. The structure stays the same regardless of platform.
A Few Implementation Notes
- Threshold distance — start with something like 20–30 cm and tune it based on your robot's speed and turning radius.
- Reverse duration — a brief reverse (200–400 ms) gives the robot room to turn without immediately re-detecting the same obstacle.
- Turn duration — experiment with how long to run the turn before resuming forward motion. A fixed-time turn is the simplest approach; you can make it smarter later.
- Motor speed — start slow. A robot moving at full speed has less time to react and is harder to debug.
Most microcontroller platforms have libraries that handle the low-level sensor timing and PWM generation for you. Use them — they save time and reduce bugs.
Testing
Test in stages rather than all at once. This makes problems much easier to isolate.
Step 1 — Test the Sensor Alone
Before connecting the motors, write a simple program that reads the sensor and prints the distance to your serial monitor. Hold your hand at various distances and confirm the readings are reasonable and consistent.
Step 2 — Test Each Motor Independently
Write a short program that runs each motor forward and backward for one second. Confirm each motor spins in the correct direction. If a motor runs backward, swap its two wires at the motor driver output terminals.
Step 3 — Test Forward Motion
Run both motors forward and confirm the robot drives in a straight line. If it curves, the motors may be running at slightly different speeds — adjust the PWM values until it tracks straight.
Step 4 — Test the Obstacle Response
Place an obstacle in front of the robot and confirm it stops and turns. Start with the robot stationary and trigger the response manually by holding your hand in front of the sensor.
Step 5 — Full Run
Let the robot run freely in an open area with a few obstacles. Observe its behavior and tune the threshold distance and turn duration until you're satisfied.
Troubleshooting
Here are the most common problems beginners encounter and how to address them.
Motors Running Backward
Swap the two motor wires at the motor driver output for the affected motor. No code change needed.
Robot Curves Instead of Driving Straight
The two motors are running at slightly different speeds. Reduce the PWM value on the faster motor until the robot tracks straight.
Inconsistent Distance Readings
Check that the sensor's power supply is stable. Electrical noise from the motors can interfere with sensor readings — add a small capacitor across the sensor's power pins, or add a short delay after changing motor state before taking a reading.
Robot Doesn't Have Enough Power
Fresh batteries make a significant difference. If the robot moves sluggishly or the microcontroller resets when the motors start, your power supply may not be providing enough current. Check that your battery capacity and motor driver ratings are appropriate for your motors.
Loose Wiring
Vibration from the motors can loosen jumper wire connections over time. If the robot behaves erratically, reseat all connections. Soldering critical connections improves reliability significantly.
Incorrect Motor Driver Connections
Double-check that your control pins are connected to the correct input pins on the motor driver. Refer to the motor driver's datasheet or pinout diagram.
Ways to Upgrade the Project
Once your obstacle-avoiding robot is working reliably, there are many natural directions to take it.
Better Sensing
- Add a second ultrasonic sensor facing to the side so the robot can choose which direction to turn based on which side has more clearance.
- Add infrared line-following sensors so the robot can follow a track on the floor.
- Replace the ultrasonic sensor with a time-of-flight sensor for faster, more accurate distance measurements.
Better Navigation
- Add wheel encoders to measure how far each wheel has traveled. This lets you make precise turns by angle rather than by time.
- Implement a simple wall-following algorithm instead of random obstacle avoidance.
Remote Control and Connectivity
- Add a Bluetooth or Wi-Fi module so you can send commands from a phone or computer.
- Build a simple web interface to monitor sensor data in real time.
More Capable Hardware
- Upgrade to a single-board computer such as a Raspberry Pi to run more complex software.
- Add a camera for basic computer vision — detecting colors, shapes, or faces.
ROS 2
Once you're comfortable with the fundamentals, ROS 2 (Robot Operating System 2) provides a professional-grade framework for building more sophisticated robots. The concepts you've learned here — sensors, actuators, decision loops — map directly onto ROS 2's node and topic architecture. Your obstacle-avoiding robot is a working mental model for understanding how ROS 2 nodes communicate.
What to Learn Next
This project touches the surface of several deep topics. Here are productive directions to explore next:
- Sensors — IMUs, encoders, lidar, cameras, and how sensor fusion combines multiple inputs
- Motor control — PID controllers, encoder feedback, and precise motion planning
- Embedded programming — interrupts, timers, real-time operating systems, and power management
- Computer vision — OpenCV, object detection, and visual navigation
- ROS 2 — the standard framework for professional and research robotics
- Autonomous navigation — SLAM (simultaneous localization and mapping), path planning, and obstacle maps
Each of these is a substantial field on its own. The robot you've just built gives you a concrete reference point for all of them.
Summary
You've built a robot that senses its environment, makes a decision, and acts — autonomously, in a continuous loop. That's not a toy concept. It's the same fundamental architecture used in self-driving vehicles, warehouse robots, and Mars rovers, scaled up in complexity but not in kind.
The most important thing this project teaches isn't any specific component or line of code. It's the habit of breaking a robot's behavior into layers — sensing, deciding, acting — and testing each layer independently before combining them. That discipline will serve you well regardless of how sophisticated your future projects become.
Keep the robot. Modify it. Break it and fix it. The best way to learn robotics is to have hardware in front of you that you built yourself.