Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Line-Following Robot with IR Sensors and L298N Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino UNO Line-Following Robot with IR Sensors and L298N Motor Driver

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

  1. 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
  2. 9V Battery

    • Description: A standard 9V battery used to power the circuit.
    • Pins: -, +
  3. IR Sensor (Left)

    • Description: An infrared sensor used to detect the presence of a line.
    • Pins: out, gnd, vcc
  4. IR Sensor (Right)

    • Description: An infrared sensor used to detect the presence of a line.
    • Pins: out, gnd, vcc
  5. 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
  6. DC Motor (Left)

    • Description: A standard DC motor.
    • Pins: pin 1, pin 2
  7. DC Motor (Right)

    • Description: A standard DC motor.
    • Pins: pin 1, pin 2

Wiring Details

Arduino UNO

9V Battery

IR Sensor (Left)

IR Sensor (Right)

L298N DC Motor Driver

DC Motor (Left)

DC Motor (Right)

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() {