Arduino UNO-Based 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 Tower Pro SG90 servo motor, an L298N DC motor driver, an HC-SR04 ultrasonic sensor, and two hobby motors. The circuit is designed to control the servo motor and the DC motors based on distance measurements from the ultrasonic sensor.
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
Tower Pro SG90 Servo
- Description: A small servo motor commonly used in hobby projects.
- Pins: Signal, +5V, GND
L298N DC Motor Driver
- Description: A dual H-Bridge motor driver that allows control of 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
HC-SR04 Ultrasonic Sensor
- Description: A sensor used to measure distance by using ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND
Motor amarillo motorreductor hobby
- Description: A small DC motor with a gearbox, commonly used in hobby projects.
- Pins: vcc, GND
Wiring Details
Arduino UNO
5V is connected to:
- L298N DC Motor Driver (5V)
- Tower Pro SG90 Servo (+5V)
- HC-SR04 Ultrasonic Sensor (VCC)
GND is connected to:
- L298N DC Motor Driver (GND)
- Tower Pro SG90 Servo (GND)
- HC-SR04 Ultrasonic Sensor (GND)
D8 is connected to:
- Tower Pro SG90 Servo (Signal)
D7 is connected to:
- L298N DC Motor Driver (IN4)
D6 is connected to:
- L298N DC Motor Driver (IN3)
D5 is connected to:
- L298N DC Motor Driver (IN2)
D4 is connected to:
- L298N DC Motor Driver (IN1)
D3 is connected to:
- HC-SR04 Ultrasonic Sensor (ECHO)
D2 is connected to:
- HC-SR04 Ultrasonic Sensor (TRIG)
Tower Pro SG90 Servo
Signal is connected to:
- Arduino UNO (D8)
+5V is connected to:
- Arduino UNO (5V)
GND is connected to:
- Arduino UNO (GND)
L298N DC Motor Driver
12V is connected to:
- Arduino UNO (5V)
5V is connected to:
- Arduino UNO (5V)
GND is connected to:
- Arduino UNO (GND)
IN4 is connected to:
- Arduino UNO (D7)
IN3 is connected to:
- Arduino UNO (D6)
IN2 is connected to:
- Arduino UNO (D5)
IN1 is connected to:
- Arduino UNO (D4)
OUT1 is connected to:
- Motor amarillo motorreductor hobby (GND)
OUT2 is connected to:
- Motor amarillo motorreductor hobby (vcc)
OUT3 is connected to:
- Motor amarillo motorreductor hobby (GND)
OUT4 is connected to:
- Motor amarillo motorreductor hobby (vcc)
ENA is connected to:
- L298N DC Motor Driver (+5V-J1)
ENB is connected to:
- L298N DC Motor Driver (+5V-J2)
HC-SR04 Ultrasonic Sensor
VCC is connected to:
- Arduino UNO (5V)
TRIG is connected to:
- Arduino UNO (D2)
ECHO is connected to:
- Arduino UNO (D3)
GND is connected to:
- Arduino UNO (GND)
Motor amarillo motorreductor hobby
vcc is connected to:
- L298N DC Motor Driver (OUT2)
- L298N DC Motor Driver (OUT4)
GND is connected to:
- L298N DC Motor Driver (OUT1)
- L298N DC Motor Driver (OUT3)
Code Documentation
#include <Servo.h>
// Pin definitions
const int trigPin = 2;
const int echoPin = 3;
const int servoPin = 8;
const int motorIn1 = 4;
const int motorIn2 = 5;
const int motorIn3 = 6;
const int motorIn4 = 7;
Servo myServo;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(motorIn3, OUTPUT);
pinMode(motorIn4, OUTPUT);
// Attach servo
myServo.attach(servoPin);
myServo.write(90);
}
void loop() {
int distance = measureDistance();
// Distance measure program
}
int measureDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
return distance;
}
// Direction check
int checkDirection(int angle) {
myServo.write(angle);
delay(500);
return measureDistance();
}
This code initializes the pins for the ultrasonic sensor, servo motor, and DC motor driver. It measures the distance using the ultrasonic sensor and controls the servo motor based on the measured distance. The DC motors are controlled through the L298N motor driver.