Arduino UNO Obstacle-Avoiding Robot with Ultrasonic Sensor and L298N Motor Driver
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This document describes an obstacle-avoiding robot circuit. The robot uses an ultrasonic sensor to detect obstacles and a servo motor to change direction when an obstacle is detected. The L298N motor driver controls the DC motors for movement. When an obstacle is detected within 15 cm, the robot moves back 20 cm, then moves forward 20 cm, and stops.
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
Ultrasonic Sensor
- Description: Sensor used to measure distance to an object using ultrasonic waves.
- Pins: +VCC, Trigger, Echo, GND
L298N DC Motor Driver
- Description: Dual H-Bridge motor driver for controlling 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 used for precise control of angular position.
- Pins: GND, +5V, PWM
Motor amarillo motorreductor hobby
- Description: Hobby DC motor with gear reduction.
- Pins: vcc, GND
7.4v Battery
- Description: Power source for the circuit.
- Pins: +, -
Wiring Details
Arduino UNO
- 5V connected to 5V of L298N DC Motor Driver
- GND connected to GND of L298N DC Motor Driver
- GND connected to GND of Ultrasonic Sensor
- D9 connected to Echo of Ultrasonic Sensor
- D8 connected to Trigger of Ultrasonic Sensor
- D7 connected to IN1 of L298N DC Motor Driver
- D6 connected to IN2 of L298N DC Motor Driver
- D5 connected to IN3 of L298N DC Motor Driver
- D4 connected to IN4 of L298N DC Motor Driver
- D3 connected to PWM of Micro Servo 9G
Ultrasonic Sensor
- +VCC connected to 5V of Arduino UNO
- GND connected to GND of Arduino UNO
- Echo connected to D9 of Arduino UNO
- Trigger connected to D8 of Arduino UNO
L298N DC Motor Driver
- 5V connected to 5V of Arduino UNO
- GND connected to GND of Arduino UNO
- IN1 connected to D7 of Arduino UNO
- IN2 connected to D6 of Arduino UNO
- IN3 connected to D5 of Arduino UNO
- IN4 connected to D4 of Arduino UNO
- OUT1 connected to GND of Motor amarillo motorreductor hobby
- OUT2 connected to vcc of Motor amarillo motorreductor hobby
- OUT3 connected to GND of Motor amarillo motorreductor hobby
- OUT4 connected to vcc of Motor amarillo motorreductor hobby
Micro Servo 9G
- +5V connected to 5V of Arduino UNO
- GND connected to GND of Arduino UNO
- PWM connected to D3 of Arduino UNO
Motor amarillo motorreductor hobby
- GND connected to OUT1 of L298N DC Motor Driver
- vcc connected to OUT2 of L298N DC Motor Driver
Motor amarillo motorreductor hobby
- GND connected to OUT3 of L298N DC Motor Driver
- vcc connected to OUT4 of L298N DC Motor Driver
7.4v Battery
- + (not connected in the provided net list)
- - (not connected in the provided net list)
Code Documentation
/*
* This Arduino sketch controls an obstacle-avoiding robot with a servo motor
* and an ultrasonic sensor. The robot uses the ultrasonic sensor to detect
* obstacles and the servo motor to change direction when an obstacle is
* detected. The L298N motor driver controls the DC motors for movement.
* When an obstacle is detected within 15 cm, the robot moves back 20 cm,
* then moves forward 20 cm, and stops.
*/
// Pin definitions
const int trigPin = 8;
const int echoPin = 9;
const int servoPin = 3;
const int motorIn1 = 7;
const int motorIn2 = 6;
const int motorIn3 = 5;
const int motorIn4 = 4;
// Motor control
const int motorSpeed = 255;
#include <Servo.h>
Servo myServo;
void setup() {
// Initialize pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(motorIn3, OUTPUT);
pinMode(motorIn4, OUTPUT);
myServo.attach(servoPin);
myServo.write(90); // Center the servo
}
void loop() {
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 < 15) { // If obstacle is detected within 15 cm
moveBackward(20); // Move back 20 cm
moveForward(20); // Move forward 20 cm
stopMotors(); // Stop
} else {
moveForward();
}
}
void moveForward(int cm = 0) {
if (cm > 0) {
long duration = cm * 29.1 * 2;
unsigned long startTime = millis();
while (millis() - startTime < duration) {
analogWrite(motorIn1, motorSpeed);
analogWrite(motorIn2, 0);
analogWrite(motorIn3, motorSpeed);
analogWrite(motorIn4, 0);
}
stopMotors();
} else {
analogWrite(motorIn1, motorSpeed);
analogWrite(motorIn2, 0);
analogWrite(motorIn3, motorSpeed);
analogWrite(motorIn4, 0);
}
}
void moveBackward(int cm) {
long duration = cm * 29.1 * 2;
unsigned long startTime = millis();
while (millis() - startTime < duration) {
analogWrite(motorIn1, 0);
analogWrite(motorIn2, motorSpeed);
analogWrite(motorIn3, 0);
analogWrite(motorIn4, motorSpeed);
}
stopMotors();
}
void stopMotors() {
analogWrite(motorIn1, 0);
analogWrite(motorIn2, 0);
analogWrite(motorIn3, 0);
analogWrite(motorIn4, 0);
}
This code controls the obstacle-avoiding robot by using the ultrasonic sensor to detect obstacles and the servo motor to change direction. The L298N motor driver controls the DC motors for movement. When an obstacle is detected within 15 cm, the robot moves back 20 cm, then moves forward 20 cm, and stops.