Sensors in robots: how a robot “sees” and “feels” its surroundings
Digital and analogue sensors, and the most common ones in robot projects – ultrasonic, infrared, light, temperature and motion – and how to read them with Arduino.
- 8 min read
People see with their eyes, hear with their ears and feel heat with their skin, and then decide what to do. A robot needs something similar to deal with the world around it: sensors. Without them, a robot is a machine repeating fixed movements, unable to tell whether there is a wall in front of it or the edge of a table. In this article we explain what a sensor is and the difference between digital and analogue sensors, and we meet the most common sensors in educational robotics projects: ultrasonic, infrared, light, temperature and motion. We learn how to read them with Arduino and how to deal with unsteady readings, and finish with an obstacle-avoiding robot project that brings together everything we have learned.
What you will learn
- How a sensor works, and the difference between digital and analogue signals.
- The most common sensors in educational robots and how each one works.
- How to read sensors with Arduino and turn a reading into a decision.
- Handling noise in readings, and an obstacle-avoiding robot project.
What is a sensor?
A sensor is an electronic part that turns a physical phenomenon – light, distance, heat – into an electrical signal a microcontroller can read. The Arduino does not “see” the wall; it reads a number from the sensor, and the program you wrote is what turns the number into meaning: “the distance is under 20 centimetres, so stop”. This loop of measuring, deciding and acting is the essence of every robot.
Digital or analogue?
By the signal they give out, sensors fall into two kinds:
- A digital sensor gives just two states: yes or no – HIGH or LOW. A push button is one, or a motion sensor that says “movement” or “no movement”. We read it with
digitalRead. - An analogue sensor gives a varying value, such as light intensity or the position of a knob. The Uno’s analogue pins turn it into a number between 0 and 1023, and we read it with
analogRead.
There is a third kind used by many modern sensors: “smart” sensors with their own circuitry, which send their readings as data over communication protocols such as I2C and usually need a ready-made library to read them easily.
The most common sensors in educational robots
The ultrasonic sensor: measuring distance
It works like a bat: it sends out a sound wave at a frequency the ear cannot hear, then waits for its echo to bounce back from an object in front of it. The Arduino measures the time between sending and receiving and calculates the distance, because the speed of sound in air is roughly known – about 343 metres per second at room temperature. Since the wave travels there and back, we divide the calculated distance by two. The most popular model in education is the HC-SR04, suitable for distances from a few centimetres to a few metres. Note that very soft or steeply angled surfaces can scatter the echo and give inaccurate readings.
Infrared sensors: following a line
The sensor has an LED that sends out infrared light and a receiver that measures how much is reflected. A white surface reflects a lot; a black one absorbs more. That lets a “line-following” robot tell whether it is over a black line drawn on a white floor, so it can keep correcting its direction. The same sensors are used to detect edges so the robot does not fall off the table.
The light sensor
The simplest is the light-dependent resistor (LDR), a resistor whose value falls as the light increases. We connect it with a fixed resistor in what is called a “voltage divider”, so the voltage the analogue pin reads changes with the light. Ideal for a robot that heads towards a light source, or a light that switches on automatically in the dark.
Temperature and humidity sensors
Some are simple analogue sensors that give a voltage proportional to temperature; others are digital, such as the DHT family, which measures temperature and humidity together and is read with a ready-made library. They are useful for smart-home and gardening projects, and for monitoring motor temperature in larger robots.
The PIR motion sensor
It detects the infrared radiation given off by warm bodies of people and animals, and gives a digital signal when it spots movement. It is used in alarms and in lights that switch on when someone passes.
The inertial measurement unit (IMU): balance and direction
This unit combines, on one small chip, an accelerometer that measures tilt and vibration, a gyroscope that measures the speed of rotation, and sometimes an electronic compass. It is what lets a phone know you have turned it sideways, and what keeps drones balanced in the air. In educational robots it is used to build a robot that balances on two wheels, to detect whether the robot has tipped over, or to measure how far it has turned more accurately than timing the wheels. It is usually read over I2C with a ready-made library, and its readings need more processing and filtering than simple sensors, so it suits the intermediate stage, once the basics are mastered.
| Sensor | What it measures | Type of signal | Example project |
|---|---|---|---|
| Ultrasonic | Distance to an object in front | A pulse time measured digitally | An obstacle-avoiding robot |
| Infrared | Reflection from a nearby surface | Analogue or digital, depending on the module | A robot that follows a black line |
| Light-dependent resistor | Light intensity | Analogue | A robot that heads towards light |
| Temperature and humidity | Temperature and relative humidity | Analogue or digital data | A small weather station |
| PIR motion | Movement of a warm body | Digital | An alarm or automatic light |
The following full course from freeCodeCamp.org covers Arduino and sensors in detail:
Unsteady readings: how do you deal with noise?
You will soon notice that a sensor does not give exactly the same number every time, even when nothing has changed. That is normal “noise” in the real world, and there are simple ways to deal with it:
- Averaging: take several readings in a row and average them instead of relying on one.
- Ignore the odd ones: a distance reading of zero, or beyond the sensor’s range, is usually an error; ignore it.
- A decision margin: instead of the robot stopping at 20 centimetres and moving again at 21, make it stop at 20 and move only after 30. That prevents rapid flipping between the two decisions.
- Good connections: loose wires cause random readings, so fix your connections firmly.
Project: an obstacle-avoiding robot
This classic project brings sensors and motors together. We need a two-wheeled chassis with two motors, a motor driver circuit, an ultrasonic sensor at the front, an Arduino board and batteries. The logic is simple:
- Read the distance in front of the robot, averaging three readings.
- If the distance is greater than the safe limit, move forward.
- If it is less, stop, reverse a little, then turn right or left.
- Repeat this in loop without stopping.
To control the motors safely, read motors in robots; a motor is never connected directly to the board’s pins.
Advice before you buy a sensor
- Check the operating voltage: some sensors run at 5 volts, others only at 3.3 volts and may be damaged by a higher voltage.
- Look for the sensor’s datasheet and an available library before buying.
- Start with common sensors that have plenty of tutorials.
If you are just starting, read what is Arduino and your first Arduino program first. In the robotics and embedded systems course at Badr Academy Education we build robots that avoid obstacles and follow lines under the instructor’s supervision. For further resources visit the learning library, and try the Find your path tool.
The takeaway
Sensors are a robot’s senses: they turn light, distance, heat and motion into signals the Arduino can read. Understand the difference between digital and analogue, handle noisy readings with averages and a decision margin, and always check the operating voltage. Then combine the sensor with a motor, and you have a robot that makes its own decisions.
© 2026 Badr Academy Education – this content is exclusive to Badr Academy Education. Short quotations are welcome with the source and a link to this page.
Category: Robotics & electronics
Comments (0)
You can comment without an account; your comment appears after review, and the academy may edit it before publishing.
No comments yet – be the first to join in.


Add a comment