Arduino UNO-Based Obstacle-Avoiding Robot with Ultrasonic Sensor and L298N Motor Driver
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This document provides a detailed overview of a circuit designed to control a robotic vehicle using an Arduino UNO microcontroller. The vehicle is equipped with an HC-SR04 ultrasonic sensor for obstacle detection, a Tower Pro SG90 servo for directional control, and an L298N motor driver to control four DC motors. The circuit is powered by a 3xAA battery pack and includes a rocker switch for power control.
Component List
HC-SR04 Ultrasonic Sensor
- Description: Ultrasonic distance sensor
- Pins: VCC, TRIG, ECHO, GND
Arduino UNO
- Description: Microcontroller board
- 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
- 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
Motor amarillo motorreductor hobby (4 units)
- Description: DC motor with gearbox
- Pins: vcc, GND
3xAA Battery
- Description: Battery pack providing 4.5V
- Pins: VCC, GND
Rocker Switch (SPST)
- Description: Single-pole single-throw switch
- Pins: 1, 2
Tower Pro SG90 Servo
- Description: Micro servo motor
- Pins: Signal, +5V, GND
Wiring Details
HC-SR04 Ultrasonic Sensor
- VCC connected to 5V on Arduino UNO
- TRIG connected to D12 on Arduino UNO
- ECHO connected to D11 on Arduino UNO
- GND connected to GND on Arduino UNO
Arduino UNO
- 5V connected to VCC on HC-SR04 Ultrasonic Sensor and +5V on Tower Pro SG90 Servo
- GND connected to GND on HC-SR04 Ultrasonic Sensor, GND on Tower Pro SG90 Servo, and GND on L298N DC Motor Driver
- Vin connected to 5V on L298N DC Motor Driver
- D9 connected to Signal on Tower Pro SG90 Servo
- D8 connected to IN2 on L298N DC Motor Driver
- D7 connected to IN1 on L298N DC Motor Driver
- D6 connected to ENA on L298N DC Motor Driver
- D5 connected to IN3 on L298N DC Motor Driver
- D4 connected to IN4 on L298N DC Motor Driver
- D3 connected to ENB on L298N DC Motor Driver
L298N DC Motor Driver
- GND connected to GND on 3xAA Battery and GND on Arduino UNO
- 5V connected to Vin on Arduino UNO
- IN2 connected to D8 on Arduino UNO
- IN1 connected to D7 on Arduino UNO
- ENA connected to D6 on Arduino UNO
- IN3 connected to D5 on Arduino UNO
- IN4 connected to D4 on Arduino UNO
- ENB connected to D3 on Arduino UNO
- OUT1 connected to vcc on one Motor amarillo motorreductor hobby and OUT2 connected to GND on another Motor amarillo motorreductor hobby
- OUT3 connected to vcc on one Motor amarillo motorreductor hobby and OUT4 connected to GND on another Motor amarillo motorreductor hobby
- 12V connected to 2 on Rocker Switch (SPST)
Motor amarillo motorreductor hobby
- vcc connected to OUT1 on L298N DC Motor Driver
- GND connected to OUT2 on L298N DC Motor Driver
- vcc connected to OUT3 on L298N DC Motor Driver
- GND connected to OUT4 on L298N DC Motor Driver
3xAA Battery
- VCC connected to 1 on Rocker Switch (SPST)
- GND connected to GND on L298N DC Motor Driver
Rocker Switch (SPST)
- 1 connected to VCC on 3xAA Battery
- 2 connected to 12V on L298N DC Motor Driver
Tower Pro SG90 Servo
- Signal connected to D9 on Arduino UNO
- +5V connected to 5V on Arduino UNO
- GND connected to GND on Arduino UNO
Documented Code
#include <Servo.h> // Standard library for the servo
#include <NewPing.h> // Library for ultrasonic sensor function
// L298N motor control pins
const int LeftMotorForward = 8;
const int LeftMotorBackward = 7;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;
const int enA = 6; // Speed control for left motor
const int enB = 3; // Speed control for right motor
// Sensor pins
#define trig_pin 12
#define echo_pin 11
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
const int motorSpeed = 100; // Set motor speed (0-255, lower = slower)
const int obstacleThreshold = 25; // Minimum distance to detect obstacle (in cm)
NewPing sonar(trig_pin, echo_pin, maximum_distance); // Sensor function
Servo servo_motor;
void setup() {
// Initialize motor pins
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
// Attach servo motor
servo_motor.attach(9);
servo_motor.write(115); // Center the servo
delay(1000);
// Initialize sensor
distance = readPing();
}
void loop() {
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= obstacleThreshold) { // Use new threshold
moveStop();
delay(200);
moveBackward();
delay(300);
moveStop();
delay(200);
distanceRight = lookRight();
distanceLeft = lookLeft();
if (distanceRight >= distanceLeft) {
turnRight();
} else {
turnLeft();
}
} else {
moveForward();
}
distance = readPing();
}
int lookRight() {
servo_motor.write(50);
delay(300);
int distance = readPing();
delay(100);
servo_motor.write(115); // Reset servo to center
return distance;
}
int lookLeft() {
servo_motor.write(170);
delay(300);
int distance = readPing();
delay(100);
servo_motor.write(115); // Reset servo to center
return distance;
}
int readPing() {
delay(50);
int cm = sonar.ping_cm();
if (cm == 0) {
cm = 250; // Assume no obstacle if out of range
}
return cm;
}
// Stop the motors
void moveStop() {
analogWrite(enA, 0);
analogWrite(enB, 0);
}
// Move forward with controlled speed
void moveForward() {
if (!goesForward) {
goesForward = true;
analogWrite(enA, motorSpeed);
analogWrite(enB, motorSpeed);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
}
// Move backward with controlled speed
void moveBackward() {
goesForward = false;
analogWrite(enA, motorSpeed);
analogWrite(enB, motorSpeed);
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
}
// Turn right
void turnRight() {
analogWrite(enA, 150);
analogWrite(enB,