Back to Blog
Robotics & IoT

Robotics Demo: Ultrasonic Sensor Triggers Robot Car

Watch this robot car avoid obstacles automatically. Learn how ultrasonic sensors work in robotics.

TechGeekStack TeamOctober 27, 2025 6 min read

πŸ€– Build an Obstacle Avoidance Robot

Learn to build an autonomous robot that uses ultrasonic sensors to detect and avoid obstacles. Perfect for students learning robotics and Arduino programming!

🎯 Project Overview

In this tutorial, we'll create a simple yet impressive robot that can navigate autonomously by detecting obstacles and changing direction. This project teaches you sensor integration, motor control, and decision-making algorithms.

πŸ“¦ Components You'll Need:

  • βœ… Arduino Uno (or compatible board) - β‚Ή500
  • βœ… HC-SR04 Ultrasonic Sensor - β‚Ή100
  • βœ… L298N Motor Driver Module - β‚Ή200
  • βœ… 2x DC Motors with wheels - β‚Ή400
  • βœ… Robot Chassis Kit - β‚Ή300
  • βœ… 9V Battery + Battery Holder - β‚Ή150
  • βœ… Jumper Wires - β‚Ή50
  • Total Cost: ~β‚Ή1,700

πŸ”§ How Ultrasonic Sensors Work

The HC-SR04 ultrasonic sensor works like a bat's echolocation:

  1. 1. Transmit: Sends out ultrasonic sound waves (40kHz)
  2. 2. Reflect: Sound bounces off obstacles
  3. 3. Receive: Sensor detects the echo
  4. 4. Calculate: Distance = (Time Γ— Speed of Sound) / 2

Speed of sound = 340 m/s or 0.034 cm/Β΅s. If echo time is 100Β΅s, distance = (100 Γ— 0.034) / 2 = 1.7 cm

πŸ”Œ Circuit Connection

ULTRASONIC SENSOR (HC-SR04):
VCC  β†’ Arduino 5V
GND  β†’ Arduino GND
Trig β†’ Arduino Pin 9
Echo β†’ Arduino Pin 10

MOTOR DRIVER (L298N):
IN1  β†’ Arduino Pin 5
IN2  β†’ Arduino Pin 6
IN3  β†’ Arduino Pin 7
IN4  β†’ Arduino Pin 8
ENA  β†’ Arduino Pin 3 (PWM)
ENB  β†’ Arduino Pin 11 (PWM)
12V  β†’ Battery Positive
GND  β†’ Battery Negative + Arduino GND

DC MOTORS:
Motor A (Right) β†’ OUT1 & OUT2 on L298N
Motor B (Left)  β†’ OUT3 & OUT4 on L298N

πŸ’» The Arduino Code

// Pin Definitions
#define TRIG_PIN 9
#define ECHO_PIN 10

#define MOTOR_A_IN1 5
#define MOTOR_A_IN2 6
#define MOTOR_A_SPEED 3

#define MOTOR_B_IN3 7
#define MOTOR_B_IN4 8
#define MOTOR_B_SPEED 11

const int SAFE_DISTANCE = 30; // cm
const int SPEED = 150; // 0-255

void setup() {
  Serial.begin(9600);
  
  // Ultrasonic sensor pins
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  
  // Motor pins
  pinMode(MOTOR_A_IN1, OUTPUT);
  pinMode(MOTOR_A_IN2, OUTPUT);
  pinMode(MOTOR_A_SPEED, OUTPUT);
  pinMode(MOTOR_B_IN3, OUTPUT);
  pinMode(MOTOR_B_IN4, OUTPUT);
  pinMode(MOTOR_B_SPEED, OUTPUT);
}

void loop() {
  int distance = getDistance();
  Serial.print("Distance: ");
  Serial.println(distance);
  
  if (distance > SAFE_DISTANCE) {
    moveForward();
  } else {
    stopMotors();
    delay(500);
    moveBackward();
    delay(800);
    turnRight();
    delay(600);
  }
  
  delay(100);
}

