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 car using an Arduino UNO microcontroller. The circuit includes various components such as an ultrasonic sensor, a servo motor, a DC motor driver, multiple motors with reducers, a rocker switch, and Li-ion batteries. The Arduino UNO is programmed to control the motors and the ultrasonic sensor to navigate the car and avoid obstacles.
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
Tower Pro SG90 Servo
- Description: Micro servo motor.
- Pins: Signal, +5V, GND
HC-SR04 Ultrasonic Sensor
- Description: Ultrasonic distance sensor.
- Pins: VCC, TRIG, ECHO, GND
Motor with Reducer
- Description: DC motor with a gear reducer.
- Pins: 3 - 6 VCC, GND
18650 Li-ion Battery x 2
- Description: Rechargeable lithium-ion batteries.
- Pins: +, -
Rocker Switch
- Description: On/Off switch.
- Pins: 1, 2
DC Motor
- Description: Standard DC motor.
- Pins: pin 1, pin 2
Wiring Details
Arduino UNO
- 3.3V connected to VCC of HC-SR04 Ultrasonic Sensor
- 5V connected to +5V of Tower Pro SG90 Servo
- GND connected to GND of Tower Pro SG90 Servo and GND of HC-SR04 Ultrasonic Sensor
- D11 connected to ECHO of HC-SR04 Ultrasonic Sensor
- D10 connected to ENB of L298N DC Motor Driver
- D9 connected to ENA of L298N DC Motor Driver
- D8 connected to TRIG of HC-SR04 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 Signal of Tower Pro SG90 Servo
L298N DC Motor Driver
- OUT1 connected to 3 - 6 VCC of two Motor with Reducers
- OUT2 connected to GND of two Motor with Reducers
- OUT3 connected to 3 - 6 VCC of two Motor with Reducers
- OUT4 connected to GND of two Motor with Reducers
- 12V connected to + of 18650 Li-ion Battery x 2 and pin 1 of Rocker Switch
- GND connected to - of 18650 Li-ion Battery x 2 and pin 2 of Rocker Switch
Tower Pro SG90 Servo
- +5V connected to 5V of Arduino UNO
- GND connected to GND of Arduino UNO
- Signal connected to D3 of Arduino UNO
HC-SR04 Ultrasonic Sensor
- VCC connected to 3.3V of Arduino UNO
- TRIG connected to D8 of Arduino UNO
- ECHO connected to D11 of Arduino UNO
- GND connected to GND of Arduino UNO
Motor with Reducer
- 3 - 6 VCC connected to OUT1 and OUT3 of L298N DC Motor Driver
- GND connected to OUT2 and OUT4 of L298N DC Motor Driver
18650 Li-ion Battery x 2
- + connected to 12V of L298N DC Motor Driver and pin 1 of Rocker Switch
- - connected to GND of L298N DC Motor Driver and pin 2 of Rocker Switch
Rocker Switch
- 1 connected to + of 18650 Li-ion Battery x 2 and 12V of L298N DC Motor Driver
- 2 connected to - of 18650 Li-ion Battery x 2 and GND of L298N DC Motor Driver
DC Motor
- pin 1 connected to pin 2 of Rocker Switch
- pin 2 connected to + of 18650 Li-ion Battery x 2
Code Documentation
#include <Servo.h>
#define trigPin 8
#define echoPin 11
#define ENA 9 // PWM pin for motor A
#define ENB 10 // PWM pin for motor B
#define IN1 7
#define IN2 6
#define IN3 5
#define IN4 4
Servo ultrasonicServo;
long duration;
int distance;
int servoAngle = 90;
int motorSpeed = 250; // Adjust this value for slow speed (0 to 255)
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT); // Updated to pin 9
pinMode(ENB, OUTPUT); // Updated to pin 10
Serial.begin(9600);
ultrasonicServo.attach(3);
ultrasonicServo.write(servoAngle); // Start with the servo facing forward
}
void loop() {
distance = measureDistance();
Serial.print("Distance: ");
Serial.println(distance); // Debugging output
if (distance > 0 && distance < 30) { // Obstacle detected within 30 cm
stopCar();
delay(500);
// Move backward for a short duration
moveBackward();
delay(1000); // Adjust this delay as needed for proper backward movement
stopCar();
delay(500);
// Check distances to the left and right
int distanceLeft = lookLeft();
int distanceRight = lookRight();
ultrasonicServo.write(90); // Reset servo to forward position
// Decide direction based on distances
if (distanceLeft > distanceRight) {
turnLeft();
} else {
turnRight();
}
delay(1000); // Allow time for turning
} else {
moveForward();
}
}
int measureDistance() {
const int samples = 5; // Number of samples for averaging
long sum = 0;
for (int i = 0; i < samples; i++) {
// Send ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read echo and calculate distance
duration = pulseIn(echoPin, HIGH);
int measuredDistance = duration * 0.034 / 2; // Convert to centimeters
sum += measuredDistance;
delay(10); // Small delay between readings
}
return sum / samples; // Return the average distance
}
int lookRight() {
ultrasonicServo.write(0); // Turn servo to the right
delay(500);
int distance = measureDistance();
delay(100);
return distance;
}
int lookLeft() {
ultrasonicServo.write(180); // Turn servo to the left
delay(500);
int distance = measureDistance();
delay(100);
return distance;
}
void moveForward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA, motorSpeed); // Speed control
analogWrite(ENB, motorSpeed);
}
void moveBackward() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4,