Electronics And Robotics

Using Sensors to Let a Robot React

How distance, light and touch sensors work, what their readings mean and how a small robot can use them to avoid obstacles, follow light or stop on contact.

On this page
  1. Why sensors change everything
  2. Touch sensing
  3. Distance sensing
  4. Light sensing
  5. Turning readings into behavior
  6. Mounting and wiring
  7. Sensors at home
  8. What sensors cannot do

Why sensors change everything

A robot without sensors repeats a fixed script and cannot tell whether it has hit a wall. Sensors give it a small, imperfect view of the world, and with a few simple rules that view lets it react. This article covers the three most common beginner sensor types, what their readings mean and how a robot can use them.

Keep projects at low voltage while you experiment, and treat batteries with care. Some sensors, such as infrared emitters, are harmless at hobby power levels, but check the documentation for any part that emits light or sound. Test with the wheels off the ground first so the robot does not run away from you.

Touch sensing

The simplest sensor is a switch. A bumper switch or a small lever switch closes when something pushes it. The robot reads a pin and gets one of two answers: pressed or not. This gives you a reliable but late warning, because the robot has already touched the obstacle.

Wiring matters here. If the switch is only connected to the input pin on one side and nothing else, the pin is undefined when the switch is open and may read random values. The usual remedy is a resistor that pulls the pin to a known level, or an internal pull-up that many microcontroller boards provide. It is the same issue behind floating inputs.

A robot might use bumpers like this: if the left bumper is pressed, back up and turn right; if the right bumper is pressed, back up and turn left. This is crude, but it works, and it makes a good first sensing project.

Distance sensing

Distance sensors let a robot notice an obstacle before touching it. Two common approaches are:

  • Ultrasonic sensors send out a short burst of sound above human hearing and time how long the echo takes to return.
  • Infrared sensors send out infrared light and measure how much comes back, or the angle at which it returns.

For an ultrasonic sensor, the distance comes from the time and the speed of sound. Sound travels at roughly 343 meters per second in air at room temperature, and the pulse travels there and back, so the one-way distance is half the total. A hypothetical example: if the echo takes 5.8 milliseconds, that is 0.0058 seconds. Multiplying by 343 gives about 1.99 meters, and halving it gives roughly 1.0 meter. Temperature changes the speed of sound a little, so exact values vary.

Each type has limitations. Ultrasonic sensors can struggle with soft surfaces that absorb sound or angled surfaces that bounce the echo away. They also have a cone-shaped field of view and a minimum range. Infrared sensors can be affected by bright sunlight, and dark or shiny surfaces may reflect differently from pale matte ones. Read the datasheet for your part, because ranges and behavior vary from model to model.

Because readings jump around, a robot usually works with a threshold and a margin. For example, it might start turning when something is closer than a chosen distance and only resume driving once the path is clearly farther than that.

Light sensing

A light-dependent resistor changes its resistance with brightness, and a photodiode or phototransistor produces a changing current. Wired into a voltage divider, a light-dependent resistor gives a voltage the robot can read as a number. You do not need to know the exact brightness; you compare readings.

With two light sensors, one on the left and one on the right, a robot can steer toward the brighter side. Compare the two readings: if the left is higher, turn left a little. This is a simple form of feedback and is a classic beginner demonstration. Swapping the rule makes the robot avoid light instead.

Light sensors also have limits. Ambient light changes during the day, so a fixed threshold that works in the morning may fail in the evening. Calibrating at startup, or comparing two sensors against each other, handles that better than hard-coded numbers.

Line-following robots use a related idea: a downward-facing reflective sensor measures how much light bounces off the floor, which differs between a dark line and a pale surface.

Turning readings into behavior

The loop that connects sensing and action is explained in more detail under robot programming. A few practical points help here:

  • Print the readings first. Watch the numbers on your computer with the robot sitting in different situations, and note typical values.
  • Smooth the data. Averaging a few readings reduces jitter.
  • Choose thresholds from observation, not guesswork.
  • Combine sensors. A touch sensor as a backup behind a distance sensor catches what the distance sensor missed.
  • Plan for failure. If a sensor reads nothing or something impossible, the robot should stop safely.

Mounting and wiring

Where you put a sensor matters as much as which one you choose. Mount distance sensors at a height where they will see obstacles, pointed slightly away from the floor. Keep light sensors shielded from stray light from the robot’s own LEDs. Secure all wires, since vibration loosens connections over time. Keep signal wires away from motor wires, as motor current causes electrical noise that disturbs sensor readings.

If a sensor behaves strangely, check supply voltage, ground, and wiring before changing code. A meter helps: multimeter readings can confirm the sensor is getting power. The ordered approach in circuit troubleshooting applies here.

Sensors at home

The same ideas apply outside robots. A motion sensor can switch a light, and a soil moisture sensor can remind you to water a plant. Home projects like these should respect privacy and local rules, and do not replace certified safety devices. Whatever the setting, the principle is the same: the sensor reports a number, and your logic decides whether that number means something.

What sensors cannot do

No sensor sees everything. A distance sensor may miss a thin chair leg, a light sensor cannot tell a shadow from a wall, and a bumper only reacts after impact. Combining different types, testing in the conditions where the robot will actually run, and keeping behavior simple are the most reliable ways to build a robot that reacts sensibly most of the time. Expect some errors, and design so that mistakes end in a gentle stop rather than a crash.

The complete guide to electronics and robotics

The DIY Electronics Handbook: A Comprehensive Guide
Read the guide

Keep exploring

Guides from other topics

All topics →