How a robot decides what to do at a closed door
Pip meets a closed door
Pip is a small robot about the size of a shoebox. It has two rubber wheels, a round bumper on the front, and two little round "eyes" above the bumper that are really sensors. Its job today is to carry a box of crayons from the kitchen to the art room at the end of the hallway.

Halfway down the hallway, Pip rolls up to a door. The door is closed. Pip stops with its bumper a few centimeters from the wood, and it waits.

You would know what to do right away. You might push the door, knock, or walk around through another room. Pip can't do any of those things unless someone has told it how. A robot has no ideas of its own and doesn't guess the way people do. Everything it does comes from instructions that a person wrote ahead of time, plus small facts it picks up about the world around it.
So the real question is what Pip was told to do in a moment like this one. By the end of this class you will be able to answer it yourself, and you'll get to predict Pip's moves before they happen.
A program tells a robot what to do next
A program is a list of exact steps that a robot follows one at a time, in order. You already know something like it. A recipe for pancakes says to crack the eggs, add the milk, stir, and then pour the batter into the pan. If you pour before you stir, you get lumpy pancakes.

Pip's program for the crayon trip looks a lot like a recipe.
- Pick up the crayon box.
- Turn to face the hallway.
- Roll forward 10 steps.
- Turn left.
- Roll forward 5 steps.
- Put the box down.
Pip does exactly what each step says, nothing more and nothing less. That makes robots very reliable, and it also makes them a little silly when the list has a mistake in it.
Imagine someone deletes step 1 by accident. Pip still turns, rolls, and turns again, and then it carefully "puts down" a box it never picked up. The crayons are still sitting on the kitchen table. Pip never notices, because nothing in its program says to check.
Order matters just as much. If steps 3 and 4 swap places, Pip turns left first and then rolls forward 10 steps, straight into the kitchen wall. Every step was right, but the order was wrong, so the whole trip went wrong. People who write programs spend a lot of time hunting for mistakes like these. They even have a name for them, bugs, which means errors hiding in a program.

- Program version
- Correct Recipe
- Swapped Steps 3 & 4
- Top-down room grid with Pip, a wall and a table
- Ready
- Run program
- Reset
- Pick up the crayon box
- Turn to face the hallway
- Roll forward 10 steps
- Turn left
- Roll forward 5 steps
- Put the box down
- Running
- Target Reached
- Wall Hit!
- Wall
- Table
Bump sensors notice when something is touched
A program alone would leave Pip blind. To find out what's happening around it, a robot uses sensors, which are parts that notice one thing about the world, like a touch, a sound, or how bright it is. The fact a sensor hands to the robot is called an input, which simply means information coming in.
The simplest sensor is a bump sensor. Pip's front bumper is a curved strip of plastic on a tiny spring. Behind it sits a little switch, like the button on a doorbell. When the bumper hits something, it slides back a few millimeters and presses the switch. The switch sends one tiny message to Pip's brain: "I touched something."
- Cutaway schematic of a bump sensor: a bumper compresses a spring and presses a microswitch, sending a signal to the circuit board
- CLICK
- PIP
- mm
You may have seen this in action at home. A robot vacuum cleaning the kitchen rolls under the table until its bumper taps a chair leg. Tock. The switch clicks, the vacuum stops, turns a little, and tries a new direction. It might tap the same chair three times before it finds a way around, but it keeps going without ever getting stuck pushing against the leg.

A bump sensor only tells the robot about something after it has already touched it. That works for a soft chair leg. It isn't so good when the thing ahead is a person's foot or a glass vase, and that's why many robots also carry a sensor that can feel ahead without touching.
Distance sensors bounce sound and light to see ahead
A distance sensor tells a robot how much empty space is in front of it. Pip's two round "eyes" are really one of these, an ultrasonic sensor. One eye sends out a quick squeak of sound that is too high for people to hear, and the other eye listens for the echo.
The trick works like shouting in a big empty gym. When the wall is far away, your echo takes a moment to come back. When the wall is close, it comes back almost right away. Pip's sensor times the echo, and that tells Pip how far away the wall is. Bats find bugs in the dark in much the same way.
- Ultrasonic sensor sending sound rings toward a wooden barrier and receiving echo rings
- Ultrasonic distance sensor
- Distance to barrier
- cm
- Echo round trip
- ms
- Barrier position
- Barrier distance in centimeters
- Pip
- Chirp
- Echo
Some robots use light instead of sound. An infrared sensor shines an invisible beam of light and measures how much of it bounces back. A close object reflects a lot of light, and a far one reflects only a little.
Plenty of toy robots use this. Hold your hand in front of one, and as your palm gets closer, the sensor sees less and less empty space. When the space gets too small, the toy rolls backward, as if it's shy. It never touched you, because it "saw" your hand coming.