int getDistance() {
  // Send ultrasonic pulse
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  // Read echo time
  long duration = pulseIn(ECHO_PIN, HIGH);
  
  // Calculate distance
  int distance = duration * 0.034 / 2;
  
  return distance;
}

void moveForward() {
  digitalWrite(MOTOR_A_IN1, HIGH);
  digitalWrite(MOTOR_A_IN2, LOW);
  digitalWrite(MOTOR_B_IN3, HIGH);
  digitalWrite(MOTOR_B_IN4, LOW);
  
  analogWrite(MOTOR_A_SPEED, SPEED);
  analogWrite(MOTOR_B_SPEED, SPEED);
}

void moveBackward() {
  digitalWrite(MOTOR_A_IN1, LOW);
  digitalWrite(MOTOR_A_IN2, HIGH);
  digitalWrite(MOTOR_B_IN3, LOW);
  digitalWrite(MOTOR_B_IN4, HIGH);
  
  analogWrite(MOTOR_A_SPEED, SPEED);
  analogWrite(MOTOR_B_SPEED, SPEED);
}

void turnRight() {
  digitalWrite(MOTOR_A_IN1, HIGH);
  digitalWrite(MOTOR_A_IN2, LOW);
  digitalWrite(MOTOR_B_IN3, LOW);
  digitalWrite(MOTOR_B_IN4, HIGH);
  
  analogWrite(MOTOR_A_SPEED, SPEED);
  analogWrite(MOTOR_B_SPEED, SPEED);
}

void stopMotors() {
  digitalWrite(MOTOR_A_IN1, LOW);
  digitalWrite(MOTOR_A_IN2, LOW);
  digitalWrite(MOTOR_B_IN3, LOW);
  digitalWrite(MOTOR_B_IN4, LOW);
  
  analogWrite(MOTOR_A_SPEED, 0);
  analogWrite(MOTOR_B_SPEED, 0);
}

πŸŽ“ How the Code Works

  1. 1. Setup: Initialize pins for sensor and motors
  2. 2. Main Loop: Continuously check distance
  3. 3. Decision Making:
    • If distance > 30cm β†’ Move forward
    • If distance ≀ 30cm β†’ Stop, reverse, turn right
  4. 4. Motor Control: PWM signals control speed (0-255)

⚠️ Troubleshooting Tips:

  • β€’ Sensor not working? Check wiring and power supply
  • β€’ Robot doesn't move straight? Adjust individual motor speeds
  • β€’ Erratic readings? Add delay between sensor readings
  • β€’ Motor driver hot? Reduce motor speed or add heat sink

πŸš€ Enhancements You Can Add

  • 🎯 Servo Motor: Add rotating sensor for 180Β° scanning
  • πŸ“± Bluetooth Control: Control robot via smartphone
  • πŸ”Š Buzzer Alerts: Sound when obstacle detected
  • πŸ’‘ LED Indicators: Visual feedback for robot state
  • πŸ—ΊοΈ Line Following: Add IR sensors for line tracking
  • 🌐 WiFi Module: IoT-enabled remote monitoring

πŸ“Ή Demo Video

Watch our step-by-step video tutorial showing the complete build process, circuit connections, and the robot in action!

πŸ“Ί

Video tutorial coming soon!

Subscribe to our channel for notifications

πŸ€– Master Robotics & IoT

Learn Arduino, ESP32, sensor integration, and build 10+ robotic projects in our comprehensive STEM course. Perfect for students!

Enroll in Robotics Course (β‚Ή2,999) β†’

πŸŽ‰ What You Learned

  • βœ… Ultrasonic sensor working principle
  • βœ… Motor driver control with Arduino
  • βœ… PWM for speed control
  • βœ… Autonomous navigation algorithms
  • βœ… Circuit design and debugging

Ready to build your own robot? Grab the components and start coding! πŸš€

Tags

#Robotics#Ultrasonic Sensor#Arduino#Robot Car#Sensors