Arduino UNO Controlled Line-Following Robot with Dual IR Sensors and L298N Motor Driver
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control a pair of DC motors using an Arduino UNO microcontroller in conjunction with an L298N DC motor driver. The system also includes two infrared sensors for detecting line tracking, which is commonly used in robotics for following paths. The Arduino UNO is powered by a 9V battery and controls the motor driver, which in turn controls the speed and direction of the DC motors. The infrared sensors are used to provide input to the Arduino, which then makes decisions to adjust the motors accordingly.
Component List
Arduino UNO
- Microcontroller board based on the ATmega328P
- It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.
9V Battery
- Provides power to the circuit.
DC Motor (x2)
- Converts electrical energy into mechanical rotation.
- Used for driving the wheels or other mechanical parts.
L298N DC Motor Driver
- An integrated monolithic circuit in a 15-lead Multiwatt and PowerSO20 packages.
- It is a high voltage, high current dual full-bridge driver designed to accept standard TTL logic levels and drive inductive loads such as relays, solenoids, DC and stepping motors.
IR Sensor (x2)
- Infrared sensors used for line tracking.
- Typically outputs a digital signal indicating the detection of a line or its absence.
Wiring Details
Arduino UNO
5Vpin connected to the VCC of both IR sensors.GNDpin connected to the GND of both IR sensors, L298N GND, and 9V Battery-.Vinpin connected to L298N5V.- Digital pins
D13,D12,D11,D10,D6,D5,D4, andD2are used to control the L298N motor driver and read from the IR sensors.
9V Battery
+connected to L298N12V.-connected to Arduino GND and L298N GND.
DC Motors
- Motor 1 connected to L298N
OUT1andOUT2. - Motor 2 connected to L298N
OUT3andOUT4.
L298N DC Motor Driver
5Vconnected to ArduinoVin.GNDconnected to 9V Battery-and Arduino GND.IN1,IN2,IN3,IN4,ENA, andENBcontrolled by Arduino digital pinsD13,D12,D11,D10,D6, andD5respectively.OUT1,OUT2,OUT3, andOUT4connected to the respective pins of the DC motors.
IR Sensors
VCCconnected to Arduino5V.GNDconnected to Arduino GND.outconnected to Arduino digital pinsD4andD2.
Documented Code
// Definición de los pines para el control de la velocidad
int VelocidadMotor1 = 6;
int VelocidadMotor2 = 5;
// Definición de los pines de control de giro de los motores
int Motor1A = 13;
int Motor1B = 12;
int Motor2C = 11;
int Motor2D = 10;
// Sensores infrarrojo - izquierdo y derecho
int infraPin = 2;
int infraPinL = 4;
// Variables para la captura de los valores: 0 - fondo claro y 1 - línea negra
int valorInfra = 0;
int valorInfral = 0;
// Configuración inicial
void setup() {
Serial.begin(9600);
delay(1000);
// Establecemos modo de los pines de los sensores infrarrojo
pinMode(infraPin, INPUT);
pinMode(infraPinL, INPUT);
// Establecemos modo de los pines del control de motores
pinMode(Motor1A, OUTPUT);
pinMode(Motor1B, OUTPUT);
pinMode(Motor2C, OUTPUT);
pinMode(Motor2D, OUTPUT);
// Configuramos los dos motores a velocidad 150/255
analogWrite(VelocidadMotor1, 150);
analogWrite(VelocidadMotor2, 150);
// Configuramos sentido de giro inicial (motores apagados en este caso)
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, LOW);
}
// Ejecución continua
void loop() {
// Leemos el valor de los sensores infrarrojos: 0 - fondo claro y 1 - línea negra
valorInfra = digitalRead(infraPin);
valorInfral = digitalRead(infraPinL);
Serial.print("Valor Infra: ");
Serial.print(valorInfra);
Serial.print(" Valor Infral: ");
Serial.println(valorInfral);
// Cuatro escenarios:
// Ninguno en línea
if (valorInfra == 0 && valorInfral == 0) {
Serial.println("Ninguno en línea");
// Ambos motores hacia adelante
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, HIGH);
digitalWrite(Motor2D, LOW);
}
// Derecho en línea
else if (valorInfra == 0 && valorInfral == 1) {
Serial.println("Derecho en línea");
// Girar a la derecha
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, HIGH);
digitalWrite(Motor2D, LOW);
}
// Izquierdo en línea
else if (valorInfra == 1 && valorInfral == 0) {
Serial.println("Izquierdo en línea");
// Girar a la izquierda
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, LOW);
}
// Ambos sensores en línea (final del circuito)
else if (valorInfra == 1 && valorInfral == 1) {
Serial.println("Ambos en línea");
// Parar los motores
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, LOW);
}
delay(100); // Añadido para evitar lecturas demasiado rápidas
}
This code is designed to be uploaded to an Arduino UNO microcontroller. It sets up the pins for motor control and reads from two infrared sensors to determine the path for a line-following robot. Depending on the sensor readings, it adjusts the motor speeds and directions to follow the line.