Arduino UNO Obstacle-Avoiding Robot with Ultrasonic Sensor and Servo Motor
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This document describes an obstacle-avoiding robot circuit. The robot is controlled by an Arduino UNO microcontroller and includes two DC motors, an ultrasonic sensor, and a servo motor. The robot moves forward until an obstacle is detected, at which point it stops, scans for a clear path using the servo-mounted ultrasonic sensor, and then turns in the direction of the clear path.
Component List
Arduino UNO
- Description: Microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
L298N DC Motor Driver
- Description: Dual H-Bridge motor driver module.
- Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
Ultrasonic Sensor
- Description: Sensor for measuring distance using ultrasonic waves.
- Pins: +VCC, Trigger, Echo, GND
DC Motor
- Description: Standard DC motor.
- Pins: pin 1, pin 2
Servo
- Description: Servo motor for precise control of angular position.
- Pins: gnd, vcc, pulse
Wiring Details
Arduino UNO
- 5V connected to +VCC of Ultrasonic Sensor and vcc of Servo.
- GND connected to GND of Servo, GND of L298N DC Motor Driver, and GND of Ultrasonic Sensor.
- Vin connected to 12V of L298N DC Motor Driver.
- D10 connected to IN1 of L298N DC Motor Driver.
- D9 connected to IN2 of L298N DC Motor Driver.
- D8 connected to IN3 of L298N DC Motor Driver.
- D7 connected to IN4 of L298N DC Motor Driver.
- D5 connected to pulse of Servo.
- D3 connected to Echo of Ultrasonic Sensor.
- D2 connected to Trigger of Ultrasonic Sensor.
L298N DC Motor Driver
- GND connected to GND of Arduino UNO.
- 12V connected to Vin of Arduino UNO.
- IN1 connected to D10 of Arduino UNO.
- IN2 connected to D9 of Arduino UNO.
- IN3 connected to D8 of Arduino UNO.
- IN4 connected to D7 of Arduino UNO.
- OUT1 connected to pin 2 of DC Motor 1.
- OUT2 connected to pin 1 of DC Motor 1.
- OUT3 connected to pin 2 of DC Motor 2.
- OUT4 connected to pin 1 of DC Motor 2.
Ultrasonic Sensor
- +VCC connected to 5V of Arduino UNO.
- GND connected to GND of Arduino UNO.
- Trigger connected to D2 of Arduino UNO.
- Echo connected to D3 of Arduino UNO.
DC Motor 1
- pin 1 connected to OUT2 of L298N DC Motor Driver.
- pin 2 connected to OUT1 of L298N DC Motor Driver.
DC Motor 2
- pin 1 connected to OUT4 of L298N DC Motor Driver.
- pin 2 connected to OUT3 of L298N DC Motor Driver.
Servo
- gnd connected to GND of Arduino UNO.
- vcc connected to 5V of Arduino UNO.
- pulse connected to D5 of Arduino UNO.
Code Documentation
/*
* This Arduino sketch controls an obstacle-avoiding robot with two DC motors,
* an ultrasonic sensor, and a servo motor. The robot moves forward until an
* obstacle is detected, at which point it stops, scans for a clear path using
* the servo-mounted ultrasonic sensor, and then turns in the direction of the
* clear path.
*/
#include <Servo.h>
// Pin definitions
const int motor1Pin1 = 10;
const int motor1Pin2 = 9;
const int motor2Pin1 = 8;
const int motor2Pin2 = 7;
const int triggerPin = 2;
const int echoPin = 3;
const int servoPin = 5;
Servo myServo;
void setup() {
// Initialize motor control pins as outputs
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
// Initialize ultrasonic sensor pins
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize servo
myServo.attach(servoPin);
myServo.write(90); // Center the servo
Serial.begin(9600);
}
long readUltrasonicDistance() {
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
return pulseIn(echoPin, HIGH) * 0.034 / 2;
}
void moveForward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
void stopMotors() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
void turnLeft() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
void turnRight() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
void loop() {
moveForward();
if (readUltrasonicDistance() < 10) {
stopMotors();
delay(500);
myServo.write(0);
delay(1000);
long distanceLeft = readUltrasonicDistance();
myServo.write(180);
delay(1000);
long distanceRight = readUltrasonicDistance();
myServo.write(90);
delay(500);
if (distanceLeft > distanceRight) {
turnLeft();
} else {
turnRight();
}
delay(500);
}
}
This code controls the obstacle-avoiding robot. It initializes the pins for the motors, ultrasonic sensor, and servo motor. The robot moves forward until an obstacle is detected. When an obstacle is detected, the robot stops, scans for a clear path using the servo-mounted ultrasonic sensor, and then turns in the direction of the clear path.