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.
-
(Negative), +
(Positive)VCC
, TRIG
, ECHO
, GND
UNUSED
, IOREF
, Reset
, 3.3V
, 5V
, GND
, Vin
, A0
to A5
, SCL
, SDA
, AREF
, D0
to D13
out
, gnd
, vcc
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
output
, input
pin 1
, pin 2
-
connected to GND of the circuit.+
connected to the input of the rocker switch.VCC
connected to 5V from the Arduino UNO.TRIG
connected to pin D8
on the Arduino UNO.ECHO
connected to pin D9
on the Arduino UNO.GND
connected to GND of the circuit.5V
and GND
used to power the IR sensors and the ultrasonic sensor.D2
, D4
, D5
, D6
, D8
, D9
, D10
, D11
, D12
, D13
used for sensor inputs and motor control signals.out
of one sensor connected to pin D2
on the Arduino UNO.out
of the other sensor connected to pin D4
on the Arduino UNO.gnd
of both sensors connected to GND of the circuit.vcc
of both sensors connected to 5V from the Arduino UNO.OUT1
, OUT2
, OUT3
, OUT4
connected to the pins of the two Hobby Gearmotors.12V
connected to the output of the rocker switch.GND
connected to GND of the circuit.5V
connected to 5V from the Arduino UNO.ENA
, ENB
connected to pins D5
, D6
on the Arduino UNO for speed control.IN1
, IN2
, IN3
, IN4
connected to pins D12
, D13
, D10
, D11
on the Arduino UNO for direction control.input
connected to +
from the 4 x AAA Battery Mount.output
connected to 12V
on the L298N DC Motor Driver.OUT1
, OUT2
, OUT3
, OUT4
on the L298N DC Motor Driver.// 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