Final project: an obstacle-avoiding robot
Everything from the roadmap comes together here: a sensor that measures, a microcontroller that decides, a driver that supplies power and two motors. Build it in 3D, test it on the track, then make it for real.
Loading the 3D scene
What you need
No prices: they change by store and country. Many "2-wheel robot" kits already include the chassis, motors, wheels and battery holder.
| Qty | Part | What for |
|---|---|---|
| 1 | Arduino Uno (or compatible) with its USB cable | The brain: it runs the code. |
| 1 | TB6612FNG driver (or L298N) | Supplies power to the motors. |
| 2 | TT gear motors with wheels | They move the robot; one per side. |
| 1 | Caster wheel | Holds up the back. |
| 1 | Acrylic chassis | The base everything goes on. |
| 1 | HC-SR04 sensor | Measures the distance to whatever is ahead. |
| 1 | 4 × AA battery holder (6 V) with switch | Powers only the motors. |
| 1 | USB power bank | Powers the Arduino, separate from the motors. |
| 1 | Mini breadboard and jumper wires | To connect without soldering. |
The wiring
| From | To | Why |
|---|---|---|
| HC-SR04 VCC / GND | Arduino 5V / GND | Powers the sensor. |
| HC-SR04 Trig | Arduino pin 7 | The Arduino asks it to measure. |
| HC-SR04 Echo | Arduino pin 8 | The sensor returns the echo time. |
| TB6612FNG VCC | Arduino 5V | The driver’s logic. |
| TB6612FNG VM | Battery holder + | The motors’ power. |
| TB6612FNG GND | Arduino GND and battery holder − | Common ground: without it, the signals don’t make sense. |
| AIN1, AIN2, PWMA | Pins 2, 4 and 5 | Direction and speed of the left motor. |
| BIN1, BIN2, PWMB | Pins 10, 11 and 6 | Direction and speed of the right motor. |
| STBY | Pin 12 | When HIGH, the driver works. |
| AO1 / AO2 and BO1 / BO2 | Left and right motor | Power output to each motor. |
The code
// Obstacle-avoiding robot: Arduino Uno + TB6612FNG + HC-SR04
const int TRIG = 7, ECHO = 8;
const int AIN1 = 2, AIN2 = 4, PWMA = 5; // left motor
const int BIN1 = 10, BIN2 = 11, PWMB = 6; // right motor
const int STBY = 12; // driver standby
void setup() {
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
int pins[] = {AIN1, AIN2, PWMA, BIN1, BIN2, PWMB, STBY};
for (int p : pins) pinMode(p, OUTPUT);
digitalWrite(STBY, HIGH); // wakes the driver up
Serial.begin(9600);
}
long distanceCm() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH); // 10 µs pulse: "measure"
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long us = pulseIn(ECHO, HIGH, 30000); // waits for the echo up to 30 ms
if (us == 0) return 400; // no echo: the way is clear
return us / 58; // µs / 58 = cm
}
// speed from -255 (backward) to 255 (forward)
void motor(int in1, int in2, int pwm, int speed) {
digitalWrite(in1, speed > 0);
digitalWrite(in2, speed < 0);
analogWrite(pwm, abs(speed));
}
void drive(int left, int right) {
motor(AIN1, AIN2, PWMA, left);
motor(BIN1, BIN2, PWMB, right);
}
void loop() {
long d = distanceCm();
Serial.println(d);
if (d < 20) {
drive(-150, -150); // back up
delay(300);
drive(150, -150); // turn in place
delay(350);
} else {
drive(180, 180); // forward
}
delay(50);
}- distanceCm() does the same as the HC-SR04 lab: a 10 µs pulse, wait for the echo and divide by 58.
- motor() uses the driver’s H-bridge: one pin per direction and PWM for speed.
- If a motor spins backward, don’t change the code: swap its two wires on the driver.
If something fails
- The Arduino resets when the motors start
- The motors and the Arduino share batteries. Power the Arduino over USB and the motors from the battery holder.
- The motors don’t move
- Check that STBY is HIGH, the battery switch is on and the grounds are joined.
- The sensor always reads 0 or 400
- Trig and Echo may be swapped, or the sensor is pointing at something soft or angled.
- The robot spins in circles
- One motor is faster than the other. Lower the faster one’s speed a little in drive().
- It goes backward when it should go forward
- Swap the two wires of that motor on the driver.
Before connecting the batteries, read the safety section. See safety and common failures.