Skip to content
RobotVisual

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.

QtyPartWhat for
1Arduino Uno (or compatible) with its USB cableThe brain: it runs the code.
1TB6612FNG driver (or L298N)Supplies power to the motors.
2TT gear motors with wheelsThey move the robot; one per side.
1Caster wheelHolds up the back.
1Acrylic chassisThe base everything goes on.
1HC-SR04 sensorMeasures the distance to whatever is ahead.
14 × AA battery holder (6 V) with switchPowers only the motors.
1USB power bankPowers the Arduino, separate from the motors.
1Mini breadboard and jumper wiresTo connect without soldering.

The wiring

FromToWhy
HC-SR04 VCC / GNDArduino 5V / GNDPowers the sensor.
HC-SR04 TrigArduino pin 7The Arduino asks it to measure.
HC-SR04 EchoArduino pin 8The sensor returns the echo time.
TB6612FNG VCCArduino 5VThe driver’s logic.
TB6612FNG VMBattery holder +The motors’ power.
TB6612FNG GNDArduino GND and battery holder −Common ground: without it, the signals don’t make sense.
AIN1, AIN2, PWMAPins 2, 4 and 5Direction and speed of the left motor.
BIN1, BIN2, PWMBPins 10, 11 and 6Direction and speed of the right motor.
STBYPin 12When HIGH, the driver works.
AO1 / AO2 and BO1 / BO2Left and right motorPower output to each motor.

The code

Robot sketch (Arduino IDE)
// 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.