Tap a part to see what it does.
Tap the parts to see what each one does. A teaching simulation, not a circuit simulator.
Build it step by step
0 of 5 doneBuild the chassis
Motors, wheels, caster and battery holder on the base.
Driver and power
Battery to VM, Uno 5V to VCC and STBY, grounds joined.
Motors
Left to AO1/AO2, right to BO1/BO2, capacitor across each.
Sensor at the front
Trig D11, Echo D12, as level as you can.
Test on blocks first
Wheels off the ground, then on the floor.
How do you build an obstacle-avoiding robot?
A two-wheel robot drives forward until the ultrasonic sensor sees something within 25 cm, then stops, backs up and turns away. You need Arduino Uno R3, HC-SR04 Ultrasonic Sensor, TB6612FNG Motor Driver, TT gear motor 3 to 6 V × 2, Ceramic Capacitor 100 nF × 2 and Breadboard 830 Tie Points. It takes 5 steps and about 120 minutes.
- Level
- Advanced
- Time
- About 120 min
- Parts
- 6
Wiring
| From | To | Why |
|---|---|---|
| 4 × AA pack | TB6612 VM, − to common GND | Motor supply |
| Uno 5V | TB6612 VCC and STBY | Logic |
| D5, D7, D8 | PWMA, AIN1, AIN2 | Left motor |
| D6, D9, D10 | PWMB, BIN1, BIN2 | Right motor |
| HC-SR04 Trig / Echo | D11 / D12 | Distance |
Code
No libraries. Tune the speeds and turn time for your chassis. If it turns the wrong way, swap that motor's two wires.
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 int PWMA = 5, AIN1 = 7, AIN2 = 8, PWMB = 6, BIN1 = 9, BIN2 = 10, TRIG = 11, ECHO = 12;
void motor(int pwm, int in1, int in2, int speed) {
digitalWrite(in1, speed > 0); digitalWrite(in2, speed < 0);
analogWrite(pwm, abs(speed));
}
void drive(int l, int r) { motor(PWMA, AIN1, AIN2, l); motor(PWMB, BIN1, BIN2, r); }
long cm() {
digitalWrite(TRIG, LOW); delayMicroseconds(2);
digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW);
long t = pulseIn(ECHO, HIGH, 25000);
return t ? t / 58 : 999;
}
void setup() {
for (int p : {PWMA, AIN1, AIN2, PWMB, BIN1, BIN2, TRIG}) pinMode(p, OUTPUT);
pinMode(ECHO, INPUT);
delay(2000); // time to put it on the floor
}
void loop() {
if (cm() > 25) { drive(170, 170); delay(50); return; }
drive(0, 0); delay(150);
drive(-150, -150); delay(400);
drive(160, -160); delay(350); // spin away
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Wheels lifted | Both spin forward; hand in front makes them stop, reverse and turn |
| On the floor towards a wall | Stops 20 to 25 cm away and turns |
| Battery change | Behaviour the same; slower if cells are low |
If it does not work
Spins in circles
One motor wired backwards: swap its wires.
Uno resets on start
Motors on the Uno's 5 V: use the battery for VM.
Hits chair legs
Thin objects reflect poorly; mount the sensor higher or add a second one.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the obstacle-avoiding robot?
Arduino Uno R3, HC-SR04 Ultrasonic Sensor, TB6612FNG Motor Driver, TT gear motor 3 to 6 V × 2, Ceramic Capacitor 100 nF × 2 and Breadboard 830 Tie Points. Ceramic Capacitor 100 nF × 2: One across each motor. Breadboard 830 Tie Points: A half-size board fits a chassis better.
How long does the obstacle-avoiding robot take to build?
About 120 minutes for a advanced maker, in 5 steps.
How do I know the obstacle-avoiding robot works?
Wheels lifted: Both spin forward; hand in front makes them stop, reverse and turn. On the floor towards a wall: Stops 20 to 25 cm away and turns. Battery change: Behaviour the same; slower if cells are low.
Spins in circles: what should I check?
One motor wired backwards: swap its wires.
Uno resets on start: what should I check?
Motors on the Uno's 5 V: use the battery for VM.
Hits chair legs: what should I check?
Thin objects reflect poorly; mount the sensor higher or add a second one.
