Projects / Two-motor bump robot

Two-motor bump robot

Beginner3.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO++

    Wire the driver supplies

    Pin 16 to 5V, pin 8 to battery plus, ground pins to GND.

  2. NANO

    Wire the enables and inputs

    D5, D6 to the enables, D7 to D10 to the inputs.

  3. NANO

    Connect the motors

    Left to pins 3 and 6, right to pins 11 and 14.

  4. NANO

    Wire the whisker switches

    D2 and D3 to GND.

  5. NANO

    Upload with the wheels off the ground

    Both wheels turn forward after three seconds.

  6. NANO

    Fix the wheel directions

    Swap a motor's wires if one spins backwards.

  7. NANO

    Press a whisker

    The robot backs up and turns away.

  8. NANO

    Try it on the floor

    It wanders and avoids walls.

In short

How do you build a two-motor bump robot?

A tiny robot drives forward and backs away when a whisker switch is pressed. You need Arduino Nano, L293D motor driver IC, TT gear motor 3 to 6 V × 2, Limit switch (micro switch with lever) × 2, 9 V battery, 9 V battery snap with DC jack and Breadboard 830 Tie Points. It takes 8 steps and about 210 minutes.

Level
Beginner
Time
About 210 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
L293D pin 16 (VCC1)Nano 5VLogic supply
L293D pin 8 (VCC2)9 V battery plusMotor supply, about 6 V reaches the motors after the chip's drop
L293D pins 4, 5, 12 and 13GND and battery minusCommon ground with the Nano
L293D pin 1 and pin 9 (enables)Nano D5 and D6PWM speed for left and right
L293D pins 2 and 7Nano D7 and D8Left motor direction
L293D pins 10 and 15Nano D9 and D10Right motor direction
Left motorL293D pins 3 and 6Left wheel
Right motorL293D pins 11 and 14Right wheel
Whisker switchesNano D2 and D3 to GNDLeft and right bump sensors

Code

The L293D is a motor driver with two H-bridges. For each motor, two logic inputs choose the direction, and an enable pin turns the motor on and sets its speed when fed with PWM. The chip has two supply pins: 5 volts for its logic and a separate pin for the motors. Its outputs lose roughly 1.4 volts on each side, so from a 9 volt battery the motors see about 6 volts, which suits small yellow gear motors. All the grounds must join: the battery, the driver and the Nano. The robot drives forward until a whisker switch touches an obstacle, then stops, backs up for a random time, and turns away from the side that was hit. The random times stop it from getting stuck in a loop in a corner. The built-in diodes in the L293D absorb motor voltage spikes. Test with the wheels off the ground first, check the directions, and swap a motor's two wires if it spins the wrong way. A 9 volt battery gives short running time with two motors; four AA cells in a holder last far longer. Do not run the motors from the Nano's 5 volt pin.

Runs on a virtual Arduino Nano 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 ENA = 5, IN1 = 7, IN2 = 8;      // left motor
const int ENB = 6, IN3 = 9, IN4 = 10;     // right motor
const int BUMP_L = 2, BUMP_R = 3;
const int SPEED = 180;                    // 0 to 255

void motor(int en, int a, int b, int speed) {   // speed from -255 to 255
  digitalWrite(a, speed > 0);
  digitalWrite(b, speed < 0);
  analogWrite(en, abs(speed));
}

void drive(int left, int right) {
  motor(ENA, IN1, IN2, left);
  motor(ENB, IN3, IN4, right);
}

void setup() {
  int pins[] = {ENA, IN1, IN2, ENB, IN3, IN4};
  for (int i = 0; i < 6; i++) pinMode(pins[i], OUTPUT);
  pinMode(BUMP_L, INPUT_PULLUP);
  pinMode(BUMP_R, INPUT_PULLUP);
  randomSeed(analogRead(A0));
  drive(0, 0);
  delay(3000);                            // time to put the robot on the floor
}

void loop() {
  bool left = (digitalRead(BUMP_L) == LOW);
  bool right = (digitalRead(BUMP_R) == LOW);

  if (left || right) {
    drive(0, 0); delay(100);
    drive(-SPEED, -SPEED); delay(random(400, 700));          // back up
    if (left && !right)      drive(SPEED, -SPEED);           // turn right, away from the left bump
    else if (right && !left) drive(-SPEED, SPEED);           // turn left
    else                     drive(random(2) ? SPEED : -SPEED, random(2) ? -SPEED : SPEED);
    delay(random(300, 700));
  } else {
    drive(SPEED, SPEED);                                      // cruise forward
  }
  delay(10);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Power onBoth wheels forward after a delay
Left whisker pressedBack up and turn right
Right whisker pressedBack up and turn left
Both pressedBack up and turn randomly
Battery lowSlow, weak driving

If it does not work

One wheel backwards

Swap that motor's two wires.

Motors do not turn

Check pin 8 has battery power and the grounds are joined.

Nano resets when motors start

Weak battery or shared supply, use a stronger battery.

Chip gets hot

Motors stalled or drawing too much, check the wheels spin freely.

Robot turns the wrong way on a bump

Swap the BUMP_L and BUMP_R pins.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the two-motor bump robot?

Arduino Nano, L293D motor driver IC, TT gear motor 3 to 6 V × 2, Limit switch (micro switch with lever) × 2, 9 V battery, 9 V battery snap with DC jack and Breadboard 830 Tie Points.

How long does the two-motor bump robot take to build?

About 210 minutes for a beginner maker, in 8 steps.

How do I know the two-motor bump robot works?

Power on: Both wheels forward after a delay. Left whisker pressed: Back up and turn right. Right whisker pressed: Back up and turn left.

One wheel backwards: what should I check?

Swap that motor's two wires.

Motors do not turn: what should I check?

Check pin 8 has battery power and the grounds are joined.

Nano resets when motors start: what should I check?

Weak battery or shared supply, use a stronger battery.

Chip gets hot: what should I check?

Motors stalled or drawing too much, check the wheels spin freely.