Arduino UNO-Based Autonomous Obstacle-Avoiding Robot with Ultrasonic Sensor and Servo Control
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This document provides a detailed overview of a circuit that includes an Arduino UNO microcontroller, a servo motor, an ultrasonic sensor, a DC motor driver, two hobby gearmotors, and a pair of 18650 Li-ion batteries. The circuit is designed to control the movement of the motors based on the distance measured by the ultrasonic sensor, with additional control provided by the servo motor.
Component List
Arduino UNO
- Description: A 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
Servo
- Description: A standard servo motor.
- Pins: gnd, vcc, pulse
HC-SR04 Ultrasonic Sensor
- Description: An ultrasonic sensor used for distance measurement.
- Pins: VCC, TRIG, ECHO, GND
L298N DC Motor Driver
- Description: A 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
18650 Li-ion Battery x 2
- Description: A pair of 18650 Li-ion batteries.
- Pins: +, -
Hobby Gearmotor with 48:1 Gearbox
- Description: A hobby gearmotor with a 48:1 gearbox.
- Pins: pin 1, pin 2
Hobby Gearmotor with 48:1 Gearbox
- Description: A hobby gearmotor with a 48:1 gearbox.
- Pins: pin 1, pin 2
Wiring Details
Arduino UNO
- 3.3V connected to 5V of L298N DC Motor Driver
- 5V connected to vcc of Servo, 12V of L298N DC Motor Driver, and + of 18650 Li-ion Battery x 2
- GND connected to gnd of Servo, GND of L298N DC Motor Driver, GND of HC-SR04 Ultrasonic Sensor, and - of 18650 Li-ion Battery x 2
- Vin connected to VCC of HC-SR04 Ultrasonic Sensor
- A2 connected to ECHO of HC-SR04 Ultrasonic Sensor
- A3 connected to TRIG of HC-SR04 Ultrasonic Sensor
- D10 connected to IN1 of L298N DC Motor Driver
- D9 connected to IN2 of L298N DC Motor Driver
- D8 connected to IN3 of L298N DC Motor Driver
- D7 connected to IN4 of L298N DC Motor Driver
- D5 connected to pulse of Servo
Servo
- vcc connected to 5V of Arduino UNO
- gnd connected to GND of Arduino UNO
- pulse connected to D5 of Arduino UNO
HC-SR04 Ultrasonic Sensor
- VCC connected to Vin of Arduino UNO
- TRIG connected to A3 of Arduino UNO
- ECHO connected to A2 of Arduino UNO
- GND connected to GND of Arduino UNO
L298N DC Motor Driver
- 5V connected to 3.3V of Arduino UNO
- 12V connected to 5V of Arduino UNO
- GND connected to GND of Arduino UNO
- IN1 connected to D10 of Arduino UNO
- IN2 connected to D9 of Arduino UNO
- IN3 connected to D8 of Arduino UNO
- IN4 connected to D7 of Arduino UNO
- OUT1 connected to pin 1 of Hobby Gearmotor with 48:1 Gearbox
- OUT2 connected to pin 2 of Hobby Gearmotor with 48:1 Gearbox
- OUT3 connected to pin 1 of Hobby Gearmotor with 48:1 Gearbox
- OUT4 connected to pin 2 of Hobby Gearmotor with 48:1 Gearbox
18650 Li-ion Battery x 2
- + connected to 5V of Arduino UNO
- - connected to GND of Arduino UNO
Hobby Gearmotor with 48:1 Gearbox
- pin 1 connected to OUT1 of L298N DC Motor Driver
- pin 2 connected to OUT2 of L298N DC Motor Driver
Hobby Gearmotor with 48:1 Gearbox
- pin 1 connected to OUT3 of L298N DC Motor Driver
- pin 2 connected to OUT4 of L298N DC Motor Driver
Code Documentation
#include <Servo.h> // Servo motor library. This is standard library
#include <NewPing.h> // Ultrasonic sensor function library. You must install this library
// our L298N control pins
const int LeftMotorForward = 10;
const int LeftMotorBackward = 9;
const int RightMotorForward = 8;
const int RightMotorBackward = 7;
// sensor pins
#define trig_pin A3 // analog input 1
#define echo_pin A2 // analog input 2
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance); // sensor function
Servo servo_motor; // our servo name
void setup() {
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
servo_motor.attach(5); // our servo pin
servo_motor.write(90);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 10) {
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceRight = lookRight();
delay(300);
distanceLeft = lookLeft();
delay(300);
if (distance >= distanceLeft) {
turnRight();
moveStop();
} else {
turnLeft();
moveStop();
}
} else {
moveForward();
}
distance = readPing();
}
int lookRight() {
servo_motor.write(10);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(90);
return distance;
}
int lookLeft() {
servo_motor.write(170);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(90);
return distance;
delay(100);
}
int readPing() {
delay(70);
int cm = sonar.ping_cm();
if (cm == 0) {
cm = 250;
}
return cm;
}
void moveStop() {
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
void moveForward() {
if (!goesForward) {
goesForward = true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
}
void moveBackward() {
goesForward = false;
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
}
void turnRight() {
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward,