Arduino-Controlled Obstacle-Avoiding Robot with Ultrasonic Sensor and Servo Motor
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control an obstacle-avoiding robot. It uses an Arduino UNO as the central microcontroller to manage inputs and outputs, including signals to a servo motor and two DC motors through an L298N motor driver. An HC-SR04 ultrasonic sensor is used to detect obstacles. The robot changes direction when an obstacle is detected within a certain distance. A 3xAAA battery pack provides the power supply for the circuit.
Component List
Arduino UNO
- Microcontroller board based on the ATmega328P
- It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.
DC Motor (x2)
- Electric motor that runs on direct current electricity.
- Used for driving the wheels of the robot.
Servo
- A rotary actuator or linear actuator that allows for precise control of angular or linear position, velocity, and acceleration.
- Used for steering the robot by changing direction.
HC-SR04 Ultrasonic Sensor
- An ultrasonic distance sensor that can measure distances from 2cm to 400cm with an accuracy of 3mm.
- Used for obstacle detection.
3xAAA Battery Pack with Switch and JST
- A battery holder for three AAA batteries with an integrated switch and JST connector.
- Provides power to the Arduino UNO and the motor driver.
L298N DC Motor Driver
- A high-power motor driver capable of driving up to 2A per channel.
- Used to control the speed and direction of the two DC motors.
Wiring Details
Arduino UNO
5Vconnected to theVCCof the HC-SR04 Ultrasonic Sensor andvccof the Servo.GNDconnected to thegndof the Servo,GNDof the HC-SR04 Ultrasonic Sensor,GNDof the L298N DC motor driver, andNEGof the 3xAAA Battery Pack.Vinconnected to thePOSof the 3xAAA Battery Pack and12Vof the L298N DC motor driver.- Digital pins
D2toD11are used to control the L298N motor driver and the Servo.
DC Motors
- Each motor has two pins (
pin 1andpin 2) connected to theOUT1,OUT2,OUT3, andOUT4of the L298N motor driver.
Servo
pulseconnected to theD5of the Arduino UNO.
HC-SR04 Ultrasonic Sensor
TRIGconnected toD2andECHOconnected toD3of the Arduino UNO.
L298N DC Motor Driver
ENAandENBconnected toD11andD6of the Arduino UNO for enabling the motor outputs.IN1toIN4connected toD10,D9,D8, andD7of the Arduino UNO for controlling motor direction.
3xAAA Battery Pack with Switch and JST
NEGconnected to theGNDof the Arduino UNO and the L298N DC motor driver.POSconnected to theVinof the Arduino UNO and12Vof the L298N DC motor driver.
Documented Code
/*
* This Arduino sketch controls an obstacle-avoiding robot with a servo motor.
* The robot uses an HC-SR04 ultrasonic sensor to detect obstacles and a servo
* motor to change direction. Two DC motors are used for movement, controlled
* by an L298N motor driver. The robot stops and changes direction when an
* obstacle is detected within a certain distance.
*/
#include <Servo.h>
// Pin definitions
const int trigPin = 2;
const int echoPin = 3;
const int servoPin = 5;
const int motor1Pin1 = 10;
const int motor1Pin2 = 9;
const int motor2Pin1 = 8;
const int motor2Pin2 = 7;
const int enA = 11;
const int enB = 6;
Servo myServo;
void setup() {
// Initialize the motor control pins as outputs
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
// Initialize the ultrasonic sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Attach the servo motor
myServo.attach(servoPin);
// Start the servo at 90 degrees
myServo.write(90);
}
void loop() {
// Measure the distance to an obstacle
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 20) { // If an obstacle is detected within 20 cm
// Stop the motors
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
// Turn the servo to look for a clear path
myServo.write(0);
delay(500);
myServo.write(180);
delay(500);
myServo.write(90);
// Reverse the motors to change direction
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
delay(1000);
} else { // If no obstacle is detected
// Move the motors forward
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
}
This code is responsible for the operation of the obstacle-avoiding robot. It initializes the pins, sets up the servo, and controls the motors based on the distance readings from the ultrasonic sensor. The robot moves forward until an obstacle is detected within 20 cm, at which point it stops, looks for a clear path with the servo, and then reverses direction.