When you hear the word "robot", you might picture a shiny metal person from a film. But most real robots look nothing like that. A robot vacuum cleaner gliding under a sofa, a robotic arm welding cars in a factory, and a drone spraying a farmer's field are all robots.
So what makes a machine a robot? And how does a robot "decide" what to do next? In this tutorial you will find out, and then you will write the brain of a pretend cleaning robot in Python.
What makes a machine a robot?
Engineers usually say a robot has three abilities:
- It can sense. It uses sensors to collect information about the world, like distance, light, sound or touch.
- It can think. A small computer inside, often called a controller, runs a program that decides what to do with that information.
- It can act. It uses actuators, such as motors, to move, grip, spin or push.
A robot repeats these three steps again and again, many times every second. This is called the sense, think, act loop.
Is a ceiling fan a robot? It has a motor, but it does not sense anything or make decisions; it just spins when you switch it on. So, no. What about an automatic door at a mall? It senses you and acts by sliding open, but its "thinking" is a single fixed rule. People disagree on whether that counts, and that is a fun debate to have. A robot vacuum, which senses walls, decides where to turn and drives its wheels, clearly fits all three.
Sensors: the robot's senses
Sensors are a robot's eyes, ears and skin. Some common ones:
- Distance sensors (ultrasonic or infrared) measure how far away an object is, often in centimetres.
- Touch or bump sensors are simple switches that are either pressed or not.
- Light sensors measure brightness; line-following robots use them to stay on a dark line.
- Cameras give a full picture, which needs more computing power to understand.
Real sensors are not perfect. Their readings can jump around a little, which is called noise, so good robots are programmed to handle that.
Motors: the robot's muscles
Motors turn electricity into movement. Two wheels with separate motors can steer a robot: spin both forward to go straight, spin one faster to turn, spin them in opposite directions to rotate on the spot. Robot arms use motors at each joint, a bit like your shoulder, elbow and wrist.
Thinking: the code in the middle
The thinking part is a program. For a simple robot it can be a few if rules: if something is very close, back up; if it is a little close, turn; otherwise, go forward. More advanced robots use AI to learn better behaviour from experience.
Try it: program a cleaning robot's brain
We do not have a real robot here, so we will pretend. Our list sensor_readings holds what the front distance sensor reported at each moment as the robot moved around a room. Your code will be the "think" step, and the printed action is what the motors would do.
- Open Python on your computer or an online Python editor.
- Type the program below.
- Before you run it, predict the action for each reading. Write your guesses down.
- Run it and compare your predictions with the output.
# A floor-cleaning robot's sense -> think -> act loop
# Each number is what the front distance sensor reads (in cm) at each step.
sensor_readings = [80, 62, 45, 28, 14, 9, 70, 55, 12, 6]
SAFE_DISTANCE = 15 # closer than this means "obstacle ahead"
def think(distance):
if distance < 10:
return "BACK UP"
elif distance < SAFE_DISTANCE:
return "TURN LEFT"
else:
return "GO FORWARD"
for step, distance in enumerate(sensor_readings, start=1):
action = think(distance) # think
print(f"Step {step:2}: sensor = {distance:3} cm -> {action}") # act
Here is the output:
Step 1: sensor = 80 cm -> GO FORWARD
Step 2: sensor = 62 cm -> GO FORWARD
Step 3: sensor = 45 cm -> GO FORWARD
Step 4: sensor = 28 cm -> GO FORWARD
Step 5: sensor = 14 cm -> TURN LEFT
Step 6: sensor = 9 cm -> BACK UP
Step 7: sensor = 70 cm -> GO FORWARD
Step 8: sensor = 55 cm -> GO FORWARD
Step 9: sensor = 12 cm -> TURN LEFT
Step 10: sensor = 6 cm -> BACK UP
Can you picture it? The robot drives towards a wall, turns as it gets close, backs up when it is too close, then heads off across the room again.
The order of the if checks matters. We test the most dangerous case (under 10 cm) first. If we checked distance < SAFE_DISTANCE first, a reading of 6 cm would trigger "TURN LEFT" and the robot might bump the wall.
Experiment
- Change
SAFE_DISTANCEto 30. Which steps change? - Add a new rule: if the distance is over 60 cm, return "GO FAST".
- Add a "noisy" reading such as 200 in the middle of the list. Would a real robot trust one strange reading? How might you make it more careful?
For parents
Robotics connects coding to the physical world, which many children find motivating. This activity introduces the sense, think, act loop that sits behind everything from robot vacuums to self-driving research. Spotting robots (and non-robots) around the house together, and debating which category each falls into, is a simple screen-free extension.
What you learned
- A robot senses, thinks and acts, again and again, in a loop.
- Sensors measure the world; motors and other actuators make movement.
- The thinking part is a program, which can be simple
ifrules or something that learns. - The order of your checks matters, especially for safety.
- Real sensor readings are noisy, so robots must handle odd values.
Build the next version
This is a first step into robotics. In a full robotics course, students explore digital and analog sensors, smooth out noisy readings, control motors and steering with feedback, study robot arms that learn by copying, and train robots that learn from rewards. Want to build more? Book a free demo class and see robotics and AI come together.