Robotics Demo: Ultrasonic Sensor Triggers Robot Car
Watch this robot car avoid obstacles automatically. Learn how ultrasonic sensors work in robotics.
π€ 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. Transmit: Sends out ultrasonic sound waves (40kHz)
- 2. Reflect: Sound bounces off obstacles
- 3. Receive: Sensor detects the echo
- 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. Setup: Initialize pins for sensor and motors
- 2. Main Loop: Continuously check distance
- 3. Decision Making:
- If distance > 30cm β Move forward
- If distance β€ 30cm β Stop, reverse, turn right
- 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
Related Articles
IoT for Pets: Smart Sensor Belt for Cats/Dogs
6 min readHow to Integrate AI in Robotics: Simple Architecture Explained
7 min readHow to Build a Grass-Cutting Robot for 1-Acre Yard
10 min readπ‘ Want to learn more?
Explore our comprehensive courses on AI, programming, and robotics.
Browse Courses