Ultrasonic Obstacle-Avoiding Car with Arduino
Building an Obstacle-Avoiding Car using Arduino, Ultrasonic Sensor, and Servo Motor |
In this project, we will be building a car that can avoid obstacles using an Arduino board, ultrasonic sensor, and servo motor. The components required for this project include: |
Arduino Nano (or Arduino Uno/Mega) |
Ultrasonic sensor |
Servo motor |
L298N motor driver |
4WD car chassis |
3-cell lithium-ion battery holder |
Breadboard and jumper wires |
Assembly of the Car Chassis |
The first step is to assemble the 4WD car chassis according to the manufacturer's instructions. |
Connecting the Motor Driver and Battery Holder |
Attach the L298N motor driver to the chassis and connect the motor terminals to the motor driver. Then, attach the 3-cell lithium-ion battery holder to the chassis and connect the positive terminal of the battery to the 12V input terminal of the motor driver, and the negative terminal to the ground terminal. |
Connecting the Arduino Board |
Attach the Arduino Nano board to a breadboard and place it in the chassis. Connect the VCC pin of the Arduino to the positive terminal of the breadboard, and the GND pin to the negative terminal. |
Connecting the Ultrasonic Sensor and Servo Motor |
Attach the ultrasonic sensor to the top of the servo motor, which is attached to the front of the chassis. Connect the ground pin of the ultrasonic sensor to the negative terminal of the breadboard, and the VCC pin to the positive terminal. |
Connect the echo pin of the ultrasonic sensor to digital pin A6 of the Arduino, and the trigger pin to digital pin A4. |
Connect the servo motor signal pin to digital pin 10 of the Arduino. |
Coding |
The code for this project includes two external libraries: Servo.h for controlling the servo motor, and NewPing.h for working with ultrasonic sensors. |
The constants define the pin numbers for motor control and the servo motor. The defined directives are used to set pin numbers for the ultrasonic sensor's trigger and echo pins. |
The NewPing object sonar is initialized with the defined trigger pin, echo pin, and maximum distance. |
In the setup() function, the servo motor is positioned at 90 degrees (center) and a one-second delay allows the servo to reach this position. |
In the loop() function, the variables distanceRight and distanceLeft are initialized to store the measured distances when looking right and left. |
If the robot detects an obstacle within 30 centimeters, it stops, moves backward, stops again, and checks for the distance on the right and left using the lookRight() and lookLeft() functions. |
The robot compares the distances measured on the left and right. If the right side is clear or has more space, it turns right; otherwise, it turns left. |
After turning, the robot stops. If there's no obstacle within 30 centimeters, the robot moves forward. |
Uploading the Code |
Select the board as Arduino Nano and select the correct port. Then, upload the code to the Arduino board. |
Robot Car |
Background: |
A robot car is a type of autonomous vehicle that uses artificial intelligence (AI) and sensor technologies to navigate and control its movements without human intervention. The concept of robot cars has been around for several decades, but significant advancements in AI, computer vision, and machine learning have made them more viable in recent years. |
Development: |
The development of robot cars involves a multidisciplinary approach, combining expertise from robotics, computer science, mechanical engineering, and automotive engineering. Researchers and engineers use various programming languages, such as Python, C++, and MATLAB, to develop algorithms and software that enable the vehicle to perceive its environment, make decisions, and execute actions. |
Key Features: |
Robot cars are equipped with a range of sensors, including cameras, lidar (light detection and ranging), radar, GPS, and ultrasonic sensors. These sensors provide the vehicle with a 360-degree view of its surroundings, allowing it to detect obstacles, track other vehicles, and navigate through complex environments. |
Applications: |
Potential applications for robot cars include autonomous taxis, self-driving trucks, and personal vehicles. They also have the potential to improve road safety, reduce traffic congestion, and enhance mobility for the elderly and disabled. |
Ultrasonic Obstacle-Avoiding Car with Arduino |
Introduction |
In this article, we will explore the concept of creating an ultrasonic obstacle-avoiding car using Arduino. The project involves designing a robotic car that can detect and avoid obstacles using ultrasonic sensors. |
Components Required |
- Arduino Uno Board
- Ultrasonic Sensor (HC-SR04)
- L293D Motor Driver IC
- DC Motors
- Battery and Power Supply
- Jumper Wires
|
Circuit Diagram |
The circuit diagram shows the connection of the ultrasonic sensor, motor driver IC, and DC motors to the Arduino board.
|
Working Principle |
The working principle of the project is based on the concept of ultrasonic sensing. The ultrasonic sensor emits high-frequency sound waves, which bounce back when they encounter an obstacle. The sensor then calculates the distance of the obstacle using the time-of-flight principle. |
Arduino Code |
const int trigPin = 9;
const int echoPin = 10;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.034) / 2;
if (distance < 20) {
// Obstacle detected, take action
} else {
// No obstacle detected, move forward
}
}
The Arduino code reads the distance value from the ultrasonic sensor and takes action accordingly.
|
Advantages and Applications |
- The project demonstrates a cost-effective and efficient way to implement obstacle avoidance in robotics.
- The system can be integrated with other sensors and actuators to create a more sophisticated robotic system.
- Applications include autonomous vehicles, robot vacuum cleaners, and industrial automation systems.
|
Q1: What is an Ultrasonic Obstacle-Avoiding Car with Arduino? |
An Ultrasonic Obstacle-Avoiding Car with Arduino is a robotic vehicle that uses ultrasonic sensors and an Arduino board to detect and avoid obstacles in its path. |
Q2: What are the main components of this project? |
The main components of this project include an Arduino board, ultrasonic sensors, a motor driver, DC motors, and a chassis with wheels. |
Q3: How does the ultrasonic sensor work in this project? |
The ultrasonic sensor emits high-frequency sound waves and measures the time it takes for the waves to bounce back from obstacles, calculating the distance of the obstacle from the sensor. |
Q4: What is the role of the Arduino board in this project? |
The Arduino board reads data from the ultrasonic sensors and controls the movement of the motors based on the distance of obstacles, allowing the car to avoid collisions. |
Q5: How does the motor driver work in this project? |
The motor driver receives signals from the Arduino board and controls the speed and direction of the DC motors, enabling the car to move forward, backward, left, or right. |
Q6: What is the purpose of the chassis with wheels in this project? |
The chassis provides a base for the other components and supports the movement of the car, while the wheels allow the car to move smoothly and efficiently. |
Q7: How does the car avoid obstacles? |
The car avoids obstacles by using the ultrasonic sensor data to detect objects in its path and then changing direction or stopping to prevent collisions. |
Q8: Can this project be modified for different applications? |
|
Q9: What programming language is used in this project? |
The Arduino board uses the C++ programming language to write and upload code for controlling the car's movements and sensor interactions. |
Q10: Can I build this project at home? |
|
Rank |
Pioneer/Company |
Description |
1 |
Make: |
Published one of the first tutorials on building an ultrasonic obstacle-avoiding car with Arduino in 2013. |
2 |
Adafruit |
Developed the popular AFMotor shield for Arduino, which enabled easy motor control and obstacle avoidance capabilities. |
3 |
SparkFun |
Created the SparkFun Inventor's Kit, which includes an ultrasonic sensor and example code for building an obstacle-avoiding car with Arduino. |
4 |
Trossen Robotics |
Developed the ArbotiX RoboController, an Arduino-compatible board designed for robotics and obstacle avoidance applications. |
5 |
DFRobot |
Released the Romeo V2, a popular Arduino-compatible robot controller with built-in ultrasonic sensor support. |
6 |
Parallax Inc. |
Developed the Ping))) Ultrasonic Distance Sensor, a popular choice for obstacle avoidance applications with Arduino. |
7 |
Seeed Studio |
Released the Grove Ultrasonic Ranger, a plug-and-play ultrasonic sensor module for Arduino and other microcontrollers. |
8 |
Elegoo |
Developed the Elegoo Smart Robot Car Kit, a popular DIY kit for building an ultrasonic obstacle-avoiding car with Arduino. |
9 |
SainSmart |
Released the SainSmart X400, a popular ultrasonic sensor module for obstacle avoidance applications with Arduino and other microcontrollers. |
10 |
RoboPeak |
Developed the RoboPeak Ultrasonic Sensor, a high-accuracy ultrasonic sensor module for obstacle avoidance applications with Arduino and other microcontrollers. |
Component |
Description |
Technical Details |
Microcontroller |
Arduino Uno R3 |
- ATmega328P microcontroller
- 14 digital input/output pins
- 6 analog inputs
- 16 MHz clock speed
- 32 KB flash memory
- 2 KB SRAM
- 1 KB EEPROM
|
Ultrasonic Sensor |
HC-SR04 |
- Measuring range: 2 cm - 400 cm
- Accuracy: ±1 cm
- Operating frequency: 40 kHz
- Trig pin input impedance: 10 kΩ
- Echo pin output impedance: 100 Ω
|
L293D Motor Driver IC |
|
- Dual H-bridge motor driver
- Maximum current: 1 A per channel
- Supply voltage: 4.5 V - 36 V
- Logic input voltage: 3.3 V - 5 V
|
DC Motor |
Generic 6V DC Motor |
- Voltage: 6 V
- Current: 200 mA - 500 mA
- Speed: 1000 rpm - 3000 rpm
- Torque: 1 kgf·cm - 5 kgf·cm
|
Breadboard and Jumper Wires |
|
- For prototyping and circuit assembly
- Jumper wires: 20 AWG, 10 cm long
|
Power Supply |
6V Battery Pack or Wall Adapter |
- Voltage: 6 V
- Current: 500 mA - 1 A
|
Programming Language |
Arduino C++ |
- Based on C++ programming language
- Specific libraries for Arduino boards
|
Circuit Diagram |
Description |
|
The circuit diagram shows the connections between the Arduino Uno, ultrasonic sensor, motor driver IC, DC motors, and power supply.
|
Code Snippet |
Description |
```c
const int trigPin = 9;
const int echoPin = 10;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Clear the trigPin by setting it LOW for 2 microseconds
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
// Set the trigPin LOW again
digitalWrite(trigPin, LOW);
// Read the echo time using pulseIn() function
int duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
float distance = duration * 0.034 / 2;
// Print the distance value to serial monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(100);
}
```
|
This code snippet reads the ultrasonic sensor values and calculates the distance in centimeters. |
|