Please enable JavaScript to use CodeHS

Robotics

How Do Robots See and Feel?

Sensors explained

A robot has no eyes, ears, or skin. It only knows what its sensors tell it, and the sensors are not always right. Ping a virtual range-finder, pick the right sensor for seven robot jobs, and see why robots average their readings.

Robotics + physics ~25 min Beginner friendly

Ping, wait for the echo, do the math.

01 / THE BIG IDEA

A robot only knows what its sensors tell it

Close your eyes and walk across a room. You would bump into things. That is how a robot with no sensors feels all the time. It cannot see the wall. It cannot feel the floor. It just runs its program.

A sensor fixes that. A sensor is a part that turns something in the real world, like light, sound, a push, or a spin, into a number. The robot’s computer reads that number and decides what to do. If you have read What Is a Microcontroller?, this is the “sense” step of the sense-think-act loop. For the bigger picture of what makes a machine a robot, see What Is a Robot?

  • SENSE: read numbers
  • THINK: run the program
  • ACT: move motors

Here are four words you will use in the labs below.

Sensor
A part that measures something in the world and turns it into a number or a signal.
Reading
One number a sensor reports at one moment. A robot takes many readings every second.
Noise
Small random errors in readings. Measure the same wall twice and you might get two slightly different numbers.
Range
How near and how far a sensor can measure. Past its range, a sensor gives no useful answer.

Remember this. A sensor reading is not the truth. It is a guess made from a signal. Good robot builders always ask: how could this reading be wrong?

02 / THE SENSOR TEAM

Meet seven sensors and what each one is good at

Each sensor measures one kind of thing. Each one also has a weak spot. Read the cards, then try the lab.

  • Light and color sensor

    Measures how bright the light is. Point it at the floor with a small lamp beside it. A white floor bounces lots of light back. A black line soaks it up. Line-following robots use this trick, often with infrared light, which is light our eyes cannot see.

    Like your eyes noticing dark and bright.
    Weak spot strong sunlight can swamp it.

  • Infrared (IR) distance sensor

    Shoots a thin beam of invisible infrared light and watches where the spot lands on a tiny detector. Near things and far things land in different places, so the robot can tell how far away something is. It works best up close.

    Like your eyes judging how close a cup is.
    Weak spot short range, and shiny or very dark surfaces can fool it.

  • Ultrasonic sensor

    Sends out a chirp of sound too high for people to hear, then times the echo. Bats do the same thing in the dark. You will try it in the lab below.

    Like a bat, or a shout in a canyon.
    Weak spot soft things soak up sound, and tilted things bounce it away.

  • Touch or bumper switch

    A tiny switch that closes when something pushes on it. It gives only two answers: pressed or not pressed. It is simple, cheap, and very hard to fool.

    Like your skin feeling a bump.
    Weak spot it only tells the robot after it has already hit something.

  • Camera

    A camera is a grid of thousands of tiny light sensors. Each one reports a color and brightness, called a pixel. Software must work out what the pixels mean. Learn how in How Does Computer Vision Work?

    Like your eyes.
    Weak spot needs light, and needs a lot of computing power.

  • Gyroscope and accelerometer

    A gyroscope measures how fast the robot is turning. An accelerometer measures changes in speed and the pull of gravity, which tells which way is down. Together they are called an IMU, short for inertial measurement unit.

    Like your inner ear, which keeps you balanced.
    Weak spot the gyroscope slowly drifts over time.

  • Wheel encoder

    A disk with slots spins with the wheel. A light beam flashes through the slots, and each flash is one tick. Count the ticks to know how much the wheel turned. Turns times the wheel’s circumference gives distance.

    Like counting your steps.
    Weak spot if the wheel slips on a smooth floor, it counts turns that did not move the robot.

These sensors need power and wiring, and they plug into a small computer chip. You can learn about the circuits behind that in Ohm’s Law and Simple Circuits.

03 / ULTRASONIC LAB

Ping like a bat: measure distance with sound

Shout in a canyon and wait. A moment later you hear your own voice come back. The farther the canyon wall, the longer you wait.

An ultrasonic sensor does exactly that, only faster. It sends a chirp of sound and starts a timer. When the echo comes back, it stops the timer. The robot then turns the time into a distance.

There is one trick. The sound has to travel to the wall and back. That is two trips, so we divide by 2. In ordinary room air, sound travels about 343 meters every second.

distance = speed of sound × echo time ÷ 2

Example: an echo comes back after 0.00583 seconds (5.83 milliseconds). Then 343 × 0.00583 ÷ 2 is about 1 meter. A millisecond is one thousandth of a second, so echoes are very quick.

Range-finder lab

Top-down view

Drag the robot, or focus it and use the arrow keys. Press [ or ] to turn. The arena is 3 m wide.

  1. 1 Ping
    Chirp

    The sensor sends a burst of sound too high to hear.

  2. 2 Echo time
    –

    Time from chirp to echo.

  3. 3 Distance
    –

    343 m/s × time ÷ 2

Lab notes (the robot cannot know these)

Real distance: –

Last 40 readings (0 to 300 cm)

Try these

  1. Drag the robot so the reading is about 100 cm. Check the echo time. Is it close to 5.83 ms?
  2. Turn on Add noise and watch one wall. Do the readings stay exactly the same?
  3. Turn on Tricky surfaces. Aim at the pillow. What does the robot report?
  4. Face a wall straight on, then turn the robot slowly. At what angle does the echo disappear?

Why do soft and tilted things fail? A soft pillow soaks up the chirp, so little sound returns. A tilted board bounces the sound off to the side, like a ball hitting a slanted wall, so the echo never gets back. Past roughly 30 degrees of tilt, many sensors lose the echo. Your robot reports “nothing there,” even though a big object is right in front of it. Warm air also carries sound a little faster than cold air, so builders sometimes correct for temperature.

04 / MATCH THE SENSOR

Give the robot the right sensor for the job

You are the robot designer. Each job needs one sensor. Pick the best one. If you pick wrong, you will see why and can try again. You score a point only if your first try is right.

Job 1 of 7Score: 0
The job

    Pick a sensor to see what happens.
    05 / NOISE AND AVERAGING

    Why robots average their readings

    Ask five friends to guess how many jellybeans are in a jar. No single guess is perfect. But the group’s average is usually close.

    Sensors work the same way. Tiny vibrations, electrical hum, and air movement nudge every reading up or down by a little. That is noise. Sometimes a reading is wildly wrong for just one moment, a spike. A robot that trusts every single reading would twitch and jerk.

    So robots smooth their readings. Two simple ways:

    • Moving average. Add up the last few readings and divide by how many there are. Random ups and downs cancel out. One big spike still drags the answer a bit.
    • Moving median. Sort the last few readings and take the middle one. A single wild spike ends up at the edge of the list, so the median ignores it.

    The window is how many recent readings you use. A bigger window gives a smoother line. It also means the robot reacts later when something really changes. That is a trade-off. Try it below.

    Noisy-reading smoother

    Distance to wall, in cm
    Raw readings
    –

    average error

    Moving average
    –

    average error

    Moving median
    –

    average error

    “Average error” means: for each reading, how far was it from the true distance? Then we average those gaps. A smaller number is better.

    Try this. Set the window to 1. That is no smoothing at all. Raise it slowly and watch the lines calm down. Then pick “A wall appears” and notice that a big window is slow to notice the jump. What window size gives the best balance?

    06 / TEAMWORK

    Sensors work best as a team

    Every sensor you met has a weak spot. So real robots use several sensors and compare them. This idea is called sensor fusion, which just means combining sensors to get a better answer.

    • A robot’s wheel encoders say it drove 1 meter. But the wheels might have slipped. The IMU and ultrasonic sensor can check that story.
    • A camera spots a person. An ultrasonic sensor says how far away they are.
    • An IR sensor watches for a table edge. A bumper is the backup if the edge is missed.

    The “think” step then decides which reading to trust. That decision is just code: a few if statements and some math. If you want to build that kind of code, a CodeHS course is a great next step.

    07 / QUICK ANSWERS

    Quick answers

    What sensors do robots use?

    Common robot sensors include light and infrared sensors, ultrasonic distance sensors, touch or bumper switches, cameras, gyroscopes and accelerometers, and wheel encoders. Each one turns something in the world, like light, sound, a push, or a spin, into a number the robot’s computer can read.

    How does an ultrasonic sensor measure distance?

    It sends out a short chirp of sound that people cannot hear, then times how long the echo takes to come back. Sound travels about 343 meters per second in room-temperature air. The sound goes out and back, so the robot multiplies speed by time and divides by two to get the distance.

    Why do robots need more than one sensor?

    Every sensor has weak spots. An ultrasonic sensor can miss soft or tilted surfaces, a camera needs light, and a gyroscope slowly drifts. Using several sensors lets a robot check one reading against another and still work when one sensor is fooled.

    What is sensor noise?

    Noise is the small random error in a sensor reading. If a robot measures the same wall five times, it may get five slightly different numbers. Robots reduce noise by averaging several readings or by taking the median, which is the middle value after sorting them.

    What is the difference between a gyroscope and an accelerometer?

    A gyroscope measures how fast something is turning. An accelerometer measures changes in speed and the pull of gravity, which shows which way is down. They are often packaged together as an inertial measurement unit, or IMU, to help a robot balance and steer.

    Can a robot see like a person?

    Not on its own. A camera gives a robot a grid of colored dots called pixels. Software has to turn those pixels into meaning, such as finding a face or a line on the floor. That software step is called computer vision.

    08 / CHECK YOURSELF

    Quick quiz

    1. An ultrasonic sensor hears its echo after 0.01 seconds. Sound travels 343 m/s. About how far away is the object?

    2. A robot must know exactly how many times its wheel has turned. Which sensor fits best?

    3. A distance sensor reads 100, 101, 99, 250, 100. Which method best ignores the 250?

    Keep learning about robots

    You now know how robots turn the world into numbers, how an echo becomes a distance, and why smoothing helps. Next, see how the whole robot fits together, or dig into the computers and circuits inside.