Arduino UNO Obstacle-Avoiding Robot with Ultrasonic Sensor and Servo
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This document provides a detailed overview of an obstacle-avoiding robot circuit. The robot uses an Arduino UNO microcontroller to control two motors via an L298N motor driver, a servo motor for scanning, and an HC-SR04 ultrasonic sensor for obstacle detection. The robot is powered by a 7.4V battery.
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
Motor with reducer (2 units)
- Description: DC motor with a gear reducer.
- Pins: 3 - 6 VCC, GND
L298N DC motor driver
- Description: Dual H-Bridge motor driver.
- 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
Tower Pro SG90 servo
- Description: Micro servo motor.
- Pins: Signal, +5V, GND
HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)
- Description: Ultrasonic sensor for distance measurement.
- Pins: VCC, TRIG, ECHO, GND
7.4V Battery
- Description: Power source for the circuit.
- Pins: +, -
Wiring Details
Arduino UNO
- 5V connected to:
- Tower Pro SG90 servo (+5V)
- HC-SR04 Ultrasonic Distance Sensor (VCC)
- GND connected to:
- Tower Pro SG90 servo (GND)
- HC-SR04 Ultrasonic Distance Sensor (GND)
- 7.4V Battery (-)
- L298N DC motor driver (GND)
- Vin connected to:
- 7.4V Battery (+)
- L298N DC motor driver (12V)
- D13 connected to:
- HC-SR04 Ultrasonic Distance Sensor (ECHO)
- D12 connected to:
- HC-SR04 Ultrasonic Distance Sensor (TRIG)
- D11 connected to:
- L298N DC motor driver (IN4)
- D10 connected to:
- L298N DC motor driver (IN3)
- D9 connected to:
- L298N DC motor driver (IN2)
- D8 connected to:
- L298N DC motor driver (IN1)
- D6 connected to:
- L298N DC motor driver (ENB)
- D4 connected to:
- Tower Pro SG90 servo (Signal)
- D3 connected to:
- L298N DC motor driver (ENA)
Motor with reducer (1st unit)
- 3 - 6 VCC connected to:
- L298N DC motor driver (OUT1)
- GND connected to:
- L298N DC motor driver (OUT2)
Motor with reducer (2nd unit)
- 3 - 6 VCC connected to:
- L298N DC motor driver (OUT3)
- GND connected to:
- L298N DC motor driver (OUT4)
L298N DC motor driver
- 5V-ENA-JMP-I connected to:
- L298N DC motor driver (5V-ENA-JMP-O)
Documented Code
Arduino UNO Code
/*
* This Arduino sketch controls an obstacle-avoiding robot with a servo.
* The robot uses an HC-SR04 ultrasonic sensor to detect obstacles and
* an L298N motor driver to control two motors. A servo is used to scan
* for obstacles at 20, 90, and 155 degrees. If an obstacle is detected
* at 20 degrees, the robot backs up for 3 seconds and turns left. If an
* obstacle is detected at 155 degrees, the robot backs up for 3 seconds
* and turns right.
*/
// Pin definitions
const int trigPin = 12;
const int echoPin = 13;
const int enA = 3;
const int in1 = 8;
const int in2 = 9;
const int enB = 6;
const int in3 = 10;
const int in4 = 11;
const int servoPin = 4;
#include <Servo.h>
Servo myServo;
void setup() {
// Initialize motor control pins
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Initialize ultrasonic sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize servo
myServo.attach(servoPin);
// Start with motors off
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
long readUltrasonicDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
return pulseIn(echoPin, HIGH) * 0.034 / 2;
}
void moveForward() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, 200);
analogWrite(enB, 200);
}
void rvs() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, 200);
analogWrite(enB, 200);
}
void turnLeft() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, 200);
analogWrite(enB, 200);
}
void turnRight() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, 200);
analogWrite(enB, 200);
}
void stopMoving() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop() {
moveForward();
long distance = readUltrasonicDistance();
if (distance < 10) {
stopMoving();
delay(1000);
myServo.write(20);
delay(1000);
long distanceLeft = readUltrasonicDistance();
myServo.write(155);
delay(1000);
long distanceCenter = readUltrasonicDistance();
myServo.write(90);
delay(1000);
rvs();
delay(500);
stopMoving();
delay(1000);
turnLeft();
delay(200);
stopMoving();
delay(1000);
}
}
L298N DC Motor Driver Code
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
This concludes the documentation for the obstacle-avoiding robot circuit.