Teach lesson
Arduino Visual Robot (3/4): proximity without crashes
Students use proximity sensor readings in visual code to avoid crashes, test thresholds, and explain how sensor input changes robot behavior.
Learning Outcomes
Read proximity sensors before making a decision.
Choose a simple threshold from observations.
Program a safe reaction to a nearby obstacle.
Student activity preview
Activity Content
Preview only. In a class session, students can fill in responses and submit their work to the teacher.
Sense before deciding
8 min
A sensor is a component that measures something in the environment and
returns a reading (a number). The sensor does not "know" what to do; the
program decides. On this robot, the proximity sensor returns an approximate
number from 1 to 6: 1 = object very far and 6 = object very close (the
closer the object, the higher the reading).
A threshold is the cut-off number that separates "safe" from "too close". The
program:
1. Reads the sensors.
2. Compares the reading with a threshold.
3. Chooses a reaction.
4. Observes whether the reaction was safe.
If a proximity reading increases when an object is closer, what does the condition reading >= 3 mean? Why should you observe readings before choosing a threshold?
Measure before reacting
20 min
Serial (serial communication) is a text channel from the robot to your screen:
it lets you show the sensor reading as a number. If your lab does not show serial
output, that is fine: in the next section you will calibrate the threshold by
watching the robot's LED, with no numbers needed.
Open the robot visual lab and choose the recommended robot option.
Set up serial communication.
In the main loop, add
Read proximity sensors.Read one value using the center sensor detecting on its left (the default for this session; it measures proximity on the left side of the center sensor).
Print the reading to serial.
Wait
300to500ms so changes are readable.Verify, upload, and observe readings when the robot is far from and near an obstacle.
Measurement block image and text reference:
Before using messages:
[Setup] Serial speed 9600
In the main loop:
[Read proximity sensors]
[print]
center sensor detect on his left
or
center sensor detect on his right
[wait] 300 milliseconds
Order matters: if you check the value before Read proximity sensors, the program may use an old reading.
Fill the table while you calibrate. Use at least three rows: one for a far
object, one for a medium distance, and one for a close object; leave extra rows
empty. In each row, record the situation, the sensor side you used, the number
or visible LED reaction, whether the robot should react, and any note that would
help you choose a threshold.
| Observed situation | Sensor used | Reading (number, or where the LED reacts) | Should it react? | Notes |
|---|---|---|---|---|
Choose a provisional threshold for "safe" versus "too close". Justify it with one reading from your table.
Program a safe reaction
18 min
Program a simple rule:
- If the reading is greater than or equal to your threshold: motors 0, red LED on, message obstacle.
- If the reading is lower than your threshold: drive slowly with 125 or 150, blue LED on, message clear.
Do not try to avoid every obstacle. In this session it is enough for the robot to warn or stop before a visible obstacle in one test situation.
Reaction block image and text reference:
In the main loop:
[Read proximity sensors]
if [center sensor detect on his left >= threshold]:
[Change two motors speed]
Left: 0
Right: 0
[Set] red [led to] HIGH
[Set] blue [led to] LOW
[print] "obstacle"
else:
[Change two motors speed]
Left: 125
Right: 125
[Set] blue [led to] HIGH
[Set] red [led to] LOW
[print] "clear"
If you switch to another sensor (for example, detecting on its right), change only the sensor reading, not the whole program structure.
Calibrate with the LED (if you cannot see serial output): this same program
lets you calibrate without numbers. Start with a threshold (for example, 3).
Move an object closer to and farther from the sensor and watch at what distance
the red LED turns on. If it turns on too far away, raise the threshold; if it only
turns on when the object is almost touching, lower it. Repeat until the red LED
triggers at the distance you want.
Did the reaction happen too early, too late, or at the right time? State what you would adjust: threshold, speed, or wait time.
Describe one limitation of the sensor or your test: object position, unstable reading, robot position, queue time, or camera view.
Evidence submission
9 min
Proximity reaction program
Submit your threshold, a screenshot or description of the program, and one observation from a test where the robot reacted or should have reacted.