One-sentence summary
A robot works by repeating the same three steps over and over: it senses with a sensor, decides by checking a condition, and acts with its motors — then starts again.
Why it matters
What separates a robot from a table toy is that a robot can notice its surroundings and behave accordingly. A box with blinking lights always does the same thing. A robot stops when it gets close to a wall, turns when it sees a line and moves again when the obstacle is gone.
Behind that behaviour is a single idea: the sense–decide–act loop. The robot does not run these three steps once — it repeats them maybe hundreds of times per second. The faster and steadier the loop turns, the more smoothly the robot responds.
This lesson brings together two ideas from earlier modules. From the algorithms module you remember the condition (if ... then) and repetition (loop) structures. From the sensor module you know how a distance sensor produces data. Robotics is putting these pieces into one working system.
The three steps of the loop
The basic flow inside a robot's brain always follows the same order. Let's meet each step on its own.
1. Sense (read the sensor)
The robot first asks: "What is happening around me right now?" The answer comes from its sensors — a number like "18 centimetres" from a distance sensor, a brightness value from a light sensor, or whether a button is pressed.
The robot makes no decision here. It simply takes a measurement and stores it in a variable, like looking out the window to check the weather before you leave the house.
2. Decide (check the condition)
The robot compares its measurement with a rule. This is where the condition you know from the algorithms module comes in:
If the distance is less than 15 centimetres
decide "too close"
Otherwise
decide "path is clear"
The decide step turns a measurement into an action. When the sensor says "18," the robot does not know what that means; the meaning comes from the condition you wrote. The same measurement can lead to a different decision under a different rule.
3. Act (drive the motor)
After the decision is made, the robot does something: it drives the motors forward, stops, reverses or changes direction. Turning on an LED or sounding a beep are actions too.
Here the result of the previous two steps reaches the physical world. The wheels turn, the surroundings change — and on the next turn the sensor measures this new situation again.
And again: the loop never stops
When the three steps finish, the robot starts over. That is why we call it a loop. The sense–decide–act order repeats again and again until the robot is switched off.
Repeat forever:
1. Read the sensor
2. Decide based on the condition
3. Drive the motor
Remember from the algorithms lesson: an infinite loop is fine when it is used on purpose and there is a safe way to stop (a power switch). In robots the main loop is usually infinite on purpose; we stop the robot by turning it off at the power switch.
Concrete examples
Example 1: A robot that does not hit the wall
A robot has a distance sensor on its front. The goal is simple: move forward, but stop when it gets too close to an obstacle.
Let's write the logic in pseudocode first:
Repeat forever:
distance = read front sensor
If distance is less than 15 centimetres
stop the motors
turn on the red LED
Otherwise
move forward
We can write the same logic in a short Arduino (C++) loop. The loop() function here already runs over and over — Arduino's own loop is our sense–decide–act turn:
void loop() {
int distance = readDistance(); // SENSE: cm value from sensor
if (distance < 15) { // DECIDE: is the obstacle close?
stop(); // ACT: stop the motors
digitalWrite(LED, HIGH); // red warning
} else {
moveForward(); // ACT: keep going
digitalWrite(LED, LOW);
}
}
Notice that readDistance, stop and moveForward are helper functions we wrote earlier. The code maps exactly onto the three steps of the pseudocode. The language changes, but the logic stays the same — just like the odd/even example in the algorithms lesson.
Example 2: A line-following robot
Now a slightly smarter behaviour. A robot with two light sensors underneath follows a black line on the floor. If the left sensor sees the line, the robot has drifted too far left and should correct to the right.
Repeat forever:
left = read left sensor
right = read right sensor
If left sees the line
steer right
Else if right sees the line
steer left
Otherwise
go straight
Here we have the same sense–decide–act loop; we just read two sensors and use more than one condition (else if). The robot makes small corrections to stay on the line, and because the loop turns very fast it looks like smooth tracking.
Both examples share the same shape: read, compare, act, repeat. Only the sensor you read and the condition change.
Mini practice
Design a robot behaviour on paper — you don't need to write real code.
Task: A robot uses its front distance sensor to move toward the edge of a table. When it gets within 10 centimetres of the edge it should stop and turn on a blue LED. If the edge clears (the distance grows again) it should move forward once more.
Write the pseudocode as a loop. Make sure all three steps appear on every turn:
- Sense: which sensor do you read, and where do you store the value?
- Decide: what is the condition? Think about both the "if" and the "otherwise" cases.
- Act: what do the motors and the LED do for each decision?
When you finish, ask yourself: if I placed this robot at the edge of a cliff, is this logic safe? Or should I point the distance sensor downward instead?
Common mistakes
Forgetting the loop
Reading the sensor only once. Then the robot gets stuck on the first measurement and does not react when the environment changes. The three steps must always live inside a loop.
Skipping the decide step
Reading the sensor and driving the motor directly, with no condition in between. Then the robot "senses" but does not "think" — the measurement is useless. There must be a rule between the reading and the action.
Ignoring the "otherwise" case
Writing "if it is close, stop" and forgetting the case where the path is clear. Then once the robot stops, it can never move again. A good condition handles both outcomes.
Turning the loop too fast with no wait
Some sensors need a short moment to take a reading. If the loop turns too fast, the readings can come back wrong. Usually a small pause (delay) is added to each turn.
Powering the motor from the logic supply
Motors cannot be powered straight from an Arduino control pin; they draw far too much current. We explain why in the safety note.
Safety note
A moving robot is different from a program on a screen — it really does bump, fall and pinch. So keep these in mind while testing:
- Clear a safe area. Test the robot on a wide, empty table or floor. Near an edge it can fall; try low speed first.
- Keep fingers, hair and cables away. Spinning wheels and gears can easily catch hair or a cable. Do not bring your hand near the wheels while the robot is running.
- Use a motor driver and a separate battery. Motors draw much more current than an Arduino control pin can supply. That is why we power motors through a motor driver from a separate low-voltage battery pack. The negative terminals (GND) of the Arduino and the battery pack must share a common ground; otherwise the signals will not work correctly.
- Never use mains electricity. Robot projects use only low-voltage batteries. Electricity from a wall socket is dangerous.
- Adult supervision. Ask an adult for help with anything involving motors, batteries or cutting tools. Set up the first test so you can switch the robot off immediately in an emergency.
Lesson summary
- Robots work by continuously repeating the sense–decide–act loop.
- In the sense step a sensor is read and the value is stored in a variable; no decision is made yet.
- In the decide step the measurement is compared with a condition (
if ... then) and turned into an action. - In the act step the motors are driven or outputs like an LED are changed; then the loop starts over.
- Motors must be powered from a separate battery through a motor driver, share a common ground, and the robot should be tested slowly in a safe area.
Check questions
- What are the three steps of the sense–decide–act loop, and in what order do they run?
- Does the robot make a decision in the "sense" step? What happens in this step?
- In the pseudocode below, which line shows the decide step?
distance = read front sensor
If distance is less than 15
stop
- Inside which Arduino function do we usually write the robot's main behaviour, and why is this function suited to a loop?
- Why do we power the motors from a separate battery and motor driver instead of from an Arduino control pin?
Answers
- The three steps run in the order sense, decide and act. The robot first reads the sensor, then decides by checking a condition, then drives the motor — and starts over.
- No, no decision is made. In the "sense" step the robot only takes a measurement from the sensor and stores it in a variable. The next step makes the decision.
- The decide step is the line
If distance is less than 15. The first line is sensing (reading) andstopis the action. - We write the main behaviour inside the
loop()function.loop()is called by Arduino over and over for as long as the robot is on, which makes it a natural loop for the sense–decide–act turn. - Motors draw far more current than an Arduino control pin can supply, and drawing it straight from the pin would damage the Arduino. A motor driver passes power from the separate battery to the motors safely, while the Arduino only sends a control signal. The two circuits must share a common ground.
Source and verification note
For “The Sense–Decide–Act Loop”, verification focuses on whether the relationship between The three steps of the loop and 2. Decide (check the condition) remains consistent across examples. Robot behaviour cannot be explained by code alone; mechanical structure, power system, sensor placement and surface conditions must be evaluated together. Test results should be recorded over several runs on the same course.
Next lesson
Chassis and Mechanical Design: Planning the robot's body, wheels and gears — how a sturdy, balanced robot is built.