The final capstone milestone of the 4-Day Robotics Workshop equips the robot car with full autonomy. Without any human steering or remote control, the vehicle drives forward continuously, scans the environment using an electronic radar turret, evaluates path clearance, and automatically decides the optimal evasion path around obstacles.
| Module / Component | Module Pin | Arduino Pin | Wiring Function & Architecture Description |
|---|---|---|---|
| L298N Left Speed | ENA | Pin 5 (Timer 0 PWM) | Left Motor Speed PWM throttle; remove jumper cap |
| L298N Right Speed | ENB | Pin 6 (Timer 0 PWM) | Right Motor Speed PWM throttle; remove jumper cap |
| L298N Direction Left | IN1, IN2 | Pin 7, Pin 8 | Left Motor Direction logic gates |
| L298N Direction Right | IN3, IN4 | Pin 11, Pin 12 | Right Motor Direction logic gates |
| SG90 Mini Servo | Signal (Orange) | Pin 10 | Servo PWM Pulse from <Servo.h> (Timer 1) |
| SG90 Mini Servo | Power (Red / Brown) | 5V & GND | Servo 5V power supply & ground reference |
| HC-SR04 Ultrasonic | TRIG | Pin A1 (Digital 15) | 10µs ultrasonic ping pulse generator |
| HC-SR04 Ultrasonic | ECHO | Pin A2 (Digital 16) | Echo return pulse duration reader |
| Active Buzzer | Positive (+) | Pin 4 | Alert horn / detection beep indicator |
| Battery Ground | Negative (−) | L298N GND & Uno GND | MANDATORY: Common Ground Junction! |
On ATmega328P (Arduino Uno), the <Servo.h> library commandeers Timer 1 to generate precise servo PPM pulses. Using Timer 1 disables hardware PWM on Pins 9 & 10! Therefore, in our autonomous architecture, motor PWM is routed to Pins 5 & 6 (Timer 0), ensuring both servo sweeps and motor speed control function simultaneously without glitching.
Before fastening the ultrasonic bracket to the servo horn, upload a test sketch or power on the servo while running radarServo.write(90);. Once the servo gear rotates to its true electronic center, press the bracket onto the gear splines facing dead straight ahead and screw the M2 center screw. This ensures 160° = Left, 90° = Center, and 20° = Right.
Sound travels at ~343 m/s (~29.1 µs/cm). Standard pulseIn(ECHO, HIGH) waits up to 1,000,000 µs (1 full second) if no echo returns! In autonomous driving, a 1-second freeze causes crashes. Our firmware sets a strict 30,000 µs timeout (max ~5 meters), keeping loop response lightning fast.
When an obstacle is detected within 25cm ahead, the car brakes immediately, beeps, rotates the servo left (160°) to measure distance, then right (20°) to measure distance. It steers toward whichever side has greater clearance. If both left and right are blocked (< 20cm), it backs up and performs a full 180° spin turn to escape dead-ends!
A servo motor and two DC gear motors active simultaneously can draw up to 1.8 Amperes during aggressive turns. If your 18650 batteries are nearly depleted, the voltage will sag below 4.5V, causing the Arduino to reboot in a continuous loop. Always use fully charged 3.7V Li-ion cells.
/*
* Connect Robo - Autonomous Radar Obstacle-Avoiding Smart Car
* Workshop: Satpuda STEM Lab Day 4 Capstone (23 Nov 2026)
* Target: Arduino Uno (ATmega328P) | Baud: 115200 bps
* Features: SG90 Servo Scanning Radar, HC-SR04 Rangefinder,
* Look-Ahead Decision Matrix & Dead-End 180-Degree Escape
*/
#include <Servo.h>
// Motor Pin Allocation (Pins 5 & 6 for Timer 0 PWM)
const int ENA = 5, IN1 = 7, IN2 = 8, IN3 = 11, IN4 = 12, ENB = 6;
const int SERVO_PIN = 10, TRIG_PIN = A1, ECHO_PIN = A2, BUZZER = 4;
const int SAFE_DIST_CM = 25, CRUISE_SPEED = 180, TURN_SPEED = 195;
Servo radarServo;
long readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// 30ms timeout prevents blocking if no echo returns
long duration = pulseIn(ECHO_PIN, HIGH, 30000);
if (duration == 0) return 300; // No obstacle detected (clear path)
return duration / 58; // Convert microseconds to centimeters
}
void setup() {
Serial.begin(115200);
pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); pinMode(ENB, OUTPUT);
pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER, OUTPUT);
// Attach servo and calibrate to 90 degrees center
radarServo.attach(SERVO_PIN);
radarServo.write(90);
stopMotors();
// Startup chirp
digitalWrite(BUZZER, HIGH); delay(100); digitalWrite(BUZZER, LOW);
delay(1000);
Serial.println(F("=================================================="));
Serial.println(F("🤖 Connect Robo Autonomous Radar Car Initialized!"));
Serial.println(F("Cruising forward with look-ahead radar obstacle evasion..."));
Serial.println(F("=================================================="));
}
void loop() {
radarServo.write(90); // Look straight ahead
long frontDist = readDistanceCM();
Serial.print(F("[RADAR] Front Distance: ")); Serial.print(frontDist); Serial.println(F(" cm"));
if (frontDist > SAFE_DIST_CM) {
// Clear path ahead - cruise forward
moveForward();
delay(60);
} else {
// Obstacle detected! Execute evasion maneuver
stopMotors();
alertBeep();
Serial.println(F("[ALERT] Obstacle ahead! Initiating radar sweep..."));
// Step 1: Scan Left (160 degrees)
radarServo.write(160);
delay(350);
long leftDist = readDistanceCM();
Serial.print(F("[RADAR] Left Clearance: ")); Serial.print(leftDist); Serial.println(F(" cm"));
// Step 2: Scan Right (20 degrees)
radarServo.write(20);
delay(400);
long rightDist = readDistanceCM();
Serial.print(F("[RADAR] Right Clearance: ")); Serial.print(rightDist); Serial.println(F(" cm"));
// Step 3: Return radar to center
radarServo.write(90);
delay(150);
// Step 4: Look-Ahead Decision Matrix
if (leftDist <= 18 && rightDist <= 18) {
// Dead-End Trap! Reverse and perform 180-degree spin escape
Serial.println(F("[DECISION] Dead-end detected! Reversing & executing 180-deg spin escape"));
moveBackward();
delay(700);
spinLeft();
delay(900);
stopMotors();
} else if (leftDist >= rightDist) {
// Left path has greater clearance - steer left
Serial.println(F("[DECISION] Left path clearer → Executing Left Turn"));
spinLeft();
delay(450);
stopMotors();
} else {
// Right path has greater clearance - steer right
Serial.println(F("[DECISION] Right path clearer → Executing Right Turn"));
spinRight();
delay(450);
stopMotors();
}
}
}
void moveForward() {
analogWrite(ENA, CRUISE_SPEED); analogWrite(ENB, CRUISE_SPEED);
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}
void moveBackward() {
analogWrite(ENA, CRUISE_SPEED); analogWrite(ENB, CRUISE_SPEED);
digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
}
void spinLeft() {
analogWrite(ENA, TURN_SPEED); analogWrite(ENB, TURN_SPEED);
digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}
void spinRight() {
analogWrite(ENA, TURN_SPEED); analogWrite(ENB, TURN_SPEED);
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
}
void stopMotors() {
digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);
analogWrite(ENA, 0); analogWrite(ENB, 0);
}
void alertBeep() {
digitalWrite(BUZZER, HIGH); delay(80); digitalWrite(BUZZER, LOW);
}
| Issue Observed | Underlying Cause | Instant Classroom Remedy |
|---|---|---|
| Ultrasonic sensor constantly reads 0 cm or 300 cm | TRIG or ECHO jumper wires loose or swapped. | Verify TRIG is on Pin A1 and ECHO is on Pin A2. |
| Servo jitters violently or resets Arduino | Voltage drop caused by simultaneous motor + servo inrush current. | Ensure 18650 battery pack is charged; verify Arduino GND and L298N GND are tied together. |
| Car turns toward the obstacle instead of away from it | Left/Right scan values swapped or motor polarity reversed. | Verify 160° points to the car's physical left, and 20° points to the physical right. |
| Car does not turn sharply enough on carpets | Friction on rubber wheels requires higher turn torque. | Increase TURN_SPEED in code from 195 to 220 or 240. |
| Servo arm hits chassis or wire bundle during sweep | Mechanical interference on rotation arc. | Tuck wires behind the sensor mount using zip-ties or rubber bands. |