This is how Pip stopped a few centimeters from the closed door instead of crashing into it. Its sound sensor noticed the space shrinking and gave the input "something is very close."
Light sensors follow paths on the floor
Some sensors point down instead of forward. A light sensor measures how bright the surface under it is. A white floor bounces back lots of light, and a thick black line bounces back very little.
A line-following car uses this to drive along a road drawn on paper. Picture a sheet of white poster board with a curvy black marker line on it, and a little car with a light sensor peeking down at the front. While the sensor sees dark, the car is on the line and keeps rolling. When the line curves and the sensor suddenly sees bright white, the car knows it has drifted off and turns until it finds the dark line again.

It drives in tiny wiggles, left, then right, then left again, but it follows the line all the way around. Robots in some big warehouses steer the same way, following stripes or markers on the floor to bring boxes to the right shelf.
- Left sensor
- Right sensor
- Dark
- Bright
Pip has a light sensor too. Under its belly, it watches for the blue tape that marks the path to the art room. Pip also has a small microphone, which is a sound sensor, so it can hear a sound like a knock. Each sensor notices just one kind of thing, so a robot that needs to know a lot carries several.
If this happens, then do that
Sensors hand the robot facts, and the program turns those facts into actions. The part of the program that chooses is a decision, a moment where the robot picks between two actions depending on what is true. Programmers write decisions as if-then rules.
An if-then rule works like a rule you follow on the way to school. If the crossing light is red, then stop. Otherwise, walk across. You check one fact and the fact picks your action.
Delivery robots that roll along sidewalks in some cities use rules just like this. When one of them reaches a street crossing, its program checks the signal and the cars around it. If the way isn't clear, then it waits at the curb. Otherwise, it rolls across. Nobody pushes a button for it each time, because the rule was written ahead of time and it is ready whenever the robot reaches a crossing.

Pip's program has rules too. Here are three of them.
- If the bumper is pressed, then back up a little.
- If something is closer than 10 centimeters, then stop. Otherwise, keep rolling.
- If the floor under me is blue, then keep following the tape.
Try predicting Pip's next move with these rules. Pip is rolling down the hallway and a cat walks right in front of it, very close. Pip stops, because the distance rule says so. Now the cat walks away and the space ahead is open again. Pip rolls forward, because the "otherwise" part of the rule is now the one that is true.
- Pip the robot and an obstacle on a hallway floor
- cm
- Otherwise
- Keep rolling
- If distance < 10 cm
- Stop
- Obstacle distance
- Obstacle distance in centimeters
Robots check their rules over and over, many times every second. That's how they can react so quickly when something changes.
Putting it all together to clear the hallway
Now we can watch Pip at the closed door, one moment at a time. Every robot repeats the same three-part loop, called sense, think, act. It senses the world, thinks by checking its rules, and then acts. Then it starts the loop over again.
Sense. Pip's sound sensor sends its squeak and the echo comes back right away. The input is "something is 3 centimeters ahead."
Think. Pip checks its rules. Something closer than 10 centimeters means stop. A second rule written for doors says that if the path stays blocked, Pip should beep twice and wait.
Act. Pip stops and plays beep beep.
Before reading on, guess what happens next. A girl in the art room hears the beeps and opens the door.
- Pip the robot at a closed door
- 1. Sense: Obstacle 3 cm ahead
- 2. Think: Too close to roll, beep for help
- 3. Act: Door opens!
The loop starts again right away. Pip senses, and this time the echo takes much longer to come back, so the way is open. Pip thinks, and the "otherwise, keep rolling" rule is now true. Pip acts and rolls through the doorway. Its light sensor finds the blue tape on the other side, so it follows the tape to the table and sets down the crayon box, which is step 6 of its program.

Pip never had a single clever idea of its own. It had a program with steps in the right order, sensors that noticed the door and the tape, and if-then rules that turned each new fact into the right move. Every robot you meet, from the vacuum under your table to a delivery robot at the curb, runs on those same three things.
Sources
Audiovisual sources
Frequently asked questions
What is a program in a robot?
A program is a list of exact steps that a robot follows one at a time, in order. Pip's program for the crayon trip includes steps like picking up the crayon box, turning to face the hallway, and rolling forward 10 steps.
What happens if a step is missing from a robot's program?
A robot only does the steps that are written down, so a missing step means the action never happens. If the pickup step is deleted, Pip still rolls forward and back, but its gripper stays empty and it has no way to know anything is missing.
How does a bump sensor work on a robot?
A bump sensor is a curved strip of plastic on a tiny spring with a switch behind it, like a doorbell button. When the bumper hits something, it slides back a few millimeters and presses the switch, sending the message "I touched something."
How does an ultrasonic sensor measure distance?
An ultrasonic sensor sends out a quick squeak of sound too high for people to hear, and listens for the echo. When the wall is far away the echo takes a moment to come back, and when it is close it comes back almost right away, so timing the echo tells the robot how far away the wall is.
What is an if-then rule in a robot program?
An if-then rule is a decision where the robot picks between two actions depending on what is true. For example, if the crossing light is red, then stop; otherwise, walk across.
