Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

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

Image of Arduino UNO Controlled Line-Following Robot with Dual IR Sensors and L298N Motor Driver

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

  • 5V pin connected to the VCC of both IR sensors.
  • GND pin connected to the GND of both IR sensors, L298N GND, and 9V Battery -.
  • Vin pin connected to L298N 5V.
  • Digital pins D13, D12, D11, D10, D6, D5, D4, and D2 are used to control the L298N motor driver and read from the IR sensors.

9V Battery

  • + connected to L298N 12V.
  • - connected to Arduino GND and L298N GND.

DC Motors

  • Motor 1 connected to L298N OUT1 and OUT2.
  • Motor 2 connected to L298N OUT3 and OUT4.

L298N DC Motor Driver

  • 5V connected to Arduino Vin.
  • GND connected to 9V Battery - and Arduino GND.
  • IN1, IN2, IN3, IN4, ENA, and ENB controlled by Arduino digital pins D13, D12, D11, D10, D6, and D5 respectively.
  • OUT1, OUT2, OUT3, and OUT4 connected to the respective pins of the DC motors.

IR Sensors

  • VCC connected to Arduino 5V.
  • GND connected to Arduino GND.
  • out connected to Arduino digital pins D4 and D2.

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.