Robotics
How to make a robot bump and turn with a bumper switch
A bumper switch is the simplest obstacle sensor a robot can have: a bar across the front with a switch behind it. Wired between an Arduino pin and ground with INPUT_PULLUP, the pin reads HIGH normally and LOW when the robot hits something. The code watches for LOW and runs an escape routine: stop, pause, pivot away, go.
Wiring it
Use a lever microswitch, or a limit switch, behind a light bar that spans the robot's nose, so a hit anywhere along the front presses it. One leg to pin 2, the other to GND:
void setup() {
pinMode(2, INPUT_PULLUP); // HIGH when free, LOW on a bump
}
The pull-up resistor inside the chip holds the pin HIGH. Pressing the switch joins the pin to ground, which wins, so a bump reads LOW. No extra resistor needed.
The escape routine
With the left wheel on pin 9 and the right on pin 10:
void loop() {
if (digitalRead(2) == LOW) { // bumped
analogWrite(9, 0);
analogWrite(10, 0);
delay(1000); // stop, and stay stopped
analogWrite(9, 255); // pivot right: left wheel only
delay(500);
}
analogWrite(9, 255); // drive on
analogWrite(10, 255);
}
The if runs the routine once per bump. The two lines after it run on every pass: they're what drives the robot in the first place, and what sets it going again once the routine is over.
Why the pause matters
A wheel takes about a fifth of a second to spin down. Stop for only 50 ms and the robot is still rolling when the pivot starts, so it turns with its nose still shoving the wall. Stop for a full second and it comes to rest, then turns cleanly.
Live experiment
Drive into the wall
The rover drives at the wall until its bumper closes and pin 2 reads LOW. Then the escape: both wheels off, a stop delay, the left wheel alone for 500 ms, then both on. With a 50 ms stop the wheels are still spinning down when the pivot starts; with 1000 ms it stops, waits and turns cleanly.
“Bump and turn” is lesson 3 of 6 in Rover Ridge, Robotics World 1. Free with an account. Lessons unlock in order, so you’ll do the 2 before it first.
Half a second of pivot turns this rover about 60°, so a wall hit head on takes two bumps to get clear of. Many robots back up a little before turning, which needs motor drivers that can reverse.
The catch
While those delay() calls run, the program does nothing else: it can't read the bumper, a line sensor or anything else. For a one-second escape that's fine. For a robot that must keep watching while it does something timed, swap the delays for millis() timestamps.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Bump and turn” is lesson 3 of 6 in Rover Ridge, Robotics World 1. Free with an account. Lessons unlock in order, so you’ll do the 2 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: The bumper is pressed: the switch joins the pin to ground. The pull-up holds the pin HIGH on its own. Pressing the bumper connects it to GND, which wins, and the read goes LOW. Free again, the pull-up takes over and it's HIGH.
- Why not “Always”? It only reads LOW while the switch is closed. The rest of the time the pull-up lifts it HIGH.
- Why not “The bumper is free”? Free, nothing joins the pin to ground, so the pull-up holds it HIGH.
- Why not “Never”? Pressing the bumper connects the pin straight to ground, which beats the weak pull-up, so it does read LOW.
Show the answer
Answer: The pause: the wheels are still spinning down when the pivot starts. A wheel takes about a fifth of a second to spin down. 50 ms is over long before that, so the rover goes straight from slowing down into the pivot, still shoving at the wall. With 1000 ms it stops, stands still, then turns.
- Why not “The stop”? Both wheels are told to stop. The trouble is that 50 ms ends before they've finished slowing.
- Why not “The pivot”? The pivot still runs for its half second. It just starts while the rover is still rolling forward.
- Why not “Go”? The rover still drives on after the routine. It's the standstill in the middle that's lost.
Show the answer
Answer: After the if, so they run every pass, including right after the routine. The routine ends with the left wheel on and the right off. The forward lines after the if put both back on, every pass, so the rover drives on until the next bump.
- Why not “Nowhere: the wheels remember”? Without them nothing starts the wheels, and after a bump the routine leaves only the left wheel on, so the rover would spin for ever.
- Why not “Inside the if”? Then nothing drives the rover before its first bump, so it never moves and never bumps.
- Why not “In setup()”? setup() runs once. After the first escape leaves only the left wheel on, nothing would turn the right one back on.
“Bump and turn” is lesson 3 of 6 in Rover Ridge, Robotics World 1. Free with an account. Lessons unlock in order, so you’ll do the 2 before it first.
Questions people ask
How do I wire a bumper switch to an Arduino?
Put a microswitch, the kind with a little lever, behind a bar across the robot's front. Wire one side to a digital pin and the other to GND, and set the pin to INPUT_PULLUP. The pin reads HIGH normally and LOW while the bumper is pressed.
Why does my robot scrape along the wall after a bump?
Its stop is too short. Wheels take a fraction of a second to spin down, so a 50 ms stop is over before the robot has stopped moving, and the pivot starts with it still pushing forward. Stop for most of a second before turning.
Can the robot read its bumper during the escape routine?
Not if the routine uses delay(), which stops the whole program until it's over. That's fine for a short escape. If the robot must react during it, time each step with millis() instead.
Bumper switch or distance sensor?
Both, usually. A distance sensor sees obstacles coming and steers round them; a bumper catches what the sensor misses, like a chair leg too thin to see or a dark surface that reads as far away. Robot vacuums carry both.
Related topics
- What is a pull-up resistor?Why an input pin needs a pull-up, and why a pressed button reads LOW.
- How an obstacle-avoiding robot worksLook ahead, slow down near walls, and turn away with a gap between the thresholds.
- millis() vs delay() on ArduinoWhy delay() freezes your sketch, and how millis() runs several jobs at once.
- How a two-wheeled robot steers and sets its speedHow a two-wheeled robot turns with no steering wheel, and how PWM sets each motor's speed.