Bump-switch whisker robot with Arduino: design worksheet and code

A ₹1,350 design worksheet for a contact-sensing robot with two lever microswitches that backs away from obstacles, including glass and soft ones, with a pin map, a sketch and a test plan.

About the author · Sources, testing and AI policy

  • Parts₹1,350
  • Bench time2–3 h
  • LevelBeginner
  • ReviewedNov 2026

A ₹1,350 bump-switch robot that feels its way around with whiskers. Two lever microswitches sit at the front corners. When one is pressed by a wall or a chair leg, the robot backs up and turns away from that side. There is no distance sensor to fool, so it works on glass, black surfaces and soft obstacles that confuse ultrasonic and infrared sensors. The page includes the pin map, a complete sketch with a stuck-in-a-corner escape, and a test plan.

Scope: beginner. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. It touches the obstacle before it reacts, so keep the speed low and the robot light. It is the best first lesson in contact sensing, and a useful backup layer on a robot that also has a distance sensor.

How it behaves

Each switch connects its pin to ground when pressed, and the Uno’s internal pull-up resistor holds the pin high otherwise, so no extra resistors are needed. The robot drives forward until a switch reads low. It then stops, reverses for a short timed step and turns away from the pressed side. If both switches are pressed together it treats that as a head-on hit and turns the full way around. A counter spots repeated bumps in a short time and runs a longer escape turn, because a robot in a corner can otherwise bounce between two walls for ever.

Bill of materials

PartQtyUnitTotal
Arduino Uno R31₹520₹520
L298N motor driver1₹180₹180
Lever microswitch2₹15₹30
TT gear motor with wheel2₹110₹220
Acrylic chassis1₹80₹80
2×18650 holder with cells1₹200₹200
Caster wheel and screws1₹40₹40
Jumper wire set1₹50₹50
Switch1₹30₹30
Parts total₹1,350

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings. Lever microswitches are sold in packs, so you will have spares.

Pin map

PartArduino UnoNote
Left microswitchD2Between the pin and GND; uses the internal pull-up
Right microswitchD4Between the pin and GND; uses the internal pull-up
L298N IN1 / IN2D5 / D6Left motor direction
L298N IN3 / IN4D12 / D13Right motor direction
L298N ENA / ENBD3 / D11PWM speed. Remove the onboard jumpers.
L298N +5V5V pinPowers the Uno from the driver’s regulator
All groundsCommon GNDBattery, L298N, Uno and both switches
Use the normally-open contact of each switch. Wire each switch between its pin and ground, nothing else.

Mounting the whiskers

  1. Fix each microswitch at a front corner so the lever sticks out ahead of the chassis by a few centimetres and angles slightly outward.
  2. Extend a short lever with a stiff wire or a strip of plastic if you want more reach. A longer lever gives the robot more time to stop, but is easier to catch on things.
  3. Check by hand that a light touch on the lever closes the switch, and that the lever springs back.
  4. Tidy the switch wires so they cannot be caught by a wheel or the lever.

The sketch

// Bump-switch robot: Uno + L298N + 2 lever microswitches
// Design worksheet: tune the timings on your own chassis.

const int LEFT_SWITCH = 2;
const int RIGHT_SWITCH = 4;

const int IN1 = 5;
const int IN2 = 6;
const int IN3 = 12;
const int IN4 = 13;
const int ENA = 3;
const int ENB = 11;

const int SPEED = 130;
const int TURN_SPEED = 140;
const unsigned long BACK_MS = 350;
const unsigned long TURN_MS = 450;
const unsigned long WINDOW_MS = 4000;   // bumps inside this window count as "stuck"

int recentBumps = 0;
unsigned long windowStart = 0;

void setup() {
  pinMode(LEFT_SWITCH, INPUT_PULLUP);
  pinMode(RIGHT_SWITCH, INPUT_PULLUP);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  stopMotors();
  delay(1500);
  windowStart = millis();
}

void loop() {
  bool leftHit = digitalRead(LEFT_SWITCH) == LOW;
  bool rightHit = digitalRead(RIGHT_SWITCH) == LOW;

  if (!leftHit && !rightHit) {
    forward();
    return;
  }

  stopMotors();
  delay(80);

  if (millis() - windowStart > WINDOW_MS) {
    windowStart = millis();
    recentBumps = 0;
  }
  recentBumps++;

  backward();
  delay(BACK_MS);

  unsigned long turnTime = TURN_MS;
  if (leftHit && rightHit) {
    turnTime = TURN_MS * 2;           // head-on: turn further
  }
  if (recentBumps >= 3) {
    turnTime = TURN_MS * 3;           // repeated bumps: likely a corner
    recentBumps = 0;
  }

  if (leftHit && !rightHit) {
    turnRight();
  } else {
    turnLeft();
  }
  delay(turnTime);
  stopMotors();
  delay(80);
}

void forward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, SPEED);
  analogWrite(ENB, SPEED);
}

void backward() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  analogWrite(ENA, SPEED);
  analogWrite(ENB, SPEED);
}

void turnLeft() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, TURN_SPEED);
  analogWrite(ENB, TURN_SPEED);
}

void turnRight() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  analogWrite(ENA, TURN_SPEED);
  analogWrite(ENB, TURN_SPEED);
}

void stopMotors() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
}

The sketch reads the switches directly and uses blocking delays, which is fine for a robot that does one thing at a time. Contact switches bounce for a few milliseconds; the 80 ms stop before reacting absorbs that. If a left hit makes the robot turn left instead of away, swap the two turn calls or the motor leads.

Test plan

  1. Switch check: with the motors disconnected, press each lever and watch a test print show the pin go low. Confirm the lever returns cleanly.
  2. Motor direction: confirm forward, backward and both turns with the wheels off the ground.
  3. Wall hit: drive head-on at a wall, then at a shallow angle. Record which switches trigger and where the robot goes.
  4. Corner: put the robot in a corner and count how many bumps it takes to escape.
  5. Soft and glass obstacles: try a cushion and a glass door. Contact sensing should work where distance sensors fail.
  6. Thin obstacle: approach a chair leg between the whiskers. A gap in the coverage means the levers need to reach further across.
  7. Speed: raise SPEED slowly and note the speed at which the impact starts to push the robot off line or damage the lever.

What usually goes wrong

SymptomLikely causeFix
Switch never reads pressedWired to the wrong contact or lever not reachingUse the normally-open contact, adjust the lever
Always reads pressedSwitch wired normally-closed, or a short to groundRewire, check for a pinched wire
Triggers randomlyVibration on a loose lever or noise on long wiresFix the switch firmly, shorten wires
Turns into the obstacleTurn direction reversedSwap the turn calls or motor leads
Bounces between two wallsNo escape ruleKeep the repeated-bump escape turn
Whisker catches on thingsLever too long or angled badlyShorten it and angle it outward

Upgrades

  • Add a distance sensor: use the bumpers as a last-resort layer under the ultrasonic scan in the obstacle avoiding robot.
  • Rear bumper: add a third switch so the robot notices when it hits something while reversing.
  • Interrupts: use the pins’ interrupts so a bump is noticed mid-turn.
  • Random turns: vary the turn time slightly so the robot does not repeat the same path.

Cost and parts notes

The ₹1,350 total is an illustration for the parts above and does not include shipping. The L298N guide explains the voltage drop that makes a 7.4 V pack behave like a 5.8 V one at the motors. Use the cost calculator to compare sellers.

Common questions

Why use INPUT_PULLUP?

It turns on a resistor inside the Uno that holds the pin high, so the switch only has to connect the pin to ground. That saves two external resistors and a lot of wiring.

Why would I choose bump switches over an ultrasonic sensor?

Bump switches are cheap, need no calibration and work on glass, black and soft surfaces. They only detect an obstacle after touching it.

Can I read the switches with interrupts?

Yes, on D2 and D3 of the Uno. This build uses simple polling, which is easier to read and fast enough at low speed.

Will it hurt furniture?

At low speed and light weight, unlikely, but keep the speed down and test on a clear floor first.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; timings and speeds are starting values, and measurements from your build are welcome.

More builds

Next builds to try

Free: the final-year project pack

A shortlist with BOMs, a report outline that matches the usual chapter scheme, a costing sheet and 40 viva questions. Built for the February–April submission season.

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.