Arduino UNO Line-Following Robot with IR Sensors and L298N Motor Driver
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This document provides a detailed overview of a circuit designed to control two DC motors using an Arduino UNO, an L298N motor driver, and two IR sensors. The circuit is powered by a 9V battery. The Arduino UNO reads inputs from the IR sensors and controls the motors through the L298N motor driver based on the sensor inputs.
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
9V Battery
- Description: A standard 9V battery used to power the circuit.
- Pins: -, +
IR Sensor (Left)
- Description: An infrared sensor used to detect the presence of a line.
- Pins: out, gnd, vcc
IR Sensor (Right)
- Description: An infrared sensor used to detect the presence of a line.
- Pins: out, gnd, vcc
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
DC Motor (Left)
- Description: A standard DC motor.
- Pins: pin 1, pin 2
DC Motor (Right)
- Description: A standard DC motor.
- Pins: pin 1, pin 2
Wiring Details
Arduino UNO
- GND: Connected to GND of both IR sensors and L298N motor driver.
- 5V: Connected to vcc of both IR sensors.
- Vin: Connected to 5V of L298N motor driver.
- D13: Connected to IN2 of L298N motor driver.
- D12: Connected to IN1 of L298N motor driver.
- D11: Connected to IN4 of L298N motor driver.
- D10: Connected to IN3 of L298N motor driver.
- D6: Connected to ENA of L298N motor driver.
- D5: Connected to ENB of L298N motor driver.
- D4: Connected to out of the right IR sensor.
- D2: Connected to out of the left IR sensor.
9V Battery
- -: Connected to GND of L298N motor driver.
- +: Connected to 12V of L298N motor driver.
IR Sensor (Left)
- gnd: Connected to GND of Arduino UNO.
- vcc: Connected to 5V of Arduino UNO.
- out: Connected to D2 of Arduino UNO.
IR Sensor (Right)
- gnd: Connected to GND of Arduino UNO.
- vcc: Connected to 5V of Arduino UNO.
- out: Connected to D4 of Arduino UNO.
L298N DC Motor Driver
- 5V: Connected to Vin of Arduino UNO.
- GND: Connected to GND of Arduino UNO and 9V Battery.
- IN2: Connected to D13 of Arduino UNO.
- IN1: Connected to D12 of Arduino UNO.
- IN4: Connected to D11 of Arduino UNO.
- IN3: Connected to D10 of Arduino UNO.
- ENA: Connected to D6 of Arduino UNO.
- ENB: Connected to D5 of Arduino UNO.
- 12V: Connected to + of 9V Battery.
- OUT1: Connected to pin 2 of the left DC motor.
- OUT2: Connected to pin 1 of the left DC motor.
- OUT3: Connected to pin 2 of the right DC motor.
- OUT4: Connected to pin 1 of the right DC motor.
DC Motor (Left)
- pin 1: Connected to OUT2 of L298N motor driver.
- pin 2: Connected to OUT1 of L298N motor driver.
DC Motor (Right)
- pin 1: Connected to OUT4 of L298N motor driver.
- pin 2: Connected to OUT3 of L298N motor driver.
Documented Code
Arduino UNO 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;
// Configuración inicial
void setup() {
Serial.begin(9600);
delay(1000);
// 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, 150);
// Configuramos sentido de giro
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, LOW);
}
// Ejecución contínua
void loop() {
// 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);
// 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("Izquierdo en linea");
// Modificamos sentido de giro de los motores
digitalWrite(Motor1A,LOW);
digitalWrite(Motor2D, LOW);
delay(25);
digitalWrite(Motor1A,LOW);
digitalWrite(Motor2D, HIGH);
delay(20);
}
// Los dos sensores infrarrojos encuentran una línea negra, el final del circuito
if(valorInfra == 1 && valorInfra1 == 1){
Serial.println("Ambos en linea");
// Paramos los motores
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, LOW);
}
}
L298N DC Motor Driver Code
void setup() {