Arduino-Controlled Obstacle-Avoiding Robot with Ultrasonic and IR Sensors
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control a pair of DC motors using an L298N motor driver, with inputs from an HC-SR04 ultrasonic sensor and two IR sensors for object detection and line following capabilities. The brain of the circuit is an Arduino UNO microcontroller, which processes sensor inputs and controls the motor driver accordingly. A 4 x AAA battery mount provides the power source, and a rocker switch is included for power control.
Component List
4 x AAA Battery Mount
- Description: Provides power to the circuit.
- Pins:
-(Negative),+(Positive)
HC-SR04 Ultrasonic Sensor
- Description: Measures distance to objects using ultrasonic waves.
- Pins:
VCC,TRIG,ECHO,GND
Arduino UNO
- Description: Microcontroller board based on the ATmega328P.
- Pins:
UNUSED,IOREF,Reset,3.3V,5V,GND,Vin,A0toA5,SCL,SDA,AREF,D0toD13
IR Sensor
- Description: Detects proximity based on infrared light reflection.
- Pins:
out,gnd,vcc
L298N DC Motor Driver
- Description: Controls the direction and speed of 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
Rocker Switch
- Description: Allows for manual power control.
- Pins:
output,input
Hobby Gearmotor with 48:1 Gearbox
- Description: Provides mechanical power for movement.
- Pins:
pin 1,pin 2
Wiring Details
4 x AAA Battery Mount
-connected to GND of the circuit.+connected to the input of the rocker switch.
HC-SR04 Ultrasonic Sensor
VCCconnected to 5V from the Arduino UNO.TRIGconnected to pinD8on the Arduino UNO.ECHOconnected to pinD9on the Arduino UNO.GNDconnected to GND of the circuit.
Arduino UNO
5VandGNDused to power the IR sensors and the ultrasonic sensor.- Digital pins
D2,D4,D5,D6,D8,D9,D10,D11,D12,D13used for sensor inputs and motor control signals.
IR Sensors
outof one sensor connected to pinD2on the Arduino UNO.outof the other sensor connected to pinD4on the Arduino UNO.gndof both sensors connected to GND of the circuit.vccof both sensors connected to 5V from the Arduino UNO.
L298N DC Motor Driver
OUT1,OUT2,OUT3,OUT4connected to the pins of the two Hobby Gearmotors.12Vconnected to the output of the rocker switch.GNDconnected to GND of the circuit.5Vconnected to 5V from the Arduino UNO.ENA,ENBconnected to pinsD5,D6on the Arduino UNO for speed control.IN1,IN2,IN3,IN4connected to pinsD12,D13,D10,D11on the Arduino UNO for direction control.
Rocker Switch
inputconnected to+from the 4 x AAA Battery Mount.outputconnected to12Von the L298N DC Motor Driver.
Hobby Gearmotors
- Each motor has two pins connected to
OUT1,OUT2,OUT3,OUT4on the L298N DC Motor Driver.
Documented Code
// Definición pines EnA y EnB para el control de la velocidad
int VelocidadMotor1 = 6;
int VelocidadMotor2 = 5;
// Definición de los pines de control de giro de los motores In1, In2, In3 e In4
int Motor1A = 13;
int Motor1B = 12;
int Motor2C = 11;
int Motor2D = 10;
// Sensores infrarrojo - izquierdo y derecho
int infraPin = 2;
int infraPin1 = 4;
// Variables para la captura de los valores: 0 - fondo claro y 1 - línea negra
int valorInfra = 0;
int valorInfra1 = 0;
// Ultrasonicos
#define PIN_TRIG 9
#define PIN_ECHO 8
long duration, distancia;
// Configuración inicial
void setup() {
Serial.begin(9600);
delay(1000);
//configuración del ultrasonico
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
// Establecemos modo de los pines de los sensores infrarrojo
pinMode(infraPin, INPUT);
pinMode(infraPin1, INPUT);
// Establecemos modo de los pines del control de motores
pinMode(Motor1A,OUTPUT);
pinMode(Motor1B,OUTPUT);
pinMode(Motor2C,OUTPUT);
pinMode(Motor2D,OUTPUT);
pinMode(VelocidadMotor1, OUTPUT);
pinMode(VelocidadMotor2, OUTPUT);
// Configuramos los dos motores a velocidad 150/255
analogWrite(VelocidadMotor1, 150);
analogWrite(VelocidadMotor2, 180);
// Configuramos sentido de giro
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, LOW);
}
// Ejecución contínua
void loop() {
// Ultrasonico: Primero, generar un pulso corto de 2-5 microsegundos.
digitalWrite(PIN_TRIG, LOW);
delayMicroseconds(5);
digitalWrite(PIN_TRIG, HIGH);
// Después de ajustar un nivel de señal alto, esperamos unos 10 microsegundos. En este punto el sensor enviará señales con una frecuencia de 40 kHz.
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Tiempo de retardo de la señal acústica en el sonar.
duration = pulseIn(PIN_ECHO, HIGH);
// Ahora es el momento de convertir el tiempo a distancia
distancia = (duration / 2) / 29.1;
Serial.print("Distancia al objeto: ");
Serial.print(distancia);
Serial.println(" см.");
// Leemos el valor de los infrarrojo: 0 - fondo claro y 1 - línea negra
valorInfra = digitalRead(infraPin);
valorInfra1 = digitalRead(infraPin1);
Serial.println(valorInfra);
Serial.println(valorInfra1);
if (distancia > 10){ //mientras no detecte nada a cierta distancia no se detiene
// Cuatro escenarios: De frente
if(valorInfra == 0 && valorInfra1 == 0){
Serial.println("Ninguno en linea");
// Modificamos sentido de giro de los motores
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor2D, HIGH);
delay(20);
digitalWrite(Motor1A, LOW);
digitalWrite(Motor2D,LOW);
delay(20);
}
// El robot encuentra línea negra con el infrarrojo derecho y hay que corregir girando a la derecha
if(valorInfra == 0 && valorInfra1 == 1){
Serial.println("Derecho en linea");
// Modificamos sentido de giro de los motores
digitalWrite(Motor1A, LOW);
digitalWrite(Motor2D,LOW);
delay(25);
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor2D,LOW);
delay(20);
}
// El robot encuentra línea negra con el infrarrojo izquierdo y hay que corregir girando a la izquierda
if(valorInfra == 1 && valorInfra1 == 0){
Serial.println("Iz