Safety and scope
Keep the prototype at low speed during tests and provide a manual power cut-off. Confirm the HC-SR04 is protected from unsuitable environments.
Plan, predict and test
Suggested virtual study time: about 25 minutes. This is a planning estimate, not a validated construction time.
Preparation: Foundation electricity and basic programming logic. Adult supervision for children. Review a foundation lesson ↗
Use paper, a spreadsheet or a simulator. This is not a verified physical assembly guide.
- Scenario: Assume a 20 cm stop threshold. Simulate 10 cm, 40 cm and a missing echo.
- Expected result: Stop at 10 cm; movement may be eligible at 40 cm; stop on missing echo.
- Checkpoint: Write your prediction, test it in the virtual model and explain any difference.
A correct answer does not certify the physical system.
Record your validation
Use only virtual inputs and indicators until the actual components, pinouts and electrical limits have been verified.
| Case | What to record |
|---|---|
| Normal input | 10 cm → stop indicator; 40 cm → movement-eligible indicator |
| Boundary | 19.9 cm → stop; 20 cm → movement-eligible in ideal model |
| Missing or invalid input | A0 is a simulated distance, not ultrasonic data. Missing echo is not represented by this sketch; add a valid-data flag before motor control. |
| Repeat test | Repeat the virtual test, record the actual output and compare with the expected result. |
These are test-planning requirements, not recorded hardware results.
Before building: understand the listed components and the relevant low-voltage safety rules. This is a concept brief, not a tested assembly manual.
What it does
Create an autonomous rover that detects an obstacle, stops before contact and chooses a new direction.
Engineering principle
Ultrasonic time-of-flight measurement. The HC-SR04 emits a short sound pulse and measures the echo return time. Distance is approximately echo time × speed of sound ÷ 2. A servo can rotate the sensor to sample the left and right directions before the controller chooses a turn.
Hardware and components
- Arduino UnoRuns the sensing and movement decision logic.
- HC-SR04 ultrasonic moduleMeasures distance; the commonly quoted usable range is approximately 2–400 cm in suitable conditions.
- SG90 servoRotates the sensor to scan the surroundings.
- L298N motor driverDrives the DC motors in forward, reverse and turning directions.
- 4 × DC geared motors, chassis and batteryProvide the four-wheel platform and its power.
Suggested build sequence
- Mount the ultrasonic sensor on the servo bracket and check for mechanical clearance.
- Measure distance at a fixed angle and validate the readings against a ruler.
- Make the car stop below a configurable threshold, for example 20 cm.
- Sweep the sensor to the left and right, compare the measured clearances, then turn toward the larger opening.
- Add timeouts for missing echoes and a safe stop if the sensor reading is invalid.
Learning outcomes
- Ultrasonic time-of-flight
- Servo positioning
- Motor direction control
- Decision logic and sensor fault handling
Possible upgrades
Add multiple distance sensors, wheel encoders, a map of the test area or a more deliberate path-selection algorithm.
Practice the core principle
Use a virtual Arduino Uno circuit with the following substitute inputs and indicators. The exercise deliberately does not drive motors, powered mechanisms or real industrial equipment. Physical experimentation requires adult supervision for children, a suitable current-limited supply and checked wiring.
Reference components
- Arduino Uno (5 V)
- 10 kΩ potentiometer
- 2 LEDs, each with 330 Ω resistor
Connection plan
- Potentiometer outer terminals → 5 V and GND; wiper → A0
- D8 → 330 Ω → stop LED anode; cathode → GND
- D9 → 330 Ω → clear LED anode; cathode → GND
Reference sketch
void setup(){
pinMode(8,OUTPUT); pinMode(9,OUTPUT);
Serial.begin(9600);
}
void loop(){
float cm=analogRead(A0)*100.0/1023.0;
bool stop=cm<20.0;
digitalWrite(8,stop); digitalWrite(9,!stop);
Serial.println(cm); delay(100);
}How the code works
The potentiometer models a distance from 0 to 100 cm. The comparison selects the stop indicator for distances below 20 cm. The other indicator represents clearance in the model only.
Practice sequence
- Set up the virtual components with power disconnected in the simulator.
- Verify pin numbers and LED polarity before running the sketch.
- Run and record the expected inputs and outputs.
- Test the boundary cases described below.
Expected result and testing
Set the simulated distance to 10 cm, then 40 cm. Confirm the stop and clear indicators respectively. Real sensors require timeout and fault detection; this model has none.
Troubleshooting
Check the specified pin assignments, supply and common ground, indicator polarity and serial monitor baud rate where applicable. If an input seems fixed, isolate that input and observe its value before changing the control logic.
Independent challenge
Change one model threshold or timing value, predict the resulting behaviour, and repeat the test. Explain the difference without adding physical actuators.