Arduino UNO Obstacle Avoidance 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 avoidance robot circuit. The circuit includes an Arduino UNO microcontroller, an HC-SR04 ultrasonic sensor for distance measurement, an L298N motor driver to control two DC motors, and a micro servo for additional movement. The robot is powered by a 12V battery.
Component List
Battery 12V
- Description: Power source for the circuit.
- Pins: +, -
L298N DC Motor Driver
- Description: Motor driver to control two DC motors.
- 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
Micro Servo 9G
- Description: Small servo motor for additional movement.
- Pins: GND, +5V, PWM
Motor with Reducer (x2)
- Description: DC motor with a gear reducer.
- Pins: 3 - 6 VCC, GND
HC-SR04 Ultrasonic Sensor
- Description: Ultrasonic sensor for distance measurement.
- Pins: VCC, TRIG, ECHO, GND
Arduino UNO
- Description: Microcontroller to control the circuit.
- 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
Wiring Details
Battery 12V
- +: Not connected in the provided net list.
- -: Not connected in the provided net list.
L298N DC Motor Driver
- OUT1: Connected to Motor with Reducer (3 - 6 VCC)
- OUT2: Connected to Motor with Reducer (GND)
- 12V: Connected to Arduino UNO (5V), HC-SR04 Ultrasonic Sensor (VCC), L298N DC Motor Driver (5V), Micro Servo 9G (+5V)
- GND: Connected to Arduino UNO (GND), HC-SR04 Ultrasonic Sensor (GND)
- 5V: Connected to Arduino UNO (5V), HC-SR04 Ultrasonic Sensor (VCC), Micro Servo 9G (+5V)
- OUT3: Connected to Motor with Reducer (3 - 6 VCC)
- OUT4: Connected to Motor with Reducer (GND)
- IN1: Connected to Arduino UNO (D7)
- IN2: Connected to Arduino UNO (D6)
- IN3: Connected to Arduino UNO (D5)
- IN4: Connected to Arduino UNO (D4)
Micro Servo 9G
- GND: Connected to Arduino UNO (GND)
- +5V: Connected to Arduino UNO (5V), HC-SR04 Ultrasonic Sensor (VCC), L298N DC Motor Driver (12V), L298N DC Motor Driver (5V)
- PWM: Connected to Arduino UNO (D3)
Motor with Reducer
- 3 - 6 VCC: Connected to L298N DC Motor Driver (OUT1)
- GND: Connected to L298N DC Motor Driver (OUT2)
Motor with Reducer
- 3 - 6 VCC: Connected to L298N DC Motor Driver (OUT3)
- GND: Connected to L298N DC Motor Driver (OUT4)
HC-SR04 Ultrasonic Sensor
- VCC: Connected to Arduino UNO (5V), L298N DC Motor Driver (12V), L298N DC Motor Driver (5V), Micro Servo 9G (+5V)
- TRIG: Connected to Arduino UNO (D8)
- ECHO: Connected to Arduino UNO (D9)
- GND: Connected to Arduino UNO (GND), L298N DC Motor Driver (GND)
Arduino UNO
- 5V: Connected to HC-SR04 Ultrasonic Sensor (VCC), L298N DC Motor Driver (12V), L298N DC Motor Driver (5V), Micro Servo 9G (+5V)
- GND: Connected to Micro Servo 9G (GND), HC-SR04 Ultrasonic Sensor (GND), L298N DC Motor Driver (GND)
- D9: Connected to HC-SR04 Ultrasonic Sensor (ECHO)
- D8: Connected to HC-SR04 Ultrasonic Sensor (TRIG)
- D7: Connected to L298N DC Motor Driver (IN1)
- D6: Connected to L298N DC Motor Driver (IN2)
- D5: Connected to L298N DC Motor Driver (IN3)
- D4: Connected to L298N DC Motor Driver (IN4)
- D3: Connected to Micro Servo 9G (PWM)
Documented Code
/*
* Obstacle Avoidance Robot with Servo
* This Arduino sketch controls an obstacle avoidance robot using an ultrasonic
* sensor (HC-SR04) for distance measurement, a motor driver (L298N) to control
* two DC motors, and a micro servo for additional movement.
*/
#include <Servo.h>
// Pin definitions
const int trigPin = 8;
const int echoPin = 9;
const int motorIn1 = 7;
const int motorIn2 = 6;
const int motorIn3 = 5;
const int motorIn4 = 4;
const int servoPin = 3;
Servo myServo;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize motor control pins
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(motorIn3, OUTPUT);
pinMode(motorIn4, OUTPUT);
// Initialize ultrasonic sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Attach servo to pin
myServo.attach(servoPin);
// Set initial servo position
myServo.write(25);
}
void loop() {
// Measure distance
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;
// Print distance to serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Obstacle avoidance logic
if (distance < 20) {
// Stop motors
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, LOW);
// Move servo to scan area
myServo.write(0);
delay(500);
myServo.write(90);
delay(500);
myServo.write(155);
} else {
// Move forward
digitalWrite(motorIn1, HIGH);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, HIGH);
digitalWrite(motorIn4, LOW);
}
delay(100);
}
This code initializes the pins for the motors, ultrasonic sensor, and servo. It measures the distance using the ultrasonic sensor and controls the motors and servo based on the distance to avoid obstacles